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

Ask A Question

Stats

  • Questions 162k
  • Answers 162k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Server side Javascript is one of the languages supported by… May 12, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer It isn't a component, but Alkit VNC does seem to… May 12, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer Microsoft is really trying to say "Don't design your class… May 12, 2026 at 12:03 pm

Related Questions

I intend to use TripleDES in one of my project. I was doing some
I'm sure this is an easy question, but I don't have an answer for.
Underlying the TCP transport stack are a number of buffer limits sometimes documented by
I am receiving packets sent to my server through UDP. I am using socket_read

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.