Blog of Heath

Ruby on Rails Plugin: string_encryption

Posted in Ruby, Ruby on Rails by heathanderson on November 23, 2009

This is a very simple Ruby on Rails plugin that allows easy encryption of strings. For the sake of simplicity there no customization. The only cipher used is ‘des-ede3-cbc’ (Triple DES using Cipher Block Chaining) . See my post on encrypting/decrypting a string with Ruby for more information. First install the plugin:

 script/plugin install git://github.com/handerson/string_encryption.git

Next we need to add an encryption key as the constant ENV['STRING_ENCRYPTION_KEY']. To do this just run

 script/generate encryption_key

or you could set
ENV['STRING_ENCRYPTION_KEY'] = "somekey"

where “somekey” is your key in environment.rb.

Now encryption/decryption is easy.

Encryption:

"Test".encrypt  #=> "NDFkZTc5NDEyNTg1MzdiZPzBrxZz5aoN%0A"

Decryption:
"NDFkZTc5NDEyNTg1MzdiZPzBrxZz5aoN%0A".decrypt  #=> "Test"

Code at Github:
http://github.com/handerson/string_encryption

Advertisement

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 )

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

Follow

Get every new post delivered to your Inbox.