AES加解密问题javax.crypto.BadPaddingException: EVP_CipherFinal_ex at com.android.org.

weixin_44380702 2019-05-15 03:40:17
javax.crypto.BadPaddingException: EVP_CipherFinal_ex
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:430)


```
public class AESCoder {

private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
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;
}

public static byte[] encrypt(byte[] seed, byte[] plaintext)
throws Exception {
byte[] raw = getRawKey(seed);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(plaintext);
return encrypted;
}

public static byte[] decrypt(byte[] seed, byte[] ciphertext)
throws Exception {
byte[] raw = getRawKey(seed);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(ciphertext);

return decrypted;
}

}
...全文
790 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ueming 2019-09-24
  • 打赏
  • 举报
回复
检查一下,是否秘钥错了
weixin_44380702 2019-05-15
  • 打赏
  • 举报
回复
大神们,有没有什么解决办法

58,441

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧