Last active
February 7, 2023 09:35
-
-
Save zainalmustofa/cecf6ab0d21e4507104189038686d846 to your computer and use it in GitHub Desktop.
Ruby AES Encrypt using OpenSSL
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
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