DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

allen has posted 6 posts at DZone. View Full User Profile

Ruby Encrypt/Decrypt

  • submit to reddit
        // Quote:
hi all, I don't want repeat this snippets at all.
but, without search and under a weak tag list, hard to find what i want exactly

require 'openssl'
require 'digest/sha1'
c ="aes-256-cbc")
# your pass is what is used to encrypt/decrypt
c.key = key = Digest::SHA1.hexdigest("yourpass")
c.iv = iv = c.random_iv
e = c.update("crypt this")
e <<
puts "encrypted: #{e}\n"
c ="aes-256-cbc")
c.key = key
c.iv = iv
d = c.update(e)
d <<
puts "decrypted: #{d}\n"


Snippets Manager replied on Tue, 2011/04/05 - 10:44am

Careful, this is not entirely correct - see If you're just looking to do basic(and secure) AES encryption, check out my Gibberish gem a - Help me stop the practice of bad encryption habits in Ruby land.