Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解密加密流时出现BadPaddingException_Java_Aes_Javax.crypto - Fatal编程技术网

Java 解密加密流时出现BadPaddingException

Java 解密加密流时出现BadPaddingException,java,aes,javax.crypto,Java,Aes,Javax.crypto,我正在使用javax.crypto.Cipher加密一个文件,然后解密它,但是我仍然得到了BadPaddingException。下面的类从输入文件接收inputStream,并为输出文件接收outputStream 错误 13:22:15,049 ERROR [STDERR] javax.crypto.BadPaddingException: Given final block not properly padded 13:22:15,081 ERROR [STDERR] at com

我正在使用
javax.crypto.Cipher
加密一个文件,然后解密它,但是我仍然得到了BadPaddingException。下面的类从输入文件接收
inputStream
,并为输出文件接收
outputStream

错误

13:22:15,049 ERROR [STDERR] javax.crypto.BadPaddingException: Given final block not properly padded
13:22:15,081 ERROR [STDERR]     at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
13:22:15,096 ERROR [STDERR]     at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
13:22:15,128 ERROR [STDERR]     at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
13:22:15,143 ERROR [STDERR]     at javax.crypto.Cipher.doFinal(DashoA13*..)
code

public class CipherAES implements Cipher {

    private static final Logger logger = Logger.getLogger(CipherAES.class);

    private Key key;

    public CipherAES() {
        this.key = generateKey();
    }

    private Key generateKey() {
        try {
            KeyGenerator generator;
            generator = KeyGenerator.getInstance("AES");
            generator.init(new SecureRandom());
            return generator.generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void decrypt(InputStream inputStream, OutputStream outputStream) throws IOException {
        try {
            javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES");
            cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);
            byte[] raw = IOUtil.toByteArray(inputStream);
            byte[] base64Decoded = Base64.decodeBase64(raw);
            byte[] decryptedData = cipher.doFinal(base64Decoded);
            outputStream.write(decryptedData);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
            outputStream.close();
        }
    }

    @Override
    public void encrypt(InputStream inputStream, OutputStream outputStream) throws IOException {
        try {
            javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES");
            cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
            byte[] raw = IOUtil.toByteArray(inputStream);
            byte[] encryptedData = cipher.doFinal(raw);
            byte[] base64Encoded = Base64.encodeBase64(encryptedData);
            outputStream.write(base64Encoded);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
            outputStream.close();
        }
    }

}
LOG

# this is log from encryption
raw: 
    [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
encrypted: 
    [92, -104, -48, -10, 95, -3, -21, 46, 27, -60, -115, 85, 114, -95, -126, 108, 88, -105, 94, -84, 86, 86, -75, -83, -73, 24, -109, 86, -34, 83, -35, 106]
base64Encoded: 
    [88, 74, 106, 81, 57, 108, 47, 57, 54, 121, 52, 98, 120, 73, 49, 86, 99, 113, 71, 67, 98, 70, 105, 88, 88, 113, 120, 87, 86, 114, 87, 116, 116, 120, 105, 84, 86, 116, 53, 84, 51, 87, 111, 61]

# this is from decryption
raw (this is the base64Encoded): 
    [88, 74, 106, 81, 57, 108, 47, 57, 54, 121, 52, 98, 120, 73, 49, 86, 99, 113, 71, 67, 98, 70, 105, 88, 88, 113, 120, 87, 86, 114, 87, 116, 116, 120, 105, 84, 86, 116, 53, 84, 51, 87, 111, 61]
base64Decoded (this is the encrypted): 
    [92, -104, -48, -10, 95, -3, -21, 46, 27, -60, -115, 85, 114, -95, -126, 108, 88, -105, 94, -84, 86, 86, -75, -83, -73, 24, -109, 86, -34, 83, -35, 106]
decrypted (this should be the raw from the encryption): 
    I don't know - the exception is thrown 

首先,您的加密代码似乎有些古怪:

byte[] raw = IOUtil.toByteArray(inputStream);
byte[] encryptedData = cipher.doFinal();
密码
它要加密的数据怎么样?我怀疑你是想把
raw
传给
doFinal
呼叫

我怀疑这是一个完整的问题,但这至少是一个起点


为后代编辑:根据评论判断,问题在于加密和解密使用了不同的实例,因此密钥不同。

对加密流进行解密时出现的Badpadding异常主要是由于以错误的格式保存加密数据

有关详细信息,请查看


它提到了哪个代码出错以及如何解决。

@Tom:您是否检查过是否已将正确的数据返回到解密方法?例如,检查
encryptedData
base64 decoded
的长度@Tom:您正在使用同一实例进行加密和解密,对吗?(否则很明显你会有不同的钥匙。)我一直这么认为,但是。。。我刚刚用一个简短但完整的程序对它进行了测试,结果很好。添加一个重现这个问题的主方法,让我们能够测试它。