Created
January 31, 2019 21:04
-
-
Save austra/b633d94f0c01636a04f23f49c1eaa353 to your computer and use it in GitHub Desktop.
Basic Encrypt/Decryption
This file contains 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 aes256_cbc_encrypt(key, data, iv) | |
if !(key.bytesize === 32) | |
key = Digest::SHA256.digest(key) | |
end | |
if !(iv.bytesize === 16) | |
iv = Digest::MD5.digest(iv) | |
end | |
#padding = 16 - (data.length % 16) | |
#data += (padding.chr * padding) | |
cipher = OpenSSL::Cipher::AES256.new(:CBC) | |
cipher.encrypt | |
cipher.iv = iv | |
cipher.key = key | |
cipher.update(data) + cipher.final | |
end | |
def aes256_cbc_decrypt(key, data, iv) | |
if !(key.bytesize === 32) | |
key = Digest::SHA256.digest(key) | |
end | |
if !(iv.bytesize === 16) | |
iv = Digest::MD5.digest(iv) | |
end | |
cipher = OpenSSL::Cipher::AES256.new(:CBC) | |
cipher.decrypt | |
cipher.iv = iv | |
cipher.key = key | |
cipher.update(data) + cipher.final | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment