考虑以下测试:

    [Test]
    public void TestAes256EcbPkcs7Stream()
    {
        // 504 bytes of plain text
        const string inputString = new string('z', 504);
        var inputBytes = Encoding.UTF8.GetBytes(inputString);
        byte[] key = {
            0, 0, 0, 0, 0, 0, 0, 0,
            1, 0, 0, 0, 0, 0, 0, 0,
            2, 0, 0, 0, 0, 0, 0, 0,
            3, 0, 0, 0, 0, 0, 0, 0
        };
        var rij = new RijndaelManaged
        {
            BlockSize = 256, // 256 bits == 32 bytes
            Key = key,
            IV = key, // just for test
            Mode = CipherMode.ECB,
            Padding = PaddingMode.PKCS7
        };
        var enc = rij.CreateEncryptor();
        var encBytes = enc.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
        Assert.AreEqual(512, encBytes.Length);
        var dec = rij.CreateDecryptor();
        byte[] decBytes = new byte[inputBytes.Length];
        int decPos = 0;
        using (var cipherMs = new MemoryStream(encBytes))
        {
            var buf = new byte[32];
            // process all blocks except the last one
            while (cipherMs.Read(buf, 0, buf.Length)==buf.Length &&
                cipherMs.Length!=cipherMs.Position)
            {
                for (int w = 0; w!=buf.Length;)
                {
                    w += dec.TransformBlock(buf, 0, buf.Length, decBytes, decPos);
                    decPos += w;
                }
            }
            // ensure that we read all blocks
            Assert.IsTrue(cipherMs.Length==cipherMs.Position);
            // process the last block
            var tailBytes = dec.TransformFinalBlock(buf, 0, buf.Length);
            // here decPos==480, that means 480 bytes were written to decBytes
            // and 504-480 = 24 bytes come from TransformFinalBlock
            Assert.AreEqual(24, tailBytes.Length); // <- fail, because the actual length is 56
            Buffer.BlockCopy(tailBytes, 0, decBytes, decPos, tailBytes.Length);
        }
        Assert.AreEqual(inputBytes, decBytes);
    }


由于某种原因,我得到了56字节的最终块,而不是24字节。
我想TransformBlock / TransformFinalBlock应该以其他方式使用,但是不幸的是,MSDN文档对这些方法的解释不多。
有什么想法吗?

最佳答案

好的,这是东西:
首次调用TransformBlock时,它将输入缓冲区的最后一块复制到所谓的depad缓冲区,然后转换并将剩余的块写入输出缓冲区。在下一次调用期间,它将数据转换为来自depad缓冲区的数据,将其写入输出缓冲区,将输入缓冲区的最后一个块再次复制到depad缓冲区,然后将转换后的剩余块写入到输出中,就像第一次一样。

TL; DR:TransformBlock缓存输入数据的最后一块,因此在调用TransformFinalBlock时,它可以抓住最后一块并删除填充。如果不进行缓存,则最后一个块可以由TransformBlock处理。在这种情况下,不会删除填充。

关于c# - 解密AES密文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32426898/

10-13 07:45