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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Ways to resolve the error - padding is invalid and cannot be removed.
Next
Answered
Sienna Tremblay
  • 32
Sienna Tremblay
Asked: May 18, 20222022-05-18T15:24:10+00:00 2022-05-18T15:24:10+00:00In: csharp

Ways to resolve the error – padding is invalid and cannot be removed.

  • 32

. Advertisement .

..3..

. Advertisement .

..4..

Hi developer experts, I have a small but frustrating use case, and so far, I couldn’t get my head around this problem & ideal solution. I am running my command and facing one problem with the padding is invalid and cannot be removed. Below is the command I used:

public void Cryptography(XmlDocument doc, bool cryptographyMode)
 {
  RijndaelManaged key = null;
  try
  {
  // Create a new Rijndael key.
  key = new RijndaelManaged();
  const string passwordBytes = "Password1234"; //password here 
 
  byte[] saltBytes = Encoding.UTF8.GetBytes("SaltBytes");
  Rfc2898DeriveBytes p = new Rfc2898DeriveBytes(passwordBytes, saltBytes);
  // sizes are devided by 8 because [ 1 byte = 8 bits ] 
  key.IV = p.GetBytes(key.BlockSize/8);
  key.Key = p.GetBytes(key.KeySize/8);
 
  if (cryptographyMode)
  {
  Ecrypt(doc, "Content", key);
  }
  else
  {
  Decrypt(doc, key);
  }
 
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  finally
  {
  // Clear the key.
  if (key != null)
  {
  key.Clear();
  }
  }
 
 }
 
 private void Decrypt(XmlDocument doc, SymmetricAlgorithm alg)
 {
  // Check the arguments. 
  if (doc == null)
  throw new ArgumentNullException("Doc");
  if (alg == null)
  throw new ArgumentNullException("alg");
 
  // Find the EncryptedData element in the XmlDocument.
  XmlElement encryptedElement = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
 
  // If the EncryptedData element was not found, throw an exception.
  if (encryptedElement == null)
  {
  throw new XmlException("The EncryptedData element was not found.");
  }
 
 
  // Create an EncryptedData object and populate it.
  EncryptedData edElement = new EncryptedData();
  edElement.LoadXml(encryptedElement);
 
  // Create a new EncryptedXml object.
  EncryptedXml exml = new EncryptedXml();
 
 
  // Decrypt the element using the symmetric key.
  byte[] rgbOutput = exml.DecryptData(edElement, alg); <---- I GET THE EXCEPTION HERE
  // Replace the encryptedData element with the plaintext XML element.
  exml.ReplaceData(encryptedElement, rgbOutput);
 
 }

When I run it, I get the following error:

Padding is invalid and cannot be removed

I am looking forward to gaining some knowledge from all experts. Thank you, guys!

cryptography
  • 2 2 Answers
  • 123 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-13T02:08:27+00:00Added an answer on June 13, 2022 at 2:08 am

    The cause: 

    When the same key and initialization vector are being used for both encoding and decoding, the error is caused by data encoding. That makes the error “padding is invalid and cannot be removed”.

    Solution:  

    The block cypher Rijndael/AES is used. It encrypts data in 16-character blocks of 128 bits. To ensure that the message’s last block is always the correct size, cryptographic padding is utilized.

    Your decryption technique is looking for whatever padding it has set as default, but it can’t locate it. For both encryption and decryption, as @NetSquirrel points out, you must specifically set the padding. You should use PKCS#7 padding for the best results.

    • 15
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Tiago Cordier
    2022-05-25T20:22:23+00:00Added an answer on May 25, 2022 at 8:22 pm

    You must ensure that the keys used to encrypt or decrypt have the same . The padding method even if not explicitly set should still allow for proper decryption/encryption (if not set they will be the same). However, if you use a different set keys for encryption than for decryption, will receive this error.

    Padding is invalid and cannot be removed

    You cannot use any algorithm to dynamically generate keys. Both encryption and decryption keys must be identical. One common way is to have the caller provide the keys in the constructor of the encryption methods class, to prevent the encryption/decryption process having any hand in creation of these items. It is focused on the task at hand (encrypting or decrypting data) so the caller must provide the iv, key codes.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.