Skip to content

Instantly share code, notes, and snippets.

@alfmatos
Created July 4, 2019 10:21
Show Gist options
  • Save alfmatos/4dcadf361792350499e08595579bfbb8 to your computer and use it in GitHub Desktop.
Save alfmatos/4dcadf361792350499e08595579bfbb8 to your computer and use it in GitHub Desktop.
Encrypt and decrypt dada using AES in rails
def aes_encrypt(key, data)
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.encrypt
aes.key = Digest::SHA256.digest(key)
aes.update(data) + aes.final
end
def aes_decrypt(key, encrypted_data)
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.decrypt
aes.key = Digest::SHA256.digest(key)
aes.update(encrypted_data) + aes.final
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment