Photo by Alex Motoc on Unsplash

So What Is PKCS#7?

--

A symmetric key block cipher, such as AES and DES, uses a defined block size -and which stores a given number of bytes. These blocks are typically either 64-bits (8 bytes) or 128 bits (16 bytes). As we cannot fill all of the blocks, we must pad the last block. PKCS #7 (Cryptographic Message Syntax) is a standard padding method that determines the number of padding bytes and then ads that as a value. For example, for a 128-bit block size, and if we have “testing”, then there are seven bytes (for ASCII coding) that represent the data, and we then have 9 (0x09) padding values. The padding block becomes:

74657374696e67090909090909090909

and “testing1” as:

74657374696e67310808080808080808

For a 64-bit block, we only have one padding bytes, thus the padding value is:

74657374696e6701

For “testing1”, we will require another block, and with eight padding bytes:

74657374696e67310808080808080808

The code is then [here]:

from cryptography.hazmat.primitives import padding
import sys
import binascii

size=128
message="abc"

if (len(sys.argv)>1):
message=str(sys.argv[1])

if (len(sys.argv)>2):
size=int(sys.argv[2])

print ("Message: ",message)
print ("Block size: ",size)

message=message.encode()
padder =…

--

--

Prof Bill Buchanan OBE FRSE
ASecuritySite: When Bob Met Alice

Professor of Cryptography. Serial innovator. Believer in fairness, justice & freedom. Based in Edinburgh. Old World Breaker. New World Creator. Building trust.