Specified key is not a valid size for this algorithm

后端 未结 5 1826
抹茶落季
抹茶落季 2020-11-29 07:13

I have with this code:

RijndaelManaged rijndaelCipher = new RijndaelManaged();

            // Set key and IV
            rijndaelCipher.Key = Convert.FromBa         


        
5条回答
  •  没有蜡笔的小新
    2020-11-29 07:44

    Use the random number generator class (RNGCryptoServiceProvider) to fill a specified buffer with random bytes as follows:

    var numberOfBits = 256; // or 192 or 128, however using a larger bit size renders the encrypted data harder to decipher
    
    var ivBytes = new byte[numberOfBits / 8]; // 8 bits per byte
    
    new RNGCryptoServiceProvider().GetBytes(ivBytes);
    
    var rijndaelManagedCipher = new RijndaelManaged();
    
    //Don't forget to set the explicitly set the block size for the IV if you're not using the default of 128
    
    rijndaelManagedCipher.BlockSize = 256;
    
    rijndaelManagedCipher.IV = ivBytes;
    

    Note the same process could be used to derive a key. Hope this helps.

提交回复
热议问题