0

Ah, the joys of legacy apps.

I'm migrating an old Kohana 3 PHP app to Ruby/Rails. A field has been stored in the database, ecrypted using Kohana's "Encrypt" module. http://kohanaframework.org/3.0/guide/api/Kohana_Encrypt

When migrating over the data, I need to decrypt these values, then re-encrypt them using something else.

However, my Mcrypt knowledge is nil and I can't seem to get it decrypted.

What I Know:

  • Kohana is setup to use -- cipher: MCRYPT_RIJNDAEL_128, mode: MCRYPT_MODE_NOFB, and I have the key
  • Kohana base64 encode's the data before storing
  • I cannot find the initialization vector anywhere. It looks like Kohana uses a custom IV for each piece of data?
  • I have a Ruby gem to use for Mcrypt. https://github.com/kingpong/ruby-mcrypt It takes a second parameter - "cbc" is the example given. Not sure what this is for or where I can find that information in this legacy Kohana project.

Any ideas on how to get this data decrypted? Do I even have all the information I need? I'm a little lost...

Thanks in advance.

1
  • In the decode function the iv is defined as $iv = substr($data, 0, $this->_iv_size);. So why don't you just use Kohanas Encrypt class and use it for decryption? ;) Or if you have tried it, what errors do you get?
    – kero
    Aug 23, 2013 at 9:00

2 Answers 2

1

The trick for me was to determine the IV size using PHP's "mcrypt_get_iv_size" for my cipher and mode. Second was to flip the mode in the Mcrypt gem from cfb to nofb (the mode that had been used in the Kohana setup)

Ported over the Koahana decrypt method and it worked like a charm.

data = Base64.strict_decode64(data)
throw "INVALID DATA" if data.empty?

# Extract the initialization vector from the data
iv = data.slice(0, iv_size)

throw "IV IS NOT THE EXPECTED SIZE" if iv.size != iv_size

# Remove the iv from the data
data = data [16..(data.size)]

puts Mcrypt.new(cipher, :nofb, key, iv).decrypt(data)
0

If I were you I would make a new field in the database of the Kohana app and then make a script that utilizes the Kohana decrypt function to decrypt the value and store them in the new field.

When port you database over to ruby and create a script to encrypt those values back to the way you want to encrypt them in Ruby. And delete the new field after that.

This will make the migrating a lot less painfull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.