Skip to content

Instantly share code, notes, and snippets.

@zainalmustofa
Last active February 7, 2023 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zainalmustofa/cecf6ab0d21e4507104189038686d846 to your computer and use it in GitHub Desktop.
Save zainalmustofa/cecf6ab0d21e4507104189038686d846 to your computer and use it in GitHub Desktop.
Ruby AES Encrypt using OpenSSL
require "openssl"
require 'base64'
KEY = "***************************************"
ALGORITHM = "AES-256-ECB"
def self.encrypt(message, iv)
cipher = OpenSSL::Cipher.new(ALGORITHM)
cipher.encrypt()
cipher.key = ENV["KEY"]
cipher.iv = iv
crypt = cipher.update(message) + cipher.final()
crypt_string = (Base64.encode64(crypt))
end
def self.decrypt(message, iv)
cipher = OpenSSL::Cipher.new(ALGORITHM)
cipher.decrypt()
cipher.key = ENV["KEY"]
cipher.iv = iv
tempkey = Base64.decode64(message)
cipher.update(tempkey) + cipher.final()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment