Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:41:14+00:00 2026-06-05T01:41:14+00:00

I have following two methods. 1st method //SymmetricEncryting private byte[] SymmetricEncrypt() { try {

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:41:16+00:00Added an answer on June 5, 2026 at 1:41 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods with the following signatures void Invoke(Action method) void Foo() What
I have the following two methods that get data from my DB and return
I have the following two methods that (as you can see) are similar in
I have the following two methods for getting data from my database: - (void)
I have the following two methods on an interface for my service layer: ICollection<T>
I have the following two archiving methods: - (void) encodeWithCoder: (NSCoder *) encoder {
I have following two methods in my backing bean - public String validateUser() {
Actually i have used following two methods for image resize but my client says
I have parsed XML using both of the following two methods... Parsing the XmlDocument
Imagine that I have the following two methods: void AddEvenNumbers() { for(int i=0; i<10000000;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.