c# 使用正确的字节数组调用函数

c# calling function with correct byte array

我需要调用这个函数来加密我的字节数组。 该函数需要字节数组进行加密,一个字节数组作为密码,另一个字节数组作为初始化向量。 函数本身:

public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV) 
{ 

    MemoryStream ms = new MemoryStream(); 


    Rijndael alg = Rijndael.Create(); 


    alg.Key = Key; 
    alg.IV = IV; 


    CryptoStream cs = new CryptoStream(ms, 
       alg.CreateEncryptor(), CryptoStreamMode.Write); 


    cs.Write(clearData, 0, clearData.Length); 


    cs.Close(); 


    byte[] encryptedData = ms.ToArray();

    return encryptedData; 
}

这听起来可能很奇怪,但我没有得到使用此功能的正确调用。我的问题是为 password/IV 使用正确的字节数组。 我尝试使用:

Encrypt(read, new byte[] { 0x49, 0x49, 0x49, 0x49, 0x4, 0x4, 0x4, 0x4 }, new byte[] { 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61 });

我只是不知道如何调用此函数。调用此函数(密码和 IV)的字节数组的正确版本是什么?

您可以使用

生成您的密钥
RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.GenerateKey();
myRijndael.GenerateIV();

然后将它们存储在某处保存以使用它们来加密和解密您的消息

byte[] key = myRijindael.Key
byte[] iv = myRijindael.Iv

编辑: 刚注意到您使用的是 Rijindael Class 而不是 RijindaelManaged。在 msdn Example 他们说 "Create a new instance of the Rijndael class. This generates a new key and initialization vector (IV)."

所以在你创建了一次实例之后

Rijndael myRijndael = Rijndael.Create()

只需存储密钥。