关于 android AES 部分机器 javax.crypto.BadPaddingException: pad block corrupted

很多人 android 上面使用 AES 出现 javax.crypto.BadPaddingException: pad block corrupted 下面贴出没有问题的代码,自己对照,不做解释!

public static class cryptogram{

public static String encrypt(String seed, String cleartext) throws Exception {

byte[] rawKey = getRawKey(seed.getBytes());

byte[] result = encrypt(rawKey, cleartext.getBytes());

return toHex(result);

}

public static String decrypt(String seed, String encrypted) throws Exception {

byte[] rawKey = getRawKey(seed.getBytes());

byte[] enc = toByte(encrypted);

byte[] result = decrypt(rawKey, enc);

return new String(result);

}

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;

}

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec,new IvParameterSpec(new byte[cipher.getBlockSize()]));

byte[] encrypted = cipher.doFinal(clear);

return encrypted;

}

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, skeySpec,new IvParameterSpec(new byte[cipher.getBlockSize()]));

byte[] decrypted = cipher.doFinal(encrypted);

return decrypted;

}

private static String toHex(String txt) {

return toHex(txt.getBytes());

}

private static String fromHex(String hex) {

return new String(toByte(hex));

}

private static byte[] toByte(String hexString) {

int len = hexString.length()/2;

byte[] result = new byte[len];

for (int i = 0; i < len; i++)

result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();

return result;

}

private static String toHex(byte[] buf) {

if (buf == null)

return "";

StringBuffer result = new StringBuffer(2*buf.length);

for (int i = 0; i < buf.length; i++) {

appendHex(result, buf[i]);

}

return result.toString();

}

private final static String HEX = "0123456789ABCDEF";

private static void appendHex(StringBuffer sb, byte b) {

sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));

}

}

关于 android AES 部分机器 javax.crypto.BadPaddingException: pad block corrupted

时间: 2024-10-08 07:57:28

关于 android AES 部分机器 javax.crypto.BadPaddingException: pad block corrupted的相关文章

左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted

好多人 android 使用上述 AES 显现 javax.crypto.BadPaddingException: pad block corrupted 下面的代码发布没问题,比较自己.不解释! public static class cryptogram{ public static String encrypt(String seed, String cleartext) throws Exception { byte[] rawKey = getRawKey(seed.getBytes(

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.getInsta

关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法

关于javax.crypto.BadPaddingException: Blocktype异常的几种解决办法 转载请注明出处 1.异常描述:最近做项目为了增强数据传输的安全性用到了RSA加密.即android客户端将要传送的信息,用私钥通过RSA非对称加密算法加密后,传到服务器端(PC端).服务器端用对应(密钥)的公钥来解密时解密失败,抛出"javax.crypto.BadPaddingException: Blocktype"异常. 2.异常原因:Android系统使用的虚拟机(da

javax.crypto.BadPaddingException: Given final block not properly padded解决方案

JAVA的AES加密解密在windows上测试一切正常,上传到空间上在解密时就出现错误.空间是Linux系统 查看日志发现出现此异常 javax.crypto.BadPaddingException: Given final block not properly padded 后面百度了一下终于解决了,在生成key的时候出现错误的 原来的代码: private Key initKeyForAES(String key) throws NoSuchAlgorithmException { if (

javax.crypto.BadPaddingException: Given final block not properly padded

异常如下 1.javax.crypto.BadPaddingException: Given final block not properly padded 1)要确认下是否加密和解密都是使用相同的填充算法(也就是说,是否都是使用PKCS5Padding)2)确认下你要解密的字节数组是否正确. javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded

javax.crypto.BadPaddingException: Given final block not properly padded 解决方法

下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.DESCipher.engin

记一次文件编码出现的BUG javax.crypto.BadPaddingException: Given final block not properly padded

1.前景:工作中需要实现一个功能,导出的数据需要加密,不能被明文看到,使用DES加密,对byte数组加密解密操作代码如下 public class DESTool { static String transformation = "DESede/ECB/PKCS5Padding"; static String algorithm = "DESede"; public byte[] decode(byte[] srcByte, byte[] keyByte, int

android 开发解密是出现pad block corrupted 错误

情景:在虚拟机上运行正常的,但是到我的真机上就解密失败,出现pad block corrupted  ,据说是版本原因:我机器是小米3 最新版的android  4.2 出现问题的代码: private static byte[] getRawKey(byte[] seed) throws Exception {        KeyGenerator kgen = KeyGenerator.getInstance("AES");        //SecureRandom sr =

Base64加密解密不一致导致AES加密报出javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher

报错的意思的是使用该种解密方式出入长度应为16bit的倍数,但实际的错误却不是这个,错误原因根本上是因为在http请求是特殊字符编码错误,具体数就是base64生成的+号,服务器接收时成了空格,然后导致base64解码出的字节数组有改变. 下面来还原并分析一下具体原因: 请求代码: @Test public void testHttp() throws Exception { //16进制字节数组,单纯的为了生成含有+的base64位字符串,是在试了好多遍都没试到+,只能把我错误例子的byte数