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


網上看到兩種方法:

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"); 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM