Software Development, Agile Practices, Lean Thinking, Music
[ start | index | login or register ]
start > 2007-04-27 > 1

2007-04-27 #1

Created by brandon. Last edited by brandon, 4 years and 287 days ago. Viewed 203 times. #4
[diff] [history] [edit] [rdf]
labels
attachments

Encryption with Ruby

The code below simply works… all I had to do was install the EzCrypto Ruby gem.
$ gem install ezcrypto

How much work would it have been to accomplish the same three symmetric encryption methods in Java using the JCE? :)

encrypter_test.rb

require 'test/unit'
require 'encrypter'

class EncrypterTest < Test::Unit::TestCase def test_encrypt_aes_128 encrypted = Encrypter.new.encrypt_with_aes_128("private stuff") assert_equal("\355\317\242F\202*\230V^\333\320M\f\231\210\201", encrypted) end def test_encrypt_blowfish encrypted = Encrypter.new.encrypt_with_blowfish("private stuff") assert_equal("\241g\266\261\342\020\376Va\006~\004\346\252H\225", encrypted) end def test_encrypt_desede encrypted = Encrypter.new.encrypt_with_desede("private stuff") assert_equal("\367\326\276\353c}t\301\267A\340\027\227\356fK", encrypted) end end

encrypter.rb
require 'rubygems'
require 'ezcrypto'

class Encrypter @@secret_key = "rubyw4y" @@salt = "1981" def encrypt_with_aes_128(plainText) crypto_key = EzCrypto::Key.with_password(@@secret_key, @@salt) # default algorithm is AES 128 cipher_text = crypto_key.encrypt(plainText); end def encrypt_with_blowfish(plainText) crypto_key = EzCrypto::Key.with_password(@@secret_key, @@salt, :algorithm=>'blowfish') cipher_text = crypto_key.encrypt(plainText); end def encrypt_with_desede(plainText) crypto_key = EzCrypto::Key.with_password(@@secret_key, @@salt, :algorithm=>'des3') cipher_text = crypto_key.encrypt(plainText); end end

It is worth mentioning that this piggy backs off of OpenSSL for the algorithm implementations.

If you're interested in more info check out >>http://ezcrypto.rubyforge.org .

no comments | post comment
Describe here what your SnipSnap is about!

Configure this box!

  1. Login in
  2. Click here: snipsnap-portlet-2
  3. Edit this box
www.brandonburk.com | Copyright 2008 Brandon N. Burk