Basic tech stuff

Programming and Linux administration

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 om: , , .

Advertisements

10 Responses to “Blowfish decryption in Ruby, revisited”

  1. 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)

  2. 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

  3. 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

  4. supersaurus said

    I guess your blogware just doesn’t like ruby code, sorry, that’s it for me.

  5. 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. šŸ™‚

  6. 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.

  7. 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

  8. No, I didn’t. We stayed with the C version.

  9. 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

  10. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: