# Need help with Decryption using blowfish CBC

**URL:** <https://www.ruby-forum.com/t/need-help-with-decryption-using-blowfish-cbc/139735>\
**Category:** Rails\
**Created:** [June 6, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/need-help-with-decryption-using-blowfish-cbc/139735 "2008-06-06T15:23:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrikShun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@FrikShun](https://www.ruby-forum.com/u/FrikShun)\
**Post date:** [June 6, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/need-help-with-decryption-using-blowfish-cbc/139735/1 "2008-06-06T15:23:50Z")

</div>

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:in`open’  
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:in`decrypt\_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:in`decrypt\_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:in`decrypt\_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  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![FrikShun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a87d85/32.png) [@FrikShun](https://www.ruby-forum.com/u/FrikShun)\
**Post date:** [June 6, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/need-help-with-decryption-using-blowfish-cbc/139735/2 "2008-06-06T15:31:35Z")

</div>

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
