Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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.io.IOException:使用BouncyCastle关闭流_Java_Exception_Stream_Bouncycastle - Fatal编程技术网

java.io.IOException:使用BouncyCastle关闭流

java.io.IOException:使用BouncyCastle关闭流,java,exception,stream,bouncycastle,Java,Exception,Stream,Bouncycastle,我正在尝试使用BouncyCastle使用PGPencryOption加密文件。 当我将加密数据格式化为AsciaMored时,解密也可以正常工作 但当我禁用AsciaMored时,它会生成大约12kb的文件[加密格式化]。 我试着用同样的代码来解密,现在它抛出了下面的错误 java.io.IOException: Stream closed at java.io.BufferedInputStream.getInIfOpen(Unknown Source) at java.io.Buffere

我正在尝试使用BouncyCastle使用PGPencryOption加密文件。 当我将加密数据格式化为AsciaMored时,解密也可以正常工作

但当我禁用AsciaMored时,它会生成大约12kb的文件[加密格式化]。 我试着用同样的代码来解密,现在它抛出了下面的错误

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.available(Unknown Source)
at org.bouncycastle.openpgp.PGPUtil$BufferedInputStreamExt.available(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.available(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.available(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.available(Unknown Source)
at java.io.FilterInputStream.available(Unknown Source)
at org.bouncycastle.crypto.io.CipherInputStream.nextChunk(Unknown Source)
at org.bouncycastle.crypto.io.CipherInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.openpgp.PGPEncryptedData$TruncatedStream.read(Unknown Source)
at java.io.InputStream.read(Unknown Source)
at org.bouncycastle.util.io.TeeInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.openpgp.PGPCompressedData$1.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
测试等级:

public void testDecrypt() throws Exception {
        this.pgpFileProcessor.setPassphrase(PASSPHRASE);
        this.pgpFileProcessor.setSecretKeyFileName(PRI_KEY_FILE);
        this.pgpFileProcessor.setAsciiArmored(false);

        this.pgpFileProcessor.setInputFileName(OUTPUT);
        this.pgpFileProcessor.setOutputFileName(TEMP_SG);
        InputStream is = this.pgpFileProcessor.decryptToStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }

        System.out.println(sb.toString());

        br.close();
        is.close();
    }
Util类

public InputStream decryptToStream() throws Exception {
    FileInputStream in = new FileInputStream(this.inputFileName);
    FileInputStream keyIn = new FileInputStream(this.secretKeyFileName);
    InputStream inputStream = this.pgpUtils.decryptFile(in, keyIn, this.passphrase.toCharArray());
    in.close();
    keyIn.close();
    return inputStream;
}
实际代码:

public InputStream decryptFile(InputStream in, InputStream keyIn, char[] passwd) throws Exception {
    InputStream decryptedData = null;

    Security.addProvider(new BouncyCastleProvider());
    in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
    PGPObjectFactory pgpF = new PGPObjectFactory(in);
    PGPEncryptedDataList enc;

    Object o = pgpF.nextObject();
    if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }

    Iterator<PGPPublicKeyEncryptedData> it = enc.getEncryptedDataObjects();
    PGPPrivateKey sKey = null;
    PGPPublicKeyEncryptedData pbe = null;

    while (sKey == null && it.hasNext()) {
        pbe = it.next();

        sKey = findPrivateKey(keyIn, pbe.getKeyID(), passwd);
    }

    if (sKey == null) {
        in.close();
        throw new IllegalArgumentException("Secret key for message not found.");
    }
    if (pbe == null) {
        in.close();
        throw new IllegalArgumentException("Encrypted Data is Malformed");
    }

    InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(sKey));

    PGPObjectFactory plainFact = new PGPObjectFactory(clear);

    Object message = plainFact.nextObject();

    if (message instanceof PGPCompressedData) {
        PGPCompressedData cData = (PGPCompressedData) message;
        PGPObjectFactory pgpFact = new PGPObjectFactory(cData.getDataStream());

        message = pgpFact.nextObject();
    }

    if (message instanceof PGPLiteralData) {
        PGPLiteralData ld = (PGPLiteralData) message;

        decryptedData = ld.getInputStream();
        /*
         * int ch; while ((ch = unc.read()) >= 0) { out.write(ch); }
         */

    } else if (message instanceof PGPOnePassSignatureList) {
        in.close();
        clear.close();
        throw new PGPException("Encrypted message contains a signed message - not literal data.");
    } else {
        in.close();
        clear.close();
        throw new PGPException("Message is not a simple encrypted file - type unknown.");
    }
    if (pbe.isIntegrityProtected()) {
        if (!pbe.verify()) {
            in.close();
            clear.close();
            throw new PGPException("Message failed integrity check");
        }
    }
    clear.close();
    in.close();
    return decryptedData;
}
公共InputStream解密文件(InputStream-in、InputStream-keyIn、char[]passwd)引发异常{
InputStream decryptedData=null;
addProvider(新的BouncyCastleProvider());
in=org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
PGPObjectFactory pgpF=新的PGPObjectFactory(in);
PGPencryptedatalist enc;
对象o=pgpF.nextObject();
if(o PGPEncryptedDataList的实例){
enc=(pGpencryptedatalist)o;
}否则{
enc=(pgpencryptedatalist)pgpF.nextObject();
}
迭代器it=enc.getEncryptedDataObjects();
PGPPrivateKey sKey=null;
PGPPublicKeyEncryptedData pbe=null;
while(sKey==null&&it.hasNext()){
pbe=it.next();
sKey=findPrivateKey(keyIn,pbe.getKeyID(),passwd);
}
if(sKey==null){
in.close();
抛出新的IllegalArgumentException(“未找到消息的密钥”);
}
如果(pbe==null){
in.close();
抛出新的IllegalArgumentException(“加密数据格式不正确”);
}
InputStream clear=pbe.getDataStream(新的BcPublicKeyDataDecryptorFactory(sKey));
PGPObjectFactory plainFact=新的PGPObjectFactory(清除);
对象消息=plainFact.nextObject();
if(PGPCompressedData的消息实例){
PGPCompressedData cData=(PGPCompressedData)消息;
PGPObjectFactory pgpFact=新的PGPObjectFactory(cData.getDataStream());
message=pgpFact.nextObject();
}
if(PGPLiteralData的消息实例){
PGPLiteralData ld=(PGPLiteralData)消息;
decryptedData=ld.getInputStream();
/*
*int ch;while((ch=unc.read())>=0){out.write(ch);}
*/
}else if(PGPOnePassSignatureList的消息实例){
in.close();
清除。关闭();
抛出新的PGPException(“加密消息包含签名消息-而不是文字数据”);
}否则{
in.close();
清除。关闭();
抛出新的PGPException(“消息不是简单的加密文件-类型未知”);
}
如果(pbe.isIntegrityProtected()){
如果(!pbe.verify()){
in.close();
清除。关闭();
抛出新的PGPException(“消息完整性检查失败”);
}
}
清除。关闭();
in.close();
返回解密数据;
}

您正在关闭
解密文件()最后一行中的流。

不要关闭那个里的小溪。通常规则是:在打开流的相同方法中关闭流。模式应如下所示:

InputStream in = null;
try {
    in = ...
    // use the stream here.
} finally {
    if (in != null) {
        in.close();
    }
}
或者,如果您使用的是java 7:

try (
    InputStream in = ...; // initialize stream here

) {
    // use the stream here.
}

您不必关闭初始化的流
try()
。它将自动关闭。享受java 7

您正在关闭
decryptFile()
最后一行中的流:

不要关闭那个里的小溪。通常规则是:在打开流的相同方法中关闭流。模式应如下所示:

InputStream in = null;
try {
    in = ...
    // use the stream here.
} finally {
    if (in != null) {
        in.close();
    }
}
或者,如果您使用的是java 7:

try (
    InputStream in = ...; // initialize stream here

) {
    // use the stream here.
}
您不必关闭初始化的流
try()
。它将自动关闭。享受java 7

Thnks 4关于java 7的信息(自动关闭)Thnks 4关于java 7的信息(自动关闭)