gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:15:13 25 4
gpt4 key购买 nike

鉴于我将 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 和加密 key 分别长 128 位和 192 位。但是,如果我将加密 key 设置为 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();
}
}
}
}

这仍然会毫无异常(exception)地进行加密。我的问题是:

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

最佳答案

KeyKeySize RijndaelManaged 类的属性继承自基类 SymmetricAlgorithm类(class)。这些属性的精确行为在 MSDN 库中没有详细记录,但如果您查看 SymmetricAlgorithm source code ,您会看到 Key 和 KeySize 属性相互交互,如下所示:

  • 设置 Key 属性时,KeySize 属性会自动设置为新 key 的长度(以位为单位)。
  • 当您设置 KeySize 属性时,现有的 Key 将被清除(并在下次访问 Key 时自动生成一个新的随 secret 钥)。

因此,如果您要设置 Key 属性,则事先设置 KeySize 属性没有意义。

关于c# - RijndaelManaged - 设置 KeySize 属性有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com