RijndaelManaged - 设置 KeySize 属性 有什么作用?

RijndaelManaged - What does setting KeySize property do?

鉴于我将 KeySize 设置为 192,当我通过执行以下操作加密一些数据时:

 static void Main()
        {
            var querystrings = "dataToEncrypt";
            byte[] keyvalue = new byte[] { 241, 205, 121, 123, 109, 246, 247, 103, 31, 225, 167, 220, 247, 119, 247, 119, 78, 125, 127, 167, 156, 213, 230, 250, };
            byte[] ivvalue = new byte[] { 194, 177, 79, 213, 87, 46, 213, 189, 243, 169, 32, 13, 217, 62, 34, 170, };
            byte[] querystringBytes = Encoding.UTF8.GetBytes(querystrings);

            using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
            {
                rijndaelManaged.BlockSize = 128;
                rijndaelManaged.KeySize = 192;
                rijndaelManaged.IV = ivvalue;
                rijndaelManaged.Key = keyvalue;
                rijndaelManaged.Padding = PaddingMode.PKCS7;
                rijndaelManaged.Mode = CipherMode.CBC;

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    ICryptoTransform transform = rijndaelManaged.CreateEncryptor();
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(querystringBytes, 0, querystringBytes.Length);
                        cryptoStream.FlushFinalBlock();
                        var output = Convert.ToBase64String(memoryStream.ToArray());
                        Console.WriteLine($"{output}");
                        Console.ReadKey();
                    }
                }
            }
        }

数据按照我的预期加密,因为 IV 和加密密钥分别长 128 位和 192 位。但是,如果我将加密密钥设置为 128 位长(为了参数起见,让我们使用与 IV 相同的字节数组)即

 static void Main()
        {
            var querystrings = "dataToEncrypt";
            byte[] keyvalue = new byte[] { 194, 177, 79, 213, 87, 46, 213, 189, 243, 169, 32, 13, 217, 62, 34, 170, };
            byte[] ivvalue = new byte[] { 194, 177, 79, 213, 87, 46, 213, 189, 243, 169, 32, 13, 217, 62, 34, 170, };
            byte[] querystringBytes = Encoding.UTF8.GetBytes(querystrings);

            using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
            {
                rijndaelManaged.BlockSize = 128;
                rijndaelManaged.KeySize = 192;
                rijndaelManaged.IV = ivvalue;
                rijndaelManaged.Key = keyvalue;
                rijndaelManaged.Padding = PaddingMode.PKCS7;
                rijndaelManaged.Mode = CipherMode.CBC;

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    ICryptoTransform transform = rijndaelManaged.CreateEncryptor();
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(querystringBytes, 0, querystringBytes.Length);
                        cryptoStream.FlushFinalBlock();
                        var output = Convert.ToBase64String(memoryStream.ToArray());
                        Console.WriteLine($"{output}");
                        Console.ReadKey();
                    }
                }
            }
        }

这仍然会毫无例外地进行加密。我的问题是:

当我明确将 KeySize 设置为 192 时,KeySize 在接受 128 位加密密钥时在内部做什么?为什么没有异常发生?

Key and KeySize properties of the RijndaelManaged class are inherited from the base SymmetricAlgorithm class. The precise behavior of these properties isn't well documented in MSDN Library, but if you look at the SymmetricAlgorithm source code,您将看到 Key 和 KeySize 属性相互交互,如下所示:

  • 当您设置密钥 属性 时,密钥大小 属性 会自动设置为新密钥的长度(以位为单位)。
  • 当你设置KeySize属性时,现有的Key会被清空(下次访问Key时会自动生成一个新的随机key)

因此,如果您要设置密钥 属性,则事先设置密钥大小 属性 毫无意义。