BouncyCastle 最后字节解密问题

发布于 2024-07-29 03:56:54 字数 1613 浏览 1 评论 0原文

我正在使用 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)

I am decrypting an XML file from the file system using Bouncy Castle. I output the decrypted text and get a fatal error SAXParseException on the very last byte of data. Below is my decryption method and the setup of the cipher object.

I was initially using cipher streams, and everything worked perfect (commented out code was my stream). Due to policy files and end users not having the 256 bit unlimited versions I need to use bouncy castle.

Any ideas why the final byte is not coming through?

From Constructor:

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

Decrypt Method:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-08-05 03:56:54

你打电话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();
    }
}

Do you call doFinal() with the final chunk of data?

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文