Blowfish decryption in Ruby, revisited
Posted by Daniel Brahneborg on 2007 March 14
I wrote earlier about my struggles to get decryption to work in Ruby. Today I got it working, with good performance.
The solution was to write a Ruby extension in C, calling the Blowfish and AES functions in the OpenSSL library directly. Converting from Ruby types to C types and back is easy, so the process of figuring out how to do it, writing the code and tweaking it until the test suite passed again, took less than a day. Now time is measured in seconds again.
The thing that surprised me the most was when I changed decryption of file names and other short strings, to also use the C version. This gave yet another performance boost, cutting the time for the test suite from 5-6 seconds to less than 1. If nothing else, it shows that Ruby needs to get faster.
Andra bloggar intressant om: programmering, ruby, kryptering.

supersaurus said
I don’t know exactly what you are doing but below is what I did. using the module below to extend an EzCrypto::Key I can encrypt a 20MB binary file in 3-4 seconds instead of 1 hour+ as with pure ruby solutions.
module EzCrypto_extension
# for both methods
# src can be a file, a StringIO
# or anything that responds to a read
# target can be a string, a file
# or anything that responds to “aes-128-cbc”) # blowfish, etc also work
# add methods to the key
key.extend(EzCrypto_extension)
# use the new methods
key.decrypt_stream(infile,outfile)
# or
key.encrypt_stream(infile,outfile)
supersaurus said
well, I don’t know what happened there, but part of the post disappeared. here is the module again:
#class EzCrypto::Key
module EzCrypto_extension
# for both methods
# src can be a file, a StringIO
# or anything that responds to a read
# target can be a string, a file
# or anything that responds to
supersaurus said
well, that didn’t work, let’s try tags
#class EzCrypto::Key
module EzCrypto_extension
# for both methods
# src can be a file, a StringIO
# or anything that responds to a read
# target can be a string, a file
# or anything that responds to
supersaurus said
I guess your blogware just doesn’t like ruby code, sorry, that’s it for me.
Daniel Brahneborg said
Thanks, I guess there is some code that actually works. If you have the time, please email me that code snippet. It would be great not having to use C code.
Jim said
I would also be very interested in getting this too work.
ezcrypto fails on files bigger that 256 bytes (well it does how I’m doing it):(
And the crypto gem is way too slow for me.
I will probably have to embed my C code too, not an ideal solution.
Allen Taylor said
Did you guys every get a working solution for ruby blowfish encryption? Is so, can you email me what you found?
Thanks,
Allen
Daniel Brahneborg said
No, I didn’t. We stayed with the C version.
Sebastian said
Hi
I would love to see the c-extension you wrote!
I have to do encryption in a module and have to create a c extension to hide the key!
The link above only gets me to a page about writing c-extensions…
Best regards
Sebastian
Daniel Brahneborg said
Sebastian: If you follow the instructions on that page and make sure you understand what is going on, you’ll be done in no time. For me, as I said, it took less than a day.
Still, I’ll check if I can post the code, but don’t count on it.