Blowfish encryption difference between perl and ruby

Why is there a difference in blowfish encryption between Crypt :: CBC (perl) and OpenSSL (ruby)?

Perl

use Crypt::CBC; my $cipher = Crypt::CBC->new( -key => 'length32length32length32length32', -cipher => 'Blowfish' ); my $ciphertext = $cipher->encrypt_hex('test'); # ciphertext is 53616c7465645f5f409c8b8eb353823c06d9b50537c92e19 

ruby

 require "rubygems" require "openssl" cipher = OpenSSL::Cipher::Cipher.new("bf-cbc") cipher.encrypt cipher.key = "length32length32length32length32" result = cipher.update("test") << cipher.final ciphertext = result.unpack("H*").first # ciphertext is 16f99115a09e0464 

Crypt :: CBC seems to add Salted__ to the default output. Can you explain what is happening, is it so different from them? Is there a way to make OpenSSL behave similarly to Crypt :: CBC?

+7
source share
2 answers

Crypt :: CBC (perl) uses its own method to randomize salt and initialization vector. Plus, in the case of Blowfish uses a key length of 56, as described above.

Using perl code from your example:

Perl

 use Crypt::CBC; my $cipher = Crypt::CBC->new( -key => 'length32length32length32length32', -cipher => 'Blowfish' ); my $ciphertext = $cipher->encrypt_hex('test'); # 53616c7465645f5f409c8b8eb353823c06d9b50537c92e19 

To decode this with ruby ​​(OpenSSL), a little tweaking is required to extract the key and initialization vector:

Ruby

 require 'openssl' # Hex string to decode(from above) string = '53616c7465645f5f409c8b8eb353823c06d9b50537c92e19' # Pack Hex string = [string].pack('H*') # Some Config pass = 'length32length32length32length32' key_len = 56; iv_len = 8; desired_len = key_len + iv_len; salt_re = /^Salted__(.{8})/ #Extract salt salt = salt_re.match(string) salt = salt.captures[0] data = ''; d = ''; while (data.length < desired_len) d = Digest::MD5::digest("#{d}#{pass}#{salt}"); data << d; end #Now you have extracted your key and initialization vector key = data.slice(0..key_len-1) iv = data.slice(key_len .. -1) # Trim string of salt string = string[16..-1] cipher = OpenSSL::Cipher::Cipher.new "bf-cbc" cipher.decrypt cipher.key_len = key_len cipher.key = key cipher.iv = iv puts cipher.update(string) << cipher.final # test 
+7
source

It turns out that the default key size has different values. OpenSSL defaults to 16, Crypt :: Blowfish defaults to 56.

You can override the key size in Crypt :: CBC by specifying -keysize => n , but unfortunately there is no way to override the key size in OpenSSL.

The Openssl library defaults to 16-byte Blowfish key sizes, so for Openssl compatibility, you might want to set -keysize => 16

http://metacpan.org/pod/Crypt::CBC

Perl (specify keys)

 my $cipher = Crypt::CBC->new( -key => 'length32length32length32length32', -keysize => 16, -cipher => 'Blowfish' ); 
+1
source

All Articles