Java 解密CipherInputStream将导致空流

Java 解密CipherInputStream将导致空流,java,encryption,cryptography,jce,Java,Encryption,Cryptography,Jce,我似乎对JCE有意见。我使用一个加密JCE密码创建一个CipherInputStream,然后使用另一个解密JCE密码创建另一个CipherInputStream 当我尝试读取第二个流时,我得到的只是空数据。我没有发现禁止上述行为的文件。有人知道问题出在哪里吗 这就是我正在运行的代码,最后明文是空的(无论我使用什么SecurityProvider 谢谢 在省略键和IV参数的部分时,根据提供的代码查找一个小片段 // add proper exception handling, left out

我似乎对JCE有意见。我使用一个加密JCE密码创建一个
CipherInputStream
,然后使用另一个解密JCE密码创建另一个
CipherInputStream

当我尝试读取第二个流时,我得到的只是空数据。我没有发现禁止上述行为的文件。有人知道问题出在哪里吗

这就是我正在运行的代码,最后明文是空的(无论我使用什么
SecurityProvider


谢谢

在省略键和IV参数的部分时,根据提供的代码查找一个小片段

// add proper exception handling, left out only for the example
public static void main(String[] args) throws Exception {
    String transformation = "AES/CBC/PKCS5PADDING";
    String provider = "SunJCE";
    String algorithm = "AES";
    byte[] payloadArray = "secret text".getBytes();

    KeyGenerator keyGen = KeyGenerator.getInstance(algorithm, provider);
    keyGen.init(128);
    Key key = keyGen.generateKey();

    byte[] ivBytes = new byte[16];
    SecureRandom prng = new SecureRandom();
    prng.nextBytes(ivBytes);
    IvParameterSpec IV = new IvParameterSpec(ivBytes);

    Cipher encryptCipher = Cipher.getInstance(transformation, provider);
    encryptCipher.init(Cipher.ENCRYPT_MODE, key, IV);

    InputStream payload = new ByteArrayInputStream(payloadArray);
    InputStream encryptStream = new CipherInputStream(payload, encryptCipher);

    Cipher decryptCipher = Cipher.getInstance(transformation, provider);
    decryptCipher.init(Cipher.DECRYPT_MODE, key, IV);
    InputStream decryptStream = new CipherInputStream(encryptStream, decryptCipher);

    for (int i = decryptStream.read(); i >= 0; i = decryptStream.read()) {
        System.out.print((char) i);
    }
}
输出

secret text
secret text