我正在尝试使用C#进行AES 256位CBC加密。我正在使用此页面上的示例密钥/iv字符串:http://pic.dhe.ibm.com/infocenter/initiate/v9r5/topic/com.ibm.einstall.doc/topics/t_einstall_GenerateAESkey.html。但是当我运行下面的函数时,尝试设置cipher.Key时会收到错误消息“指定的密钥不是此算法的有效大小。” 我能够在node.js项目中使用此密钥/iv组合,但我尝试将其移植到C#时无功而返。我在下面做错了什么?
static void Main(string[] args)
{
string keyString = "B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF";
string ivString = "7E892875A52C59A3B588306B13C31FBD";
byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] iv = Encoding.UTF8.GetBytes(ivString);
Console.WriteLine("Key is " + key.Length + " bytes.");
using (RijndaelManaged cipher = new RijndaelManaged())
{
cipher.Mode = CipherMode.CBC;
cipher.KeySize = 256;
cipher.BlockSize = 128;
cipher.Key = key;
cipher.IV = iv;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = cipher.CreateEncryptor(cipher.Key, cipher.IV);
}
Console.WriteLine("Success!");
Console.ReadKey();
}