Skip to content

Instantly share code, notes, and snippets.

@sdogruyol
Created November 24, 2015 08:29
Show Gist options
  • Save sdogruyol/ce737a84af194edd668d to your computer and use it in GitHub Desktop.
Save sdogruyol/ce737a84af194edd668d to your computer and use it in GitHub Desktop.
require 'openssl'
require 'base64'
# AES 128-CBC with No Padding
data = '3470060503201504081141291105065707769122253042568918000000000000'
key = '584F73BE5AD6E6CF47F506784353C55E'
iv = '00000000000000000000000000000000'
cipher = OpenSSL::Cipher::AES.new(128, :CBC) #=> #<OpenSSL::Cipher::AES:0x007fb794a27a80>
cipher.encrypt #=> #<OpenSSL::Cipher::AES:0x007fb794a27a80>
cipher.key = [key].pack "H*" #=> "XOs\xBEZ\xD6\xE6\xCFG\xF5\x06xCS\xC5^"
cipher.iv = [iv].pack "H*" #=> "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
cipher.padding = 0 #=> 0
encrypted_data_byte_string = cipher.update data + cipher.final #=> "\xDBH\x9C\x8E\xFDO|\xC0<;\x83\x89\xAD\xA0<\xE7s\xB0\xE9\xED\xE5\vW\x05|\xF4\x98gP\x9F\xD1Q\xBF\xF2%\xD3a\x98Y\xD4kvserT\xBC\xB4\xFC\xD9\xFCpi\x0F\xA9\xC6D\x01~j2\t\x067"
encrypted_data_base64 = Base64.encode64 encrypted_data_byte_string #=> "20icjv1PfMA8O4OJraA853Ow6e3lC1cFfPSYZ1Cf0VG/8iXTYZhZ1Gt2c2Vy\nVLy0/Nn8cGkPqcZEAX5qMgkGNw==\n"
encrypted_data_hex = encrypted_data_byte_string.unpack("H*")[0]
puts "Encrypted data as base64: #{encrypted_data_base64}"
puts "Encrypted data as hex: #{encrypted_data_hex}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment