1. 程式人生 > >android 上AES解密是報錯javax.crypto.BadPaddingException: pad block corrupted

android 上AES解密是報錯javax.crypto.BadPaddingException: pad block corrupted

corrupted init block dom see roi and sha1 pad

網上看到兩種方法:

1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES");

private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);

kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}

紅色的部分為註意項,不能寫為SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

2.Cipher cipher = Cipher.getInstance("AES"); ---------4.3以上有bug

修改為

Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");

android 上AES解密是報錯javax.crypto.BadPaddingException: pad block corrupted