android端调用AES解密报错

isaactalx 2016-05-21 01:23:12
将java后台加密的文件在android端进行解密,下面是后台拿过来的代码,我照网上的改了下,只能在手机上加解密,但是后台加密的仿手机上报了javax.crypto.BadPaddingException的错
public class AESCoder {

private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//
// SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
//这里针对android要进行修改
SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
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;
}

}
...全文
180 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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