Created
October 22, 2022 08:37
-
-
Save xxjapp/8e641c25b9cfdcefb9c27a0bd7832aaf to your computer and use it in GitHub Desktop.
ruby aes 128 ecb example
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' | |
def aes_128_ecbe key, data | |
cipher = OpenSSL::Cipher.new('AES-128-ECB') | |
cipher.encrypt # this needs to be called first to make encryption work for AES-128-ECB | |
cipher.key = key | |
cipher.padding = 1 | |
encrypted = cipher.update(data) + cipher.final | |
Base64.strict_encode64 encrypted | |
end | |
# setup some input parameters | |
data = 's=0&account=account001' | |
key = '15EB22B8665071BA' # define shared secret key here | |
puts aes_128_ecbe key, data | |
# => | |
# 0Yhs7CbR9jsrnClszW/LsUUok3kOdYpTmXhAgxLItWc= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment