Need help with Decryption using blowfish CBC

Hello all,

Hoping someone can help me out here. I’ve burned almost a week trying
to figure out how to decrypt
an image file that has been encrypted using Blowfish CBC.

I found some code on the net and have modified as follows:

require 'openssl'
require 'digest/sha1'
ivArr = [0x0D,

0x0E,
0x0A,
0x0D,
0x0F,
0x0A,
0x0C,
0x0E
]
iv = ivArr.pack(“cccccccc”)
bf = OpenSSL::Cipher::Cipher.new(“BF-CBC”)
key = Digest::SHA1.hexdigest(“fOyBtMaN4u2T”)
bf.decrypt
bf.key = key
bf.iv = iv
File.open(“C:\mapcrypt\MAP.TIFF”,‘wb’) do |enc|
File.open(“C:\mapcrypt\EncryptedMAP.TIFF”,“rb”) do |f|
size = File.size(“C:\mapcrypt\EncryptedMAP.TIFF”)
blocks = size / 8

    for i in 1..blocks
      r = f.read(8)
      cipher = bf.update(r)
      enc << cipher
    end

    if size%8 >0
      r = f.read((size%8))
      cipher = bf.update(r)
      enc << cipher
    end
  end
  enc << bf.final
end

However, this returns the error:
OpenSSL::CipherError: wrong final block length
from (irb):89:in final' from (irb):89 from (irb):72:inopen’
from (irb):72

I’ve also tried using the Blowfish library:
require ‘crypt/blowfish’
blowfish = Crypt::Blowfish.new(“fOyBtMaN4u2T”)
blowfish.decrypt_file(‘C:\mapcrypt\11225035.00X’, ‘MAP.TIFF’)
But this returns the error:
NoMethodError: undefined method %' for true:TrueClass from c:/ruby/lib/ruby/gems/1.8/gems/crypt-1.1.4/./crypt/ blowfish.rb:84:indecrypt_pair’
from c:/ruby/lib/ruby/gems/1.8/gems/crypt-1.1.4/./crypt/
blowfish.rb:82:in downto' from c:/ruby/lib/ruby/gems/1.8/gems/crypt-1.1.4/./crypt/ blowfish.rb:82:indecrypt_pair’
from c:/ruby/lib/ruby/gems/1.8/gems/crypt-1.1.4/./crypt/
blowfish.rb:104:in decrypt_block' from c:/ruby/lib/ruby/gems/1.8/gems/crypt-1.1.4/./crypt/cbc.rb: 62:indecrypt_stream’
from c:/ruby/lib/ruby/gems/1.8/gems/crypt-1.1.4/./crypt/cbc.rb:
99:in `decrypt_file’
from (irb):93

I’m using ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Thanks in advance for your help,

Allen
[email protected]

On 6 Jun 2008, at 14:23, FrikShun wrote:

Hello all,

File.open(“C:\mapcrypt\MAP.TIFF”,‘wb’) do |enc|
File.open(“C:\mapcrypt\EncryptedMAP.TIFF”,“rb”) do |f|
size = File.size(“C:\mapcrypt\EncryptedMAP.TIFF”)
i think you’re overcomplicating.

enc << bf.update(f.read)
should work ok, without all that fiddling you’re doing

Fred