How to remove padded value in Decrypted Message?

I am using AES Algorithm.

Let's take this as the decrypted message:

"abcdefghijklmn  "

There is a 2 space in that decrypted message which is equivalent to 0x00. how can I removed that value? during decryption to show the exact message before it was encrypted.

I am using ECB Mode, I know that ECB mode is not secured, but I only want to know how to remove padded value in my decrypted message.


1 Answer 1


How you remove padding after decryption depends on how padding was added before encryption.

Typically, we design padding methods so that it is easy to figure out exactly how much padding was added; see This article for an overview.

From your question, it sounds like the encryptor is doing zero padding (that is, adding zero bytes until the next block boundary); if you can assume that the message will never end in a zero byte, then it is easy to remove. If you can't make that assumption, it sounds like you may need to switch to another padding method; the one in RFC5652 is one common method.

  • $\begingroup$ how to perform RFC 5652? I can't understand it clearly.. $\endgroup$
    – goldroger
    Jun 5, 2012 at 17:32
  • $\begingroup$ I only understand in that is that if I only need 1 padding value all I need to do is to add this "01" if the padding that is need is 14, all I need to do is this.. "1414141414141414141414141414" $\endgroup$
    – goldroger
    Jun 5, 2012 at 17:34
  • $\begingroup$ @JohnPaulParreño: if you need to add 14 bytes of padding, you would add (in hex) 0e 0e 0e 0e 0e 0e 0e 0e 0e 0e 0e 0e 0e 0e (there are exactly 14 of the 0e bytes, and 0e hex is 14 decimal) $\endgroup$
    – poncho
    Jun 5, 2012 at 17:36
  • 3
    $\begingroup$ In RFC 5652, you always add padding. That means that if the original message fell on a block boundary, you add an entire block of 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 bytes (10 hex = 16 decimal). $\endgroup$
    – poncho
    Jun 5, 2012 at 17:44
  • 1
    $\begingroup$ PKCS#7 padding can always be removed because the last byte of the padded message tells you how much padding there is. If your original message is 02 02 02 ... 02 02, and the padded message is 02 02 02 ... 02 02 02 02, then you know that the last two bytes, and only the last two bytes, are padding. The other bytes are the actual message. $\endgroup$
    – rossum
    Jun 5, 2012 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.