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 (null == key || key.length() == 0) {
            throw new NullPointerException("key not is null");
        }
        SecretKeySpec key2 = null;try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            key2 = new SecretKeySpec(enCodeFormat, "AES");
        } catch (NoSuchAlgorithmException ex) {
            throw new NoSuchAlgorithmException();
        }
        return key2;

    }

主要是红色部分的问题

修改后代码:

private Key initKeyForAES(String key) throws NoSuchAlgorithmException {
        if (null == key || key.length() == 0) {
            throw new NullPointerException("key not is null");
        }
        SecretKeySpec key2 = null;
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(key.getBytes());
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            key2 = new SecretKeySpec(enCodeFormat, "AES");
        } catch (NoSuchAlgorithmException ex) {
            throw new NoSuchAlgorithmException();
        }
        return key2;

    }

其实就是SecureRandom创建的方式不同而引起的错误,具体原理我也不懂,因为加密解密代码都是网上搜的,具体没研究过这个。总之能解决问题就好。

我是从这找到解决方案的:http://wenku.baidu.com/link?url=wOibKHENi2Z5gFOL5prjGBE8RES1dZEZlrvfY1NTl89QJWtTwXUNLmgEXVYWGBGXR25oRvOKPJTI5M3o95KW0yIHwgFVEnJiZt1-0YvRQua

时间: 2024-07-28 21:15:32

javax.crypto.BadPaddingException: Given final block not properly padded解决方案的相关文章

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

Java 之 Given final block not properly padded

获取Cipher对象的时候一定要写成 Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 不要写成 Cipher cipher = Cipher.getInstance("DES"); 否则解密的时候会报错: Given final block not properly padded 原因是Cipher cipher = Cipher.getInstance("DES");与Ciphe

AES解密异常Given final block not properly padded-在线助手博客

AES 128/192/256位CBC/CFB/ECB/OFB/PCBC 在线加密解密 解密内容:1243CFEBD819AA6B1C717DE870459F7B 秘钥:http://www.it399.com 没有使用填充向量iv AES解密异常Given final block not properly padded javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypt

左右 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

很多人 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.getByte

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

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

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