Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
how to perform RFC 5652? I can't understand it clearly.. –  goldroger Jun 5 '12 at 17:32
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" –  goldroger Jun 5 '12 at 17:34
@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) –  poncho Jun 5 '12 at 17:36
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). –  poncho Jun 5 '12 at 17:44
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. –  rossum Jun 5 '12 at 21:04
show 5 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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