1
public static String Encrypt(String text){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes= new byte[16];
byte[] b= KEY.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
String result = Base64.encodeToString(results, 0);
//result = URLEncoder.encode(result.trim(),"UTF-8");
return result;
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
public static String Decrypt(String text){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes= new byte[16];
byte[] b= KEY.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
String result = Base64.encodeToString(results, 0);
//result = URLEncoder.encode(result.trim(),"UTF-8");
return result;
} catch (Exception e) {
// TODO: handle exception
Log.d("decryption", e.getMessage());
return null;
}
}
這是加密和解密的代碼,我從其他問題得到加密算法,它對我來說工作正常,但我也需要解密,我試圖模仿它像加密,但它的不工作。異常說最後一個塊不完整。解密算法在Android的AES
工作就像一個魅力。謝謝! – CodetrixStudio