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 8718611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:35:36+00:00 2026-06-13T06:35:36+00:00

I am trying to encrypt a string of text using the RijndaelManaged class in

  • 0

I am trying to encrypt a string of text using the RijndaelManaged class in .NET. However, I keep getting a CryptographicException (“Length of the data to decrypt is invalid”). This message isn’t exactly helpful, especially since its happening when I attempt to ENCRYPT data, not decrypt. Below is the code.

public static string EncryptKMSToken(string valueToEncrypt, string encryptionKey)
{
  string results = string.Empty;

  using (RijndaelManaged aes = new RijndaelManaged())
  {
    aes.BlockSize = 128;
    aes.KeySize = 128;
    aes.Padding = PaddingMode.PKCS7;
    aes.Mode = CipherMode.CBC;

    UTF8Encoding byteTransform = new UTF8Encoding();
    aes.Key = byteTransform.GetBytes(encryptionKey);
    ICryptoTransform encryptor = aes.CreateDecryptor(aes.Key, aes.IV);

    using (MemoryStream stream = new MemoryStream())
    {
      using (CryptoStream encryptStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
      {
        using (StreamWriter writer = new StreamWriter(encryptStream))
        {
          writer.Write(valueToEncrypt);
        }

        byte[] encryptedBytes = stream.ToArray();
        results = byteTransform.GetString(encryptedBytes);
      }
    }
  }

  return results;
}

The error happens when the 3rd using statement closes (i.e. the line after writer.Write(valueToEncrypt). If I try to move the two lines below that inside the third using block, I end up with an empty string (and the error still happens). I pulled this code directly off of this site (http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp) but it doesn’t seem to work. Does anyone have any ideas?

  • 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-13T06:35:36+00:00Added an answer on June 13, 2026 at 6:35 am

    Yes, but I think you will too if you reread your code:

    ICryptoTransform encryptor = aes.CreateDecryptor(aes.Key, aes.IV);
    

    That’s a decryptor named as an encryptor…

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

Sidebar

Related Questions

I'm trying to encrypt a text file using Perl and then decrypt it using
I'm trying to figure out how to encrypt / decrypt a string in VB.Net.
I am trying to encrypt a string using C# and decrypt it using Python.
I am trying to encrypt/decrypt a string using eith Rijndael or Aes and the
I'm trying to encrypt and decrypt a file stream over a socket using RijndaelManaged,
I am trying to encrypt and decrypt some simple text. But for some reason
Talking about javax.crypto.Cipher I was trying to encrypt data using Cipher.getInstance(RSA/None/NoPadding, BC) but I
I am trying to write encrypt/decrypt methods for AES 256 CBC encryption using PKCS5Padding
Hello I am trying to encrypt / decrypt a string via Rijaendal. I simply
I'm trying to encrypt a simple string using AES algorithm. Everything seems fine and

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.