Question

I have following two methods.

1st method

  //SymmetricEncryting 
 private byte[] SymmetricEncrypt()
  {
  try
      {
        //Get Byte Value 
        byte[] x= Encoding.Default.GetBytes("Test");

        byte [] y;

        //Create Symmetric Key Encription
        RijndaelManaged rijndaelManaged = new RijndaelManaged();

        //GetSymmetricPublicKey
        _symmetricPublicKey = rijndaelManaged.Key;

        //Get Symmetric Public IV
        _symmetricPublicIv = rijndaelManaged.IV;

        using (MemoryStream memoryStream = new MemoryStream(x))
        {
            //Start EncriptionProcess
            var cryptoStream = new CryptoStream(memoryStream,
                                                rijndaelManaged.CreateEncryptor
                                                 (_symmetricPublicKey,
                                                 _symmetricPublicIv),
                                                CryptoStreamMode.Write);

            cryptoStream.Write(x, 0, x.Length);

            // Complete the encryption process
            //cryptoStream.FlushFinalBlock();

            y= memoryStream.ToArray();


        }

        return y;
    }
    catch (Exception)
    {
        throw;
    }
}

2nd method

private string Decrypt(
    byte[] y,
    byte[] symmetricPublicKey,
    byte[] symmtricPublicIv)
{
    try
    {
        //Create the Key Container
        CspParameters cspParameters = new CspParameters();

        //Get the AsyPrivate and Public key from the Container
        cspParameters.KeyContainerName = "Keys";


        var rsaCryptoServiceProvider = new RSACryptoServiceProvider(cspParameters);

        //Decrypt and get the Symmetric Public key
        var decryptedSymmetricPubk = rsaCryptoServiceProvider.Decrypt(symmetricPublicKey, false);

        //Decrypt and get the Symmetric Public IV
        var decryptedSymmetricPubIv = rsaCryptoServiceProvider.Decrypt(symmtricPublicIv, false);

        //Create RijndaelManaged object to do the Symmtric dycrption
        RijndaelManaged rijndaelManaged = new RijndaelManaged();

        //Create cryptostream using decrypted symmetric Public Key and IV
        ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor(decryptedSymmetricPubk,
                                                                            decryptedSymmetricPubIv);
        //Create a memory stream 
        using (MemoryStream memoryStream = new MemoryStream(y))
        {

            var cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Read);

            byte[] z= new byte[y.Length];

            cryptoStream.Read(z, 0, z.Length);

            //cryptoStream.FlushFinalBlock();

            //Convert byte array to string
            var x= System.Text.Encoding.Default.GetString(z);

            return x;
        }
    }
    catch (Exception)
    {
        throw;
    }

As you see in the code i am trying to encrypt a string using symmetric encryption.I encrypt the symmetric public key and Iv by using the asymmetric public key which i have already created.Then i am trying to decrypt the encrypted string .

Problem 1 What is the purpose of having cryptoStream.FlushFinalBlock(); on both encryption and decryption.As i learned from msdn it will end the processes running on the cyptostream

Problem 2 If i uncomment the line cryptoStream.FlushFinalBlock(); it throws an exception "Memory stream is not expandable.". But if i comment the line it will work fine and return a byte array.

Problem 3 However the second method throws an exception "system.security.cryptography.cryptographicexception length of the data to decrypt is invalid ,on the execution of line cryptoStream.Read(z, 0, z.Length);

I couldn't find the actual cause of these errors on my debugging .Also i did some search on Google.But unfortunately i couldn't find any solution.Can any one please explain the answer?

Was it helpful?

Solution

You are encrypting using PKCS-padding (this is the default). AES/Rijndael is a block-cipher, which means that it only can encrypt blocks of 16 bytes at a time. To allow block-cipher to encrypt data of arbitrary sizes we use a padding algorithm. PKCS-padding works by adding 1-16 bytes at the end when encrypting and removing them when decrypting. The length of the padding is encoded in the padding itself.

You need the FlushFinalBlock when encrypting to let the CryptoStream know that there is no more incoming data and it should add the padding. It is not necessary and should not be used when you are using the CryptoStream in Read-mode.

The first exception come because you are using the plaintext-array as a backing store for the MemoryStream. Because of the padding the encryption will be larger than the plaintext.

The second exception is because you removed the FlushFinalBlock statement and because the MemoryStream is not allowed to resize to make an array of the correct length. The encrypted data should always be a multiple of 16 bytes, but since the MemoryStream will reuse x, y will have the same length as x, which is not always a valid length.

The solution is:

  1. Use FlushFinalBlock in SymmetricEncrypt.
  2. Replace using (MemoryStream memoryStream = new MemoryStream(x)) and using (MemoryStream memoryStream = new MemoryStream(y)) with using (MemoryStream memoryStream = new MemoryStream()). This will allow the MemoryStreams to resize freely.

OTHER TIPS

Oddly, it works for me to just do a write operation when de-crypting as well. Something like:

var decryptMemoryStream = new MemoryStream();
var decryptStream = new CryptoStream(decryptMemoryStream, iCryptoTransform , CryptoStreamMode.Write);

//write the unencrypted data array to the stream
decryptStream.Write(y, 0, y.Length);
decryptStream.Flush();
decryptStream.Close();

var decryptedData = decryptMemoryStream.ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top