Skip to content

Instantly share code, notes, and snippets.

@austra
Created January 31, 2019 21:04
Show Gist options
  • Save austra/b633d94f0c01636a04f23f49c1eaa353 to your computer and use it in GitHub Desktop.
Save austra/b633d94f0c01636a04f23f49c1eaa353 to your computer and use it in GitHub Desktop.
Basic Encrypt/Decryption
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