简体   繁体   English

C# 中的 AES-ECB 块解密

[英]AES-ECB block-decryption in C#

I wrote code that decrypts the input string completely.我编写了完全解密输入字符串的代码。 But since this is an ECB mode, I wanted to somehow decrypt not the entire input text, but only a separate block of it.但由于这是一种 ECB 模式,我想以某种方式不解密整个输入文本,而只解密其中的一个单独块。

As far as I understand, ECB AES encrypts in blocks of 8 bytes.据我了解,ECB AES 以 8 个字节的块进行加密。 How can I add the AES_Decrypt function to it so that it decrypts only the last 8 bytes of the input string, for example.例如,如何向其中添加AES_Decrypt函数,以便它仅解密输入字符串的最后 8 个字节。

    byte[] bytesToBeDecrypted = new byte[32];

    byte[] 8_bytesToBeDecrypted = new byte[8];  // Only 8 bytes of bytesToBeDecrypted

    public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
        {
            byte[] decryptedBytes = null;

            string salt = "12345678";
            Encoding unicode = Encoding.Unicode;
            byte[] saltBytes = unicode.GetBytes(salt);

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.KeySize = 256;
                    AES.BlockSize = 128;
                    AES.Padding = PaddingMode.Zeros;

                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 65535);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);

                    AES.Mode = CipherMode.ECB;

                    using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                        cs.Close();
                    }
                    decryptedBytes = ms.ToArray();
                }
            }

            return decryptedBytes;
        }

You can likely just Seek the MemoryStream with a multiple of 16 bytes from the beginning (or apparently even 16 bytes from the last part of the stream) and then connect the CryptoStream to decrypt whatever is left.您可能只是从头开始使用 16 个字节的倍数(或者显然甚至从流的最后一部分开始Seek 16 个字节)来Seek MemoryStream然后连接CryptoStream以解密剩下的任何内容。 With CBC that would be a bit more tricky as you would have to set the IV to the previous ciphertext block, but with ECB this should be a breeze.使用 CBC 会有点棘手,因为您必须将 IV 设置为前一个密文块,但使用 ECB 这应该是轻而易举的。


Notes:笔记:

  • Of course, you don't need to set the IV for the ECB mode, that should not be required.当然,您不需要为 ECB 模式设置 IV,这应该不是必需的。
  • Rijndael with a block size of 128 is actually AES.块大小为 128 的 Rijndael 实际上是 AES。 C#/.NET only supports a subset of Rijndael by the way: only 64 increments of the block size and key size are supported, if I remember correctly.顺便说一下,C#/.NET 只支持 Rijndael 的一个子集:如果我没记错的话,只支持块大小和密钥大小的 64 增量。
  • I'd still use aes = Aes.Create() rather than aes = new RijndaelManaged() as that leaves the choice of the AES implementation to the system, and it will probably choose the hardware accelerated one over the managed one.我仍然会使用aes = Aes.Create()而不是aes = new RijndaelManaged()因为这将 AES 实现的选择留给系统,并且它可能会选择硬件加速的而不是托管的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM