網上很多code運行時會出現下面這個錯誤:
javax.crypto.BadPaddingException: pad block corrupted
多數是傳入的key有問題,不妨試試下面的code。
以下代碼在android L50、M60、N70、N71測試通過。採用AES + BASE64雙重加密。
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by williamvon on 17-5-9.
*/
public class AESUtils {
private static final String TAG = "AESUtils";
// CBC(Cipher Block Chaining, 加密快鏈)模式,PKCS7Padding補碼方式
// AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
// AES 加密
private static final String AES = "AES";
// 密鑰偏移量
private static final String mstrIvParameter = "1234567890123456";
/* key必須爲16位,可更改爲自己的key */
//String mstrTestKey = "1234567890123456";
// 加密
public static String encrypt(String strKey, String strClearText) throws Exception {
Log.d(TAG, "### begin encrypt: ");
Log.d(TAG, "key = " + strKey + ",ClearText: " + strClearText);
if (TextUtils.isEmpty(strClearText)) {
Log.e(TAG, "clear text is empty.");
return null;
}
if (null == strKey) {
Log.e(TAG, "encrypt KEY is null.");
return null;
}
// check the KEY is 16 or not
if (16 != strKey.length()) {
Log.e(TAG, "encrypt KEY length is 16.");
return null;
}
try {
byte[] raw = strKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] cipherText = cipher.doFinal(strClearText.getBytes());
Log.d(TAG, "encrypt result(not BASE64): " + cipherText.toString());
String strBase64Content = Base64.encodeToString(cipherText, Base64.DEFAULT); // encode it by BASE64 again
Log.d(TAG, "encrypt result(BASE64): " + strBase64Content);
return strBase64Content;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 解密
public static String decrypt(String strKey, String strCipherText) throws Exception {
Log.d(TAG, "### begin decrypt: ");
Log.d(TAG, "key = " + strKey + ",CipherText: " + strCipherText);
if (TextUtils.isEmpty(strCipherText)) {
Log.e(TAG, "cipher text is empty.");
return null;
}
if (null == strKey) {
Log.e(TAG, "decrypt KEY is null.");
return null;
}
// check the KEY is 16 or not
if (16 != strKey.length()) {
Log.e(TAG, "decrypt KEY length is 16.");
return null;
}
try {
byte[] raw = strKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
IvParameterSpec iv = new IvParameterSpec(mstrIvParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] cipherText = Base64.decode(strCipherText, Base64.DEFAULT); // decode by BASE64 first
Log.d(TAG, "BASE64 decode result(): " + cipherText.toString());
byte[] clearText = cipher.doFinal(cipherText);
String strClearText = new String(clearText);
Log.d(TAG, "decrypt result: " + strClearText);
return strClearText;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
更多內容讀者可以參考以下文章: