java - BouncyCaSTLe 最后一个字节解密问题

标签 java bouncycastle encryption

我正在使用 Bouncy CaSTLe 从文件系统解密 XML 文件。我输出解密的文本,并在数据的最后一个字节上收到 fatal error 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)

最佳答案

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

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();
    }
}

关于java - BouncyCaSTLe 最后一个字节解密问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1188732/

相关文章:

java - Spring集成:@Header参数的可配置名称?

java - 使用 BouncycaSTLe 生成的 CSR 缺少公钥和属性

C#序列化一个没有无参数构造函数的类

java - 导出/导入加密代码 Java

encryption - iPhone 应用程序中的 base64 是否被视为加密?

c - C中三元运算符的逻辑?

java - 为什么我的 Java 代码无法正确执行 bash 命令?

java - 如何使用 AffineTransform 缩放 Swing 组件?

java - Servlet 无法从数据库中提取值

java - 从未压缩的客户端临时公钥和 Open SSL 静态服务器私钥生成 ECPublicKey/ECPrivateKey 期间出错