CryptoSys API Library Manual

Padding

Before encrypting random-length plaintext with a block cipher algorithm in ECB or CBC mode it needs to be padded to an exact multiple of the block length.

There are many conventions used in practice, see our web page Using Padding in Encryption. It's up to you and your recipient which method you use, but you must agree on one method and use it consistently. If your data is always an exact multiple of the block length and the sender and the recipient agree then you can omit the padding string.

We recommend the convention from section 6.3 of RFC 3852 [CMS] (formerly RFC 3369 and RFC 2630), PKCS #5 [PKCS5] and PKCS #7 [PKCS7]; namely:

For a 64-bit block size: Append 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; namely, 8 bytes of value 0x08, 7 bytes of value 0x07, ..., 2 bytes of 0x02, or one byte of value 0x01. The length of the plaintext to be encrypted thus will be a multiple of 8 bytes and it will be possible to recover the message unambiguously from the decrypted ciphertext.

For a 128-bit block size (e.g. for AES), replace "8 bytes" in the above paragraph with "16 bytes" and replace "0x08" with "0x10", and reword accordingly.

See the functions PAD_BytesBlock, PAD_UnpadBytes, PAD_HexBlock and PAD_UnpadHex.

[Contents] [Index]

[HOME]   [NEXT: Stream ciphers...]

Copyright © 2001-9 D.I. Management Services Pty Ltd. All rights reserved.