Skip to content

Instantly share code, notes, and snippets.

@dramsay
Created October 13, 2008 20:46
Show Gist options
  • Save dramsay/16597 to your computer and use it in GitHub Desktop.
Save dramsay/16597 to your computer and use it in GitHub Desktop.
require 'openssl'
module Encryption
def encrypt(data, password, salt)
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.encrypt
cipher.pkcs5_keyivgen(password, salt)
encrypted_data = cipher.update(data)
encrypted_data << cipher.final
end
def decrypt(encrypted_data, password, salt)
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.decrypt
cipher.pkcs5_keyivgen(password, salt)
data = cipher.update(encrypted_data)
data << cipher.final
end
end
# Note: you will need to generate the salt and use the same salt and password for both encryption
# and decryption
#
# sample salt generator below
#
# def generate_salt
# [rand(2**64 - 1)].pack("Q")
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment