在用充气城堡解密后,为什么要添加这些方形符号? [英] Why are these square symbols appended after decrypting with bouncy castle?

查看:97
本文介绍了在用充气城堡解密后,为什么要添加这些方形符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的java方法,该方法使用有弹性的城堡库对文本进行加密和解密。加密按预期工作,但是当我解密某些内容时,最后得到以下多余的方形符号:

I have created a simple java method that encrypts and decrypts text using the bouncy castle library. Encryption works as expected but when I decrypt something I get these extra square symbols at the end:


我认为这可能与填充有关,但是我一直遵循示例出现在Bouncy Castle的网站上,所以我真的不明白为什么会得到这种输出。这是我正在使用的代码:

I think this might be something to do with padding but I've followed the example featured on bouncy castle's website, so I really can't understand why I would be getting this sort of output. Here is the code I am using:

[Main]

public static void main(String[] argv) {
    String ciphertext = "PlJR5pzbowsuzHIc9iTKHg==";
    String decrypted;
    CryptoCodec codec = new CryptoCodec();

    decrypted = codec.exec("AES", "xxxxooooxxxxoooo", ciphertext, false);

    System.out.println("Ciphertext: " + ciphertext);
    System.out.println("Decrypted: " + decrypted);
}

[CryptoCodec]

[CryptoCodec]

// Eod: (true) Encrypt or (false) decrypt.
public String exec(String algorithm, String key, String data, boolean eod) {

    // Using AESEngine();
    BlockCipher engine = CipherEngine.getBlockCipher(algorithm);

    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));

    byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
    byte[] dataBytes;
    if(eod) {
        dataBytes = data.getBytes(StandardCharsets.UTF_8);
    } else {
        dataBytes = Base64.decode(data);
    }

    cipher.init(eod, new KeyParameter(keyBytes));

    byte[] outputText = new byte[cipher.getOutputSize(dataBytes.length)];
    int outputTextLen = cipher.processBytes(dataBytes, 0, dataBytes.length, outputText, 0);

    try {
        cipher.doFinal(outputText, outputTextLen);
    } catch (CryptoException err) {
        err.printStackTrace();
    }

    if(eod) {
        return new String(Base64.encode(outputText));
    } else {
        return new String(outputText);
    }
}

请记住,我仍在学习加密技术,很想听听关于发生这种情况的任何解释。

Please keep in mind I am still learning about cryptography and would love to hear any sort of explanation to why this may be happening. Thanks in advance.

推荐答案

解密期间 cipher.getOutputSize(dataBytes.length)不知道将从填充中删除多少个字节(甚至不知道您要告诉它数据的最后一部分)。因此,它告诉您可能的最大值。

During decryption cipher.getOutputSize(dataBytes.length) doesn't know how many bytes it will remove from padding (it doesn't even know that you're telling it about the last part of the data). So it tells you the maximum it could be.

因此,您的目标数组大于所需的数组,并且您需要考虑如何

Your destination array is therefore larger than it needs to be, and you need to respect how much data got filled in.

您怎么知道填写了多少数据?从 doFinal 捕获返回值。那你怎么办呢?告诉String构造函数何时停止读取。

How do you know how much got filled in? Capture the return value from doFinal. What do you do with it then? Tell the String constructor when to stop reading.

然后,您最终得到类似

try {
    outputTextLen += cipher.doFinal(outputText, outputTextLen);
} catch (CryptoException err) {
    err.printStackTrace();
}

if(eod) {
    return new String(Base64.encode(outputText));
} else {
    return new String(outputText, 0, outputTextLen);
}

这还修复了以下错误:如果您现在加密16个字节的数据,无法成功解密。

Which also fixes your bug that if you encrypt 16 bytes of data right now you won't decrypt successfully.

这篇关于在用充气城堡解密后,为什么要添加这些方形符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆