Blowfish Cipher Tutorials - Herong's Tutorial Examples - v2.04, by Herong Yang
Crypt::ECB Auto Padding
Padding option is described for the Crypt::ECB encryption process. It offers only 1 padding option called PADDING_AUTO, which uses the standard PKCS#5 algorithm.
In previous tutorials, we have learned how to use Crypt::ECB without padding. In order to encrypt plaintext of any size, we need to use padding.
Currently, Crypt::ECB only offers one padding option, PADDING_AUTO, which uses the PKCS#5 padding algorithm: The last block is padded with the number of bytes that should be truncated. So, if blocksize is 8, then "0A0B0C" will be padded with "05", resulting in "0A0B0C0505050505". If the final block is a full block of 8 bytes, then a whole block of "0808080808080808" is appended.
Here is how to create a create a new Crypt::ECB object with PADDING_AUTO:
$cipher = Crypt::ECB->new($keyStr, 'Blowfish'); $cipher->padding(PADDING_AUTO);
To help us understanding how Crypt::ECB works with PADDING_AUTO, I wrote the following example Perl script, Crypt-ECB-Blowfish-Auto-Padding.pl:
#- Crypt-ECB-Blowfish-Auto-Padding.pl #- Copyright (c) 2015 HerongYang.com, All Rights Reserved. use Crypt::ECB; my ($keyHex, $plainHex) = @ARGV; print("Crypt::ECB Auto Padding - output in Hex:\n"); $keyHex = "1122334455667788" unless $keyHex; $plainHex = "0123456789abcdef" unless $plainHex; $keyStr = pack("H*", $keyHex); $plainStr = pack("H*", $plainHex); print(" Secret Key ($keyHex)\n"); print(" Plaintext ($plainHex)\n"); print("Creating Crypt::ECB with secret key ...\n"); $cipher = Crypt::ECB->new($keyStr, 'Blowfish'); $cipher->padding(PADDING_AUTO); print("Encrypting plaintext...\n"); $cipherStr = $cipher->encrypt($plainStr); $cipherHex = unpack("H*", $cipherStr); print(" Ciphertext ($cipherHex)\n"); print("Decrypting ciphertext...\n"); $decryptStr = $cipher->decrypt($cipherStr); $decryptHex = unpack("H*", $decryptStr); print(" Decrypted text ($decryptHex)\n"); $keyStr = $cipher->key(); $keyHex = unpack("H*", $keyStr); print(" Secret Key ($keyHex)\n"); $padding = $cipher->padding(); print(" Padding ($padding)\n"); print("Getting Padded Plaintext...\n"); $cipher = Crypt::ECB->new($keyStr, 'Blowfish'); $cipher->padding(PADDING_NONE); $decryptStrPadded = $cipher->decrypt($cipherStr); $decryptHexPadded = unpack("H*", $decryptStrPadded); print(" Padded Plaintext ($decryptHexPadded)\n");
Now run this example script with a full block, you will see an extra padding block is added:
C:\herong >perl Crypt-ECB-Blowfish-Auto-Padding.pl \ 1122334455667788 0123456789abcdef Crypt::ECB Auto Padding - output in Hex: Secret Key (1122334455667788) Plaintext (0123456789abcdef) Creating Crypt::ECB with secret key ... Encrypting plaintext... Ciphertext (103248f0eea98e891c08749f99c934a4) Decrypting ciphertext... Decrypted text (0123456789abcdef) Secret Key (1122334455667788) Padding (1) Getting Padded Plaintext... Padded Plaintext (0123456789abcdef0808080808080808) ---------------- padding
Now run this example script with a half block, you will see the plaintext is padded to a full block.
C:\herong >perl Crypt-ECB-Blowfish-Auto-Padding.pl \ 1122334455667788 01234567 Crypt::ECB Auto Padding - output in Hex: Secret Key (1122334455667788) Plaintext (01234567) Creating Crypt::ECB with secret key ... Encrypting plaintext... Ciphertext (637d6d0f24ef5a23) Decrypting ciphertext... Decrypted text (01234567) Secret Key (1122334455667788) Padding (1) Getting Padded Plaintext... Padded Plaintext (0123456704040404) -------- padding
Table of Contents
Installing Crypt::ECB 1.45 with ActivePerl
Crypt::ECB Encryption with No Padding
Crypt::ECB Encryption Test Cases
OpenSSL "enc -bf-ecb" for Blowfish/ECB Encryption
OpenSSL "enc -bf-cbc" for Blowfish/CBC Encryption
OpenSSL "enc -bf-cfb" for Blowfish/CFB Encryption
OpenSSL "enc -bf-ofb" for Blowfish/OFB Encryption