CTR模式
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class CryptoAes {
public static final String KEY_ALGORITHM = "AES";
public static final String CIPHER_ALGORITHM = "AES/CTR/PKCS5Padding";
public static final String ivParameter = "8888888888888888";
public static String Encrypt(String key, String text) {
try {
byte[] keyBytes = key.getBytes();
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
SecretKeySpec sKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String Decrypt(String Key, String text) {
try {
byte[] raw = Key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(text);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String encrypt = Encrypt("88888888888888888888888888888888", "zhangsan");
System.out.println(encrypt);
System.out.println(Decrypt("88888888888888888888888888888888",encrypt));
}
}
ECB模式
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
public class CryptoAes {
private static final String KEY = "8888888888888888";
private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
public CryptoAes() {
}
public static String aesDecrypt(String encrypt) {
try {
return aesDecrypt(encrypt, "8888888888888888");
} catch (Exception var2) {
var2.printStackTrace();
return "";
}
}
public static String aesEncrypt(String content) {
try {
return aesEncrypt(content, "8888888888888888");
} catch (Exception var2) {
var2.printStackTrace();
return "";
}
}
public static String binary(byte[] bytes, int radix) {
return (new BigInteger(1, bytes)).toString(radix);
}
public static String base64Encode(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
public static byte[] base64Decode(String base64Code) throws Exception {
return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
}
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, new SecretKeySpec(encryptKey.getBytes(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
public static String aesEncrypt(String content, String encryptKey) {
try {
if (!StringUtils.isEmpty(encryptKey) && !StringUtils.isEmpty(content)) {
if (encryptKey.length() < 16) {
encryptKey = encryptKey + "8888888888888888";
encryptKey = encryptKey.substring(0, 16);
}
return base64Encode(aesEncryptToBytes(content, encryptKey));
} else {
return null;
}
} catch (Exception var3) {
var3.printStackTrace();
return null;
}
}
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, new SecretKeySpec(decryptKey.getBytes(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
public static String aesDecrypt(String encryptStr, String decryptKey) {
try {
if (!StringUtils.isEmpty(decryptKey) && !StringUtils.isEmpty(encryptStr)) {
if (decryptKey.length() < 16) {
decryptKey = decryptKey + "8888888888888888";
decryptKey = decryptKey.substring(0, 16);
}
return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
} else {
return null;
}
} catch (Exception var3) {
var3.printStackTrace();
return null;
}
}
public static void main(String[] args) throws Exception {
String content = "888888";
System.out.println(aesEncrypt(content, "1"));
System.out.println(aesDecrypt(aesEncrypt(content, "1"), "1"));
}
}