1

I copied the code from here and i changed something that's not crucial and I now get this error:

Specified initialization vector (IV) does not match the block size for this algorithm

byte[] publicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
            74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
            207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
            108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
            240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
            168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
            38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
            106,99,179,68,175,211,164,116,64,148,226,254,172,147};

byte[] exponent = { 1, 0, 1 };

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
MemoryStream ms = new MemoryStream();
RijndaelManaged rm = new RijndaelManaged();

RSAParameters rsaKeyInfo = new RSAParameters
{
    Modulus = publicKey,
    Exponent = exponent
};

rsa.ImportParameters(rsaKeyInfo);

byte[] encryptedSymmetricKey = rsa.Encrypt(rm.Key, false);
byte[] encryptedSymmetricIV = rsa.Encrypt(rm.IV, false);

CryptoStream cryptoStream = new CryptoStream(ms, rm.CreateEncryptor(encryptedSymmetricKey, encryptedSymmetricIV), CryptoStreamMode.Write);

(I only added the CryptoStream)

Why in the microsoft article they encrypt key and iv if then throws exception of matching the size? for what purpose the encrypt this key and iv, how can i use this key and iv encrypted ?

Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
Susta250
  • 109
  • 3

0 Answers0