Cipher.getInstance("AES/ECB/PKCS5Padding");
加解密算法
public class CipherUtils {
private static final String ALGORITHM = "AES";
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
private SecretKeySpec key;
public CipherUtils(String hexKey) {
key = new SecretKeySpec(hexKey.getBytes(), ALGORITHM);
}
public String encryptData(String data) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.ENCRYPT_MODE, key);
return new BASE64Encoder().encode(cipher.doFinal(data.getBytes("utf-8")));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String decryptData(String data) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(data)), "utf-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}