使用BouncyCastle解密AES-CBC-PKCS7时,多余的空字符

时间:2019-05-14 13:21:59

标签: c# encryption aes bouncycastle

我需要在2个不同的项目中实施AES加密,但是一个必须使用.NET标准加密库,另一个必须使用BouncyCastle。两者都是C#代码。相关方法如下:

.NET:

internal class NETAesCryptor : IAesCryptor
{
    public Tuple<byte[], byte[]> Encrypt(string plaintext, byte[] key)
    {
        byte[] ciphertext, iv;
        using (var aes_provider = new AesCryptoServiceProvider())
        {
            aes_provider.Padding = PaddingMode.PKCS7;
            aes_provider.GenerateIV();
            iv = aes_provider.IV;
            var encryptor = aes_provider.CreateEncryptor(key, iv);
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                {
                    using (var sw = new StreamWriter(cs))
                    {
                        sw.Write(plaintext);
                    }
                    ciphertext = ms.ToArray();
                }
            }
        }
        var result = new Tuple<byte[], byte[](ciphertext, iv);
        return result;
    }

    public string Decrypt(byte[] ciphertext, byte[] iv, byte[] key)
    {
        string plaintext;
        using (var aes_provider = new AesCryptoServiceProvider())
        {
            aes_provider.Padding = PaddingMode.PKCS7;
            aes_provider.IV = iv;
            var decryptor = aes_provider.CreateDecryptor(key, iv);
            using (var ms = new MemoryStream(ciphertext))
            {
                using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                {
                    using (var sr = new StreamReader(cs))
                    {
                        plaintext = sr.ReadToEnd();
                    }
                }
            }
        }
        return plaintext;
    }
}

丰盛的城堡:

internal class BCAesCryptor : IAesCryptor
{
    private SecureRandom _r;

    public BCAesCryptor()
    {
        _r = new SecureRandom();
    }

    public Tuple<byte[], byte[]> Encrypt(string plaintext, byte[] key)
    {
        var plaintext_bytes = Encoding.UTF8.GetBytes(plaintext);
        var iv = GenerateRandomBytes(16);

        var engine = new AesEngine();
        var cbc_cipher = new CbcBlockCipher(engine);
        var cipher = new PaddedBufferedBlockCipher(cbc_cipher, new Pkcs7Padding());
        var key_param = new KeyParameter(key);
        var key_param_with_iv = new ParametersWithIV(key_param, iv);

        cipher.Init(true, key_param_with_iv);
        var ciphertext = new byte[cipher.GetOutputSize(plaintext_bytes.Length)];
        var length = cipher.ProcessBytes(plaintext_bytes, ciphertext, 0);
        cipher.DoFinal(ciphertext, length);

        var result = new Tuple<byte[], byte[]>(ciphertext, iv);
        return result;
    }

    public string Decrypt(byte[] ciphertext, byte[] iv, byte[] key)
    {
        var engine = new AesEngine();
        var cbc_cipher = new CbcBlockCipher(engine);
        var cipher = new PaddedBufferedBlockCipher(cbc_cipher, new Pkcs7Padding());
        var key_param = new KeyParameter(key);
        var key_param_with_iv = new ParametersWithIV(key_param, iv);

        cipher.Init(false, key_param_with_iv);
        var plaintext = new byte[cipher.GetOutputSize(ciphertext.Length)];
        var length = cipher.ProcessBytes(ciphertext, plaintext, 0);
        cipher.DoFinal(plaintext, length);

        var result = Encoding.UTF8.GetString(plaintext);
        return result;
    }

    private byte[] GenerateRandomBytes(int length = 16)
    {
        var result = new byte[length];
        _r.NextBytes(result);
        return result;
    }
}

.NET方法之间的加密/解密正常,而Bouncycastle加密/.NET解密也正常。但是由于某种原因,Bouncycastle解密在明文末尾添加了数量可变的\0个字符,我不知道为什么会这样。

我正在使用的测试代码:

[TestClass]
public class AesCryptorTests
{
    private byte[] _key;
    private string _plaintext;

    public AesCryptorTests()
    {
        _key = GenerateRandomBytes();
        _plaintext = "Lorem ipsum dolor sit amet";
    }

    [TestMethod]
    public void TestMethod2()
    {
        var bc = new BCAesCryptor();
        var net = new NETAesCryptor();
        var result = net.Encrypt(_plaintext, _key);
        var new_plaintext = bc.Decrypt(result.Ciphertext, result.IV, _key);
        Assert.AreEqual(_plaintext, new_plaintext);
    }

    private byte[] GenerateRandomBytes(int cantidad = 16)
    {
        var result = new byte[cantidad];
        using (var r = new RNGCryptoServiceProvider())
        {
            r.GetBytes(result);
        }
        return result;
    }
}

在先前的测试中,解密返回Lorem ipsum dolor sit amet\0\0\0\0\0\0而不是明文。

任何建议/评论将不胜感激。

2 个答案:

答案 0 :(得分:3)

充气城堡只能在调用GetOutputSize期间预先猜测明文消息的输出大小。它不知道使用了多少填充字节,因为这些填充字节仅在解密后可用。因此,他们必须对密文进行部分解密才能知道填充量,而这又太过分了。因此,您只能从高端进行估算,以便最大字节数仍可以容纳在新创建的缓冲区中。

您将需要ProcessBytesDoFinal的返回值来查看从密文解密的实际字节数(在输入缓冲区内部缓冲区)何时调用方法。 DoFinal解密最后一个块,然后从最后一个块中删除填充,因此只有那时(剩余)明文的大小才知道。

由于纯文本大小小于GetOutputSize返回的值,因此您当前看到的零值字节就是缓冲区的未使用字节。


当然,这全都隐藏在.NET示例的流代码中,其中ReadToEnd是进行某些高级缓冲(可能在内部使用MemoryStream所必需的)。

答案 1 :(得分:2)

instructions之后,Maarten Bodewes的最终工作代码如下:

public string Decrypt(byte[] ciphertext, byte[] iv, byte[] key)
{
    var engine = new AesEngine();
    var cbc_cipher = new CbcBlockCipher(engine);
    var cipher = new PaddedBufferedBlockCipher(cbc_cipher, new Pkcs7Padding());
    var key_param = new KeyParameter(key);
    var key_param_with_iv = new ParametersWithIV(key_param, iv);

    cipher.Init(false, key_param_with_iv);
    var decryption_buffer = new byte[cipher.GetOutputSize(ciphertext.Length)];
    var initial_length = cipher.ProcessBytes(ciphertext, decryption_buffer, 0);
    var last_bytes = cipher.DoFinal(decryption_buffer, initial_length);
    var total_bytes = initial_length + last_bytes;

    var plaintext = new byte[total_bytes];
    Array.Copy(decryption_buffer, plaintext, total_bytes);
    var result = Encoding.UTF8.GetString(plaintext);
    return result;
}

请注意,现在使用解密方法的整数输出来计算纯文本的长度,并且简单的数组副本就可以创建无多余字符的纯文本。