Created
July 4, 2019 10:21
-
-
Save alfmatos/4dcadf361792350499e08595579bfbb8 to your computer and use it in GitHub Desktop.
Encrypt and decrypt dada using AES in rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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