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

12.20.2005
| 20147 views |
  • submit to reddit
        // Quote:
technoweenie 
http://www.bigbold.com/snippets/posts/show/576
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 = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.encrypt
# 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 << c.final
puts "encrypted: #{e}\n"
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.decrypt
c.key = key
c.iv = iv
d = c.update(e)
d << c.final
puts "decrypted: #{d}\n"
    

Comments

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

Careful, this is not entirely correct - see http://snippets.dzone.com/posts/show/576 If you're just looking to do basic(and secure) AES encryption, check out my Gibberish gem a https://github.com/mdp/gibberish - Help me stop the practice of bad encryption habits in Ruby land.