Blowfish Cipher Tutorials - Herong's Tutorial Examples - v2.04, by Herong Yang
Crypt::ECB Encryption with No Padding
A tutorial example is provided to show how to use Crypt::ECB to perform encryption with no padding. The plaintext must be multiples of 8-byte blocks.
Since the ECB (Electronic CodeBook) operation mode is so simple, Crypt::ECB does not need to offer many options. If we use it with Crypt::Blowfish, there are only two options:
With no padding, we can simply create a new Crypt::ECB object with two method calls:
$cipher = Crypt::ECB->new($keyStr, 'Blowfish'); $cipher->padding(PADDING_NONE);
To help us understanding how Crypt::ECB works without padding, I wrote the following example Perl script, Crypt-ECB-Blowfish-Encryption.pl:
#- Crypt-ECB-Blowfish-Encryption.pl
#- Copyright (c) 2015 HerongYang.com, All Rights Reserved.
use Crypt::ECB;
my ($keyHex, $plainHex) = @ARGV;
print("Crypt::ECB Encryption Test - 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_NONE);
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");
If you run this example script in a command window, you will get:
C:\Herong>perl Crypt-ECB-Blowfish-Encryption.pl Crypt::ECB Encryption Test - output in Hex: Secret Key (1122334455667788) Plaintext (0123456789abcdef) Creating Crypt::ECB with secret key ... Encrypting plaintext... Ciphertext (103248f0eea98e89) Decrypting ciphertext... Decrypted text (0123456789abcdef) Secret Key (1122334455667788) Padding (0)
The output seems to be correct. Crypt::ECB and Crypt::Blowfish modules are not giving me any errors.
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