Skip to content

Instantly share code, notes, and snippets.

@xxjapp
Created October 22, 2022 08:37
Show Gist options
  • Save xxjapp/8e641c25b9cfdcefb9c27a0bd7832aaf to your computer and use it in GitHub Desktop.
Save xxjapp/8e641c25b9cfdcefb9c27a0bd7832aaf to your computer and use it in GitHub Desktop.
ruby aes 128 ecb example
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