注意:base64编码默认一行76个字符,超过会换行,如果他人解密失败可能由于两边base64编码设置不同
ECB模式,不需要偏移量
// 加密
public static String encode(String content, String key) throws Exception {
String enresult = "";
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
byte[] byteContent = content.getBytes("UTF-8");
byte[] crypted = cipher.doFinal(byteContent);
enresult = new BASE64Encoder().encode(crypted);
return enresult;
}
// 解密
public static String decrypt(String content, String key) throws Exception {
String decodeString = "";
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
byte[] output = cipher.doFinal(new BASE64Decoder().decodeBuffer(content));
decodeString = new String(output, "UTF-8");
return decodeString;
}