可能也会出现java.lang.NoClassDefFoundError 问题
一篇文章提到不要使用sun.misc包下的BASE64Encoder及BASE64Decoder。这两个方法都是sun公司的内部方法,并没有在java api中公开过,所以使用这些方法是不安全的,将来随时可能会从中去除,所以相应的应该使用替代的对象及方法 点击打开链接
所以可以考虑使用Base64
例如:
/**
* 加密
* @param content
* @param key
* @return
*/
public static String encryptAES(String content, String key) {
try {
byte[] byteContent = content.getBytes("UTF-8");
byte[] enCodeFormat = key.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
byte[] initParam = AES_KEY_NUMBER.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encryptedBytes = cipher.doFinal(byteContent);
return new String(Base64.encode(encryptedBytes, Base64.DEFAULT));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
/**
* 解密
* @param content
* @param key
* @return
*/
public static String decryptAES(String content, String key) {
try {
byte[] bytesrc = Base64.decode(content.getBytes(), Base64.DEFAULT);
byte[] enCodeFormat = key.getBytes();
SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
byte[] initParam = AES_KEY_NUMBER.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] result = cipher.doFinal(bytesrc);
return new String(result, "UTF-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
其中
import android.util.Base64;
另外需要导入rt.jar包。