简体   繁体   中英

c# calling function with correct byte array

I've got this function I need to call to encrypt my byte array. The function needs the byte array to be encryptet, a byte array as password and another byte array as initialization vector. The function itself:

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; 
}

It may sound strange but I don't get a correct call to use this function. My problem is in using a correct byte array for the password/IV. I tried using:

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

I just don't get the trick how to call this function. What's the correct version of a byte array to call this function (password and IV)?

you can generate your Keys with

RijndaelManaged myRijndael = new RijndaelManaged();
myRijndael.GenerateKey();
myRijndael.GenerateIV();

then store them somewhere save to use them to encrypt and decrypt your messages

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

EDIT: just noticed you are using the Rijindael Class not RijindaelManaged. In the msdn Example they say "Create a new instance of the Rijndael class. This generates a new key and initialization vector (IV)."

So after you created the instance once

Rijndael myRijndael = Rijndael.Create()

just store the keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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