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

  • Home
  • SEARCH
  • 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 198245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:55:18+00:00 2026-05-11T16:55:18+00:00

I get Length of the data to decrypt is invalid. exception when i try

  • 0

I get “Length of the data to decrypt is invalid.” exception when i try to decrypt a memory stream. I am beginner, cant figure out whats wrong. whats wrong?

public bool EncryptStream()
    {

        string password = @"myKey123"; // Your Key Here
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        s_EncryptedStream = new MemoryStream();
        int NoOfBytes;
        byte[] b_Buffer = new byte[8192];

        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        s_CrytpoStream = new CryptoStream(s_EncryptedStream,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write);

        while (s_MemoryStream.Length < s_MemoryStream.Position)
        {
            NoOfBytes = s_MemoryStream.Read(b_Buffer, 0, 8192);
            s_CrytpoStream.Write(b_Buffer, 0, NoOfBytes);
        }

        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        while (s_EncryptedStream.Position < s_EncryptedStream.Length)
        {
            NoOfBytes = s_EncryptedStream.Read(b_Buffer, 0, 8192);
            s_MemoryStream.Write(b_Buffer, 0, NoOfBytes);

        }
        s_CrytpoStream.Flush();
        s_CrytpoStream.Close();

        return true;

    }


    public bool DecryptStream()
    {


        string password = @"myKey123"; // Your Key Here

        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        int NoOfBytes;
        byte[] b_Buffer = new byte[8192];

        s_DecryptedStream = new MemoryStream();


        RijndaelManaged RMCrypto = new RijndaelManaged();

        s_CrytpoStream = new CryptoStream(s_MemoryStream,
            RMCrypto.CreateDecryptor(key, key),
            CryptoStreamMode.Read);

        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        while (s_MemoryStream.Length > s_MemoryStream.Position)
        {
            NoOfBytes = s_CrytpoStream.Read(b_Buffer, 0, 8192);
            s_DecryptedStream.Write(b_Buffer, 0, NoOfBytes);
        }

        s_DecryptedStream.Seek(0, SeekOrigin.Begin);
        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        while (s_DecryptedStream.Position < s_DecryptedStream.Length)
        {
            NoOfBytes = s_DecryptedStream.Read(b_Buffer, 0, 8192);
            s_MemoryStream.Write(b_Buffer, 0, NoOfBytes);

        }

        s_CrytpoStream.Flush();
        s_CrytpoStream.Close();

        return true;

    }
  • 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-05-11T16:55:18+00:00Added an answer on May 11, 2026 at 4:55 pm

    For a start, this while loop condition is never right:

    while (s_MemoryStream.Length < s_MemoryStream.Position)
    

    How can the position be beyond the length?

    Rather than using the length of a stream, the usual pattern to copy a stream is to read repeatedly until the value returned isn’t positive. As you’re doing that twice in this code anyway, you might as well encapsulate it:

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[8192];
        int read;
        while ( (read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }
    }
    

    It’s also best to use using statements to clean up strings. Additionally, the Encoding.Unicode property means you don’t have to create a new UnicodeEncoding yourself. Also, I generally find that setting the Position property is more readable than using Seek. Finally, there’s no point in a method returning a value if it’s always going to be true. So, your code would become:

    public void EncryptStream()
    {
        string password = @"myKey123"; // Your Key Here
        byte[] key = Encoding.Unicode.GetBytes(password);
    
        s_EncryptedStream = new MemoryStream();
        s_MemoryStream.Position = 0;
    
        RijndaelManaged RMCrypto = new RijndaelManaged();
    
        using (Stream crytpoStream = new CryptoStream(s_EncryptedStream,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write))
        {
            CopyStream(s_MemoryStream, cryptoStream);
        }
    
        s_MemoryStream.Position = 0;
        s_EncryptedStream.Position = 0;
        CopyStream(s_EncryptedStream, s_MemoryStream);
    }
    
    public void DecryptStream()
    {
        string password = @"myKey123"; // Your Key Here
        byte[] key = Encoding.Unicode.GetBytes(password);
    
        s_DecryptedStream = new MemoryStream();
        s_MemoryStream.Position = 0;
    
        RijndaelManaged RMCrypto = new RijndaelManaged();
    
        using (Stream crytpoStream = new CryptoStream(s_MemoryStream,
            RMCrypto.CreateDecryptor(key, key),
            CryptoStreamMode.Read))
        {
            CopyStream(cryptoStream, s_DecryptedStream);
        }
    
        s_DecryptedStream.Position = 0;
        s_MemoryStream.Position = 0;
    
        CopyStream(s_DecryptedStream, s_MemoryStream);
    }
    

    Even after amending this code, it feels like you’ve got way too many non-local variables here. I can’t see why any of this should be in instance variables. Make the stream to be encrypted or decrypted a parameter (along with the password) and return a memory stream with the encrypted/decrypted data in, or just a byte array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to encrypt / decrypt a string in VB.Net.
I have a C# application that needs to get variable-length data passed back from
Is there some efficient way how to get length of data in varbinary(max) filestream
How would I get the length of an ArrayList using a JSF EL expression?
How can I get the length of text entered in a textbox using jQuery?
How do you get the length of a string in jQuery?
How in C++ get array length with pointers only ? I know that tab
If I want to get the length of each match within the parentheses in
Is there a way to get the length in bytes of a CFString given
Is there a way to get the length of a collection from this? ParallelQuery<string>

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.