The most common query we get about cryptography concerns padding with a block cipher.
If you have to encrypt some data to send to another user and everything else works OK except the last few bytes, your problem is probably one of padding.Read on.
This article describes the mechanics of padding with examples using commonblock encryption algorithms like AES, Triple DES and Blowfish.It considers where you should use padding and where you don't need to.
What is padding?Security considerationsExample of ECB modeExample using Blowfish in CBC modeWhat about ciphers like AES that use larger block sizes?Example using AESCipher-Feedback (CFB) and Output-Feedback (OFB) ModesWhen don't I need to use padding?Example using Triple DES without paddingUsing random paddingSo when do I use padding and when don't I?ReferencesOther InformationCommentsBlock cipher algorithms like AES and Triple DES in Electronic Code Book (ECB)and Cipher Block Chaining (CBC) mode require their input to be an exact multipleof the block size. If the plaintext to be encrypted is not an exact multiple, you need to pad before encryptingby adding a padding string.When decrypting, the receiving party needs to know how to remove the padding in an unambiguous manner.
The plain text is the ASCII encoding of"Now is the time for". That is, the 19-byte sequence4E 6F 77 20 69 73 20 74 68 65 20 74 69 6D 65 20 66 6F 72.We are encrypting using DES in ECB mode with the cryptographic key0x0123456789ABCDEF.To encrypt, we break up the plaintext into blocks of 8 bytes (Note we are using 8 in this example because the blocksize for DES is 64 bits or 8 bytes; if this were AES we'd be using 16 bytes).
Note: Using DES with a single 64-bit key is definitely no longer recommended, but we use it herefor simplicity to demonstrate the principle of padding for an 8-byte block. It is easy to extend this to a more-secure 192-bit Triple DES key or 256-bit Blowfish key. Similarly we use ECB mode to avoid the complications of initialization vectors.
The input plaintext of 19 bytes N o w _ i s _ t h e _ t i m e _ f o r 4E 6F 77 20 69 73 20 74 68 65 20 74 69 6D 65 20 66 6F 72 breaks up into two blocks of 8 bytes plus an odd three bytes at the end +-----------------------+-----------------------+-----------------------+ |N o w _ i s _ t |h e t i m e _ |f o r ? ? ? ? ? | +-----------------------+-----------------------+-----------------------+ |4E 6F 77 20 69 73 20 74|68 65 20 74 69 6D 65 20|66 6F 72 ?? ?? ?? ?? ??| +-----------------------+-----------------------+-----------------------+ In ECB mode, each 8-byte block is encrypted independently. First block: DES INPUT BLOCK = N o w _ i s _ t (IN HEX) 4E 6F 77 20 69 73 20 74 DES OUTPUT BLOCK = 3F A4 0E 8A 98 4D 43 15 Second block: DES INPUT BLOCK = h e _ t i m e _ (IN HEX) 68 65 20 74 69 6D 65 20 DES OUTPUT BLOCK = 6A 27 17 87 AB 88 83 F9 So far, so good. But what do we do with the odd characters at the end of our plaintext? DES INPUT BLOCK = f o r ?? ?? ?? ?? ?? (IN HEX) 66 6F 72 ?? ?? ?? ?? ??We need to pad the block with padding bytes to make it up to the required length.There are at least five common conventions:-
Pad with bytes all of the same value as the number of padding bytesPad with 0x80 followed by zero bytesPad with zeroes except make the last byte equal to the number of padding bytesPad with zero (null) charactersPad with space charactersMethod one is the most popular and is usually referred to as "PKCS5 padding".It is generally recommended in the absence of any other considerations.
Hint: If you are short of time and don't want to read all of this document, use PKCS5 padding.
Pad the input with a padding string of between 1 and 8 bytes to make the total length an exact multiple of 8 bytes. The value of each byte of the padding string is set to the number of bytes added - i.e. 8 bytes of value 0x08, 7 bytes of value 0x07, ..., 2 bytes of 0x02, or one byte of value 0x01.
Our fourth block is padded with 5 bytes of value 0x05: DES INPUT BLOCK = f o r _ _ _ _ _ (IN HEX) 66 6F 72 05 05 05 05 05 KEY = 01 23 45 67 89 AB CD EF DES OUTPUT BLOCK = FD 29 85 C9 E8 DF 41 40After decrypting, read the last character decrypted and strip off that many bytes.
This method can be used with any plaintext, ASCII or binary. Don't forgetto check first that the number of characters to be stripped is between one and eight.This also gives you an extra check that the decryption has been carried out correctly(well, if its value is not between 1 and 8 you know for sure that is hasn't, but if it is,it doesn't necessarily mean it has). As an additional check, check all of the padding bytes.
Add a single padding byte of value 0x80 and then pad thebalance with enough bytes of value zero to make the total length an exact multipleof 8 bytes. If the single 0x80 byte makes the total length an exact multiplethen do not add any zero bytes. This is known as "OneAndZeroes padding"(and general usage seems to retain the arguably incorrect spelling of "zeroes"!).
Our fourth block is padded with 0x80 followed by 4 bytes of value 0x00: DES INPUT BLOCK = f o r _ _ _ _ _ (IN HEX) 66 6F 72 80 00 00 00 00 KEY = 01 23 45 67 89 AB CD EF DES OUTPUT BLOCK = BE 62 5D 9F F3 C6 C8 40After decrypting, strip off all trailing zero bytes and the 0x80 byte.
This method can be used with any plaintext, ASCII or binary. Again, you have a check for incorrect decryption if you don't find the expected padding bytes at the end.Cryptographers who work with smart cards seem to prefer this method, see[RANK].It is recommended in NIST 800-38a [BCMO].In their latest book [FERG], Niels Ferguson and Bruce Schneier recommend either this method or method 1.
This is a variant of method 1 described in [SCHN]. The convention with this method is usuallyalways to add a padding string, evenif the original plaintext was already an exact multiple of 8 bytes.The final byte could therefore have a value between 0x01 and 0x08.
After decrypting, read the last character decrypted and strip off that many bytes(checking first that its value is between one and eight).
This method can be used with any plaintext, ASCII or binary.
After decrypting, trim all null characters found at the end untilyou find a non-null character.You cannot use this method when the plaintext could contain a null value.This is not a problem if you are dealing with ASCII text, butwould be if encrypting binary data like an EXE file.
After decrypting, just trim any trailing space characters, if any
The resulting ciphertext from these five methods will be as follows:
|-- block 1 ---| |-- block 2 ---| |-- block 3 ---| 1. 3FA40E8A984D4315 6A271787AB8883F9 FD2985C9E8DF4140 2. 3FA40E8A984D4315 6A271787AB8883F9 BE625D9FF3C6C840 3. 3FA40E8A984D4315 6A271787AB8883F9 91192C64B55C5DB8 4. 3FA40E8A984D4315 6A271787AB8883F9 9E14FB96C5FEEB75 5. 3FA40E8A984D4315 6A271787AB8883F9 E3FFECE5211F3525 Note how different the last blocks are.This example is from Eric Young's set of test vectors for Blowfish.Note that the plaintext data includes a trailing null character (0x00) after the final space character (0x20).
key[16] = 0123456789ABCDEFF0E1D2C3B4A59687 iv[8] = FEDCBA9876543210 data[29] = "7654321 Now is the time for " (includes trailing '\0') data[29] = 37363534333231204E6F77206973207468652074696D6520666F722000 Method 1 PKCS5 method IN: 37363534333231204E6F77206973207468652074696D6520666F722000030303 OUT: 6B77B4D63006DEE605B156E27403979358DEB9E7154616D9749DECBEC05D264B ^^^^^^^^^^^^^^^^ Method 2 Pad with 0x80 + zeros IN: 37363534333231204E6F77206973207468652074696D6520666F722000800000 OUT: 6B77B4D63006DEE605B156E27403979358DEB9E7154616D9BB3F8B9254003C40 ^^^^^^^^^^^^^^^^ Method 3 Pad with nulls + # bytes IN: 37363534333231204E6F77206973207468652074696D6520666F722000000003 OUT: 6B77B4D63006DEE605B156E27403979358DEB9E7154616D9A078DBB46155E4AC ^^^^^^^^^^^^^^^^ Method 4 Pad with nulls (this is the method used in the test vectors) IN: 37363534333231204E6F77206973207468652074696D6520666F722000000000 OUT: 6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC ^^^^^^^^^^^^^^^^ Method 5 Pad with spaces IN: 37363534333231204E6F77206973207468652074696D6520666F722000202020 OUT: 6B77B4D63006DEE605B156E27403979358DEB9E7154616D9332223899980E694 ^^^^^^^^^^^^^^^^Note that the last 8 bytes of ciphertext are all completely different.For this example, because the plaintext contains trailing space characters and nulls, the padding in methods 4 and 5 cannot be removed unabiguously.
More modern block ciphers like the Advanced Encryption Algorithm (AES) have larger block sizes than DES and Blowfish. All the arguments above still apply, except you shouldreplace the number 8 with the appropriate block size inbytes. For example, if usingAES with a 128-bit block, pad to the next multiple of 16. Note, too, that it is theblocksize that matters, not the size of the key.
If you encrypt using Cipher-Feedback (CFB) or Output-Feedback (OFB) or counter (CTR) modes then the ciphertext will be the same size as the plaintext and so padding is not required. Be careful, though, when using these modes, becausethe initialisation vectors (IV) must be unique.
Similarly, encrypting using a stream cipher like RC4 or PC1 does not require padding.
You need to communicate a packet of information that is always the same format:-
FieldLengthExampleCustomer reference number:5 digits12345Credit card number:15 digits376066666655555Expiry date:4 digits1205Total:24 digitsThis is always 24 bytes long, so if we are encrypting with DES or Triple DES or Blowfish, our data is always a fixed multiple of the block size.There is no need to add any padding to this before encrypting,provided that you and the recipient agree not to.
Example using Triple DES with key 0x0123456789ABCDEFFEDCBA987654321089ABCDEF01234567
ASCII PLAINTEXT: 123453760666666555551205 3DES INPUT BLOCKS: 12345376 06666665 55551205 INPUT IN HEX: 3132333435333736 3036363636363635 3535353531323035 3DES OUTPUT: 7ADE45981580DB32 421E3D90B5B47D5B 1175FA3DD8B932D7If you are transmitting messages that are shorter than the block length and encryptingusing ECB mode, your ciphertext will be identical for identical messages.One solution is to use a variant of method 3 but use random padding bytes instead of nulls.
Alternatively, if the size of your encrypted message might give some information away,add a random number of random padding bytes to the message.
We could use a variant of our method 3 above using random padding bytes instead ofnulls. The final byte tells us how many bytes to strip after deciphering.The following two examples use four random padding bytes plus 0x05 and producecompletely different results.
ACSII PLAINTEXT: buy (IN HEX): 627579 INPUT BLOCK: 62757958B3989B05 KEY: FEDCBA9876543210 OUTPUT BLOCK: 061FF118B96F4EE8 ACSII PLAINTEXT: buy (IN HEX): 627579 INPUT BLOCK: 62757934297CC805 KEY: FEDCBA9876543210 OUTPUT BLOCK: AAE42B7527A9078AA receiving party with the correct key could derive the original plaintextfrom any such message. An attacker is now faced with solving many morealternatives.
Note that this will only work for a short message. You can achieve the same securityby using CBC mode with a unique Initialisation Vector (IV), but you then have the overhead of having to transmit the IV as well as the ciphertext.
As a very contrived example, let's say we might be sending two alternativemessages that read:
Offer $90000.00 OR Offer $1000000.00 Let's say we use the PKCS5 method of padding when we encrypt these two messagesusing Blowfish in CBC mode with 128-bit key 0x0123456789ABCDEFF0E1D2C3B4A59687and IV 0xFEDCBA9876543210. ASCII PT: Offer $90000.00 (IN HEX): 4F66666572202439303030302E3030 INPUT: 4F66666572202439303030302E303001 OUTPUT: 33BEF550BADE4798DDA5C960E2C70EB9 ASCII PT: Offer $1000000.00 (IN HEX): 4F666665722024313030303030302E3030 INPUT: 4F666665722024313030303030302E303007070707070707 OUTPUT: A4B8D1BF3020DB24CDD459BAB6A7BA7B02AC39EE7C1BF090An eavesdropper who knows that the message may contain either $90,000or $1,000,000 could work out the answer just from the length of the ciphertext.
One solution is to add a random number of random bytes as padding, and toindicate the number of random bytes added by the last byte in padded inputblock.
ASCII PT: Offer $90000.00 (IN HEX): 4F66666572202439303030302E3030 INPUT: 4F66666572202439303030302E303012441C0D5E2C60147DF54910B6A6445311 OUTPUT: 33BEF550BADE4798B164164E571A5266B0D488FAD934D6386494FAF528C8ED82 ASCII PT: Offer $1000000.00 (IN HEX): 4F666665722024313030303030302E3030 INPUT: 4F666665722024313030303030302E3030CEF8302A84BA07 OUTPUT: A4B8D1BF3020DB24CDD459BAB6A7BA7BC01DF3FCC3B7DC1BThis particular convention of using only the last byte limits you to 255 random padding bytes.It may be more practical to encode your plaintext with a header that indicates the number of valid bytes that follow plus a much larger random number of random padding bytes and then encrypt that.
To comment on this page or ask a question, please send us a message.
This page last updated 30 March 2016
10 comments so far
[Comments are now closed]
Great explanation. Especially about advises about;"So when do I use padding and when don't I?"
PublicDinamicPi | Spain_Codebreakers - Sun, Jan 30 2011 13:53 GMTLiked a lot the text, very clear!
LuKe | Braszil - Wed, Apr 27 2011 16:29 GMTReally great article. The reason I found this page was one quesiton I did not see answered. What should the convention be if your unpadded data does fit an exact blocksize. Since you agree to use padding, depending on those last few bytes, it could be misread as actual padding bytes instead of real data. I imagine the solution is to add a full block of straight padding following the real data. Is that a reasonable approach?
Encrpyt_Newb | - Sat, Jul 30 2011 01:35 GMTEncrpyt_Newb: A very good point. Methods 1 to 4 use the convention that a padding block is *always* added, thus avoiding the problem you correctly foresee. With method 5 the convention is usually that no padding block is added, because this is usually used only for simple plain text where extra spaces at the end do not affect the main message.
See the section
http://www.di-mgt.com.au/cryptopad.html#whenandwhennot Dave | Moderator - Sun, Jul 31 2011 18:06 GMTVery helpful documentation, thanks on that. There is one thing I'm not sure about. The text says "Don't forget to check first that the number of characters to be stripped is between one and eight" for method 1. In my opinion the check should be between one and seven? Eight chars to strip would mean that the whole block had only padding data. So the user data was a multiple of the blocklength and no padding is needed at all?
ltc_user | - Thu, Mar 1 2012 15:10 GMTltc_user: See the reply to Encrypt_Newb above. With method 1 padding is *always* added. If the user data is an exact multiple of the blocklength then we add a whole extra padding block of length blocklength. This then needs to be stripped afterwards.
If you think about it, this provides an unambiguous method of padding, which is simple to program to remove when decrypting. Providing, of course, that both parties agree to use it. This is the method used in almost all approved encryption protocols.
Dave | Moderator - Tue, Mar 6 2012 11:42 GMTCan you explain CSSM_PADDING_PKCS1 padding which is essentially PKCS1 v1.5 signature padding?
webie | - Fri, Apr 20 2012 16:01 GMTwebie: See our RSA page "Signing using PKCS#1v1.5":
http://www.di-mgt.com.au/rsa_alg.html#signpkcs1 Dave | Moderator - Sat, Apr 21 2012 04:46 GMTHi, nice posting,. So when we use CTR mode in AES, is there any things which we need to take care ourselves for this padding.
Suresh P | INDIA - Fri, Jun 22 2012 08:06 GMTSuresh P: With CTR mode you *must* make sure the IV is unique for *every* message you send with a given key. If you ever send two messages with the same IV and key, it is a trivial matter for someone to break the ciphertext.
You can safely use a 16-byte random IV each time because the probability of two 16-byte random values clashing is infinitesimal, providing the generator is OK. Another way is to use a counter that *always* increments even if the system crashes (this is actually very hard to do in practice). Or you can combine say a 32-bit counter with some random bits.
Dave | Moderator - Fri, Jun 22 2012 09:06 GMT