4

我正在使用 Bouncy Castle 从文件系统中解密 XML 文件。我输出解密后的文本并在最后一个数据字节上得到一个致命错误 SAXParseException。下面是我的解密方法和密码对象的设置。

我最初使用密码流,一切正常(注释掉的代码是我的流)。由于策略文件和最终用户没有 256 位无限版本,我需要使用充气城堡。

任何想法为什么最后一个字节没有通过?

从构造函数:

keyParam = new KeyParameter(key);
engine = new AESEngine();
paddedBufferedBlockCipher = 
    new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));

解密方法:

public void decrypt(InputStream in, OutputStream out) {
    try
    {
        paddedBufferedBlockCipher.init(false, 
            new ParametersWithIV(keyParam, _defaultIv));
//          cipher.init(Cipher.DECRYPT_MODE, secretKey, ivs);
//          CipherInputStream cipherInputStream 
//                      = new CipherInputStream(in, cipher);

        byte[] buffer = new byte[4096];
        byte[] outBuffer = new byte[4096];

        for (int count = 0; (count = in.read(buffer)) != -1;) {
            paddedBufferedBlockCipher.processBytes(buffer, 0, 
                count, outBuffer, 0);
            out.write(outBuffer, 0, count);         
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

[Fatal Error] :40:23: Element type "Publi" must be followed by either attribute specifications, ">" or "/>".
org.xml.sax.SAXParseException: Element type "Publi" must be followed by either attribute specifications, ">" or "/>".
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:264)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
4

1 回答 1

3

你用最后的数据块调用doFinal () 吗?

public void decrypt(InputStream in, OutputStream out) {
    try
    {
        paddedBufferedBlockCipher.init(false, 
            new ParametersWithIV(keyParam, _defaultIv));
        byte[] buffer = new byte[4096];
        byte[] outBuffer = new byte[4096];

        for (int count = 0; (count = in.read(buffer)) != -1;) {
            int c2 = paddedBufferedBlockCipher.processBytes(buffer, 0, 
                count, outBuffer, 0);
            out.write(outBuffer, 0, c2);                     
        }
        count = paddedBufferedBlockCipher.doFinal(outBuffer, 0);
        out.write(outBuffer, 0, count);                     
   }
    catch(Exception e) {
        e.printStackTrace();
    }
}
于 2009-07-27T15:29:01.937 回答