I would like to know in the following code if PKCS#5 padding is added ? If not how to add ?
$message = "insert plaintext message here";
$iv = pack('H*', 'insert hex iv here');
$key = pack('H*', 'insert hex key here');
$enc = mcrypt_encrypt(MCRYPT_DES, $key, $message, MCRYPT_MODE_CBC, $iv);
echo bin2hex($enc);
I also want to create a PHP code to decrypt a string created with DES/CBC/PKCS5Padding. I think the above mentioned code can be modified to get a decryption.
The important thing for me is to get the PKCS#5 Padding and Unpadding script.
I have found some scripts and modified them below , check if now the PKCS#5 Padding is done .
No, it is not added. Unfortunately PHP / mcrypt uses zero padding until the message is N times the block size.
To add PKCS#5 padding, use the formula:
Where
l
is the message length,b
is the block size and%
is the remainder operation. Then addp
bytes with the valuep
to the end before performing the encryption.