Skip to content

Instantly share code, notes, and snippets.

@benders
Created May 15, 2009 18:59
Show Gist options
  • Save benders/112379 to your computer and use it in GitHub Desktop.
Save benders/112379 to your computer and use it in GitHub Desktop.
Example symmetric encryption (Snipped from phedders / prh-rlib)
require 'openssl'
def simple_decrypt(iv,crypted,password)
crypt = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
crypt.decrypt
crypt.key = Digest::SHA256.digest(password)
crypt.iv = iv
data = crypt.update(crypted)
data << crypt.final rescue nil
end
def simple_encrypt(iv,data,password)
crypted = {}
crypt = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
crypt.encrypt
crypt.key = Digest::SHA256.digest(password)
crypt.iv = crypted[:iv] = iv ? iv : crypt.random_iv
crypted[:data] = crypt.update(data)
crypted[:data] << crypt.final
return crypted
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment