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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:45:57+00:00 2026-05-27T01:45:57+00:00

I’m trying to encrypt XML, and after decryption I end up with 1 byte

  • 0

I’m trying to encrypt XML, and after decryption I end up with 1 byte too many – probably because of padding. This is my code. How can I change this to make it work?

public byte[] encryptData(byte[] source,string key)
{
    byte[] btKeyInBytes = UTF8Encoding.UTF8.GetBytes(key);
    Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(key, btKeyInBytes);

    AesManaged encryptor = new AesManaged();
    encryptor.Padding = PaddingMode.Zeros;

    using (MemoryStream encryptStream = new MemoryStream())
    {
        using (CryptoStream encStream = new CryptoStream(encryptStream, encryptor.CreateEncryptor(rfc.GetBytes(16), rfc.GetBytes(16)), CryptoStreamMode.Read))
        {
            //Read from the input stream, then encrypt and write to the output stream.
            encStream.Write(source, 0, source.Length);
            encStream.FlushFinalBlock();
            encryptor.Clear();
        }
        encryptStream.Flush();
        encryptedSource = encryptStream.ToArray();
    }
    return encryptedSource;
}

public byte[] decryptData(byte[] source, string key)
{
    byte[] encryptedSource = null;

    byte[] btKeyInBytes = UTF8Encoding.UTF8.GetBytes(key);
    Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(key, btKeyInBytes);

    AesManaged encryptor = new AesManaged();
    encryptor.Padding = PaddingMode.Zeros;

    using (MemoryStream encryptStream = new MemoryStream())
    {
        using (CryptoStream encStream = new CryptoStream(encryptStream, encryptor.CreateDecryptor(rfc.GetBytes(16), rfc.GetBytes(16)), CryptoStreamMode.Write))
        {
            //Read from the input stream, then encrypt and write to the output stream.
            encStream.Write(source, 0, source.Length);
            encStream.FlushFinalBlock();
            encryptor.Clear();
        }
        encryptStream.Flush();
        encryptedSource = encryptStream.ToArray();
    }

    return encryptedSource;
}

It seems that the padding gives me 1 extra byte during decryption

  • 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-27T01:45:58+00:00Added an answer on May 27, 2026 at 1:45 am

    I got it!

    Now let’s try to explain.

    Let’s say I have a file of 927 bytes.

    What I do is to read this file and split it in pieces of 656 bytes. This byte array of 656 bytes is being encrypted. The second array will be 271 bytes.

    In every block for encryption I used padding. When decrypting, you will not be able to know in which block padding was used because every block now can be divided by 16 (because of the padding in the encryption). Basically I only want padding used for the last block(271).

    so this is my new code:

    public byte[] encryptData(byte[] source, string key, bool padding)
    {
        byte[] encryptedSource = null;
    
    
        byte[] btKeyInBytes = UTF8Encoding.UTF8.GetBytes(key);
        Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(key, btKeyInBytes);
    
        AesManaged encryptor = new AesManaged();
        //encryptor.Mode = CipherMode.CFB; 
        encryptor.KeySize = 128;          // in bits     
        encryptor.Key = new byte[128 / 8];  // 16 bytes for 128 bit encryption     
        encryptor.IV = new byte[128 / 8];
        if (padding) { encryptor.Padding = PaddingMode.PKCS7; }
        else { encryptor.Padding = PaddingMode.None; }
    
    
        using (MemoryStream encryptStream = new MemoryStream())
        {
            using (CryptoStream encStream =
                       new CryptoStream(encryptStream,
                                        encryptor.CreateEncryptor(rfc.GetBytes(16),
                                                                  rfc.GetBytes(16)),
                                        CryptoStreamMode.Write))
            {
                //Read from the input stream, then encrypt and write to the output stream.
                encStream.Write(source, 0, source.Length);
            }
            encryptStream.Flush();
            encryptedSource = encryptStream.ToArray();
        }
        return encryptedSource;
    }
    
    public byte[] decryptData(byte[] source, string key,bool padding)
    {
        byte[] encryptedSource = null;
    
        byte[] btKeyInBytes = UTF8Encoding.UTF8.GetBytes(key);
        Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(key, btKeyInBytes);
    
        AesManaged encryptor = new AesManaged();
        encryptor.Key = new byte[128 / 8];  // 16 bytes for 128 bit encryption     
        encryptor.IV = new byte[128 / 8];
        if (padding) { encryptor.Padding = PaddingMode.PKCS7; }
        else { encryptor.Padding = PaddingMode.None; }
    
    
        using (MemoryStream encryptStream = new MemoryStream())
        {
            using (CryptoStream encStream =
                     new CryptoStream(encryptStream,
                                      encryptor.CreateDecryptor(rfc.GetBytes(16),
                                                                rfc.GetBytes(16)),
                                      CryptoStreamMode.Write))
            {
                //Read from the input stream, then encrypt and write to the output stream.
                encStream.Write(source, 0, source.Length);
            }
            encryptStream.Flush();
            encryptedSource = encryptStream.ToArray();
    
        }
    
        return encryptedSource;
    }
    

    I hope this helps!

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.