Skip to content

Instantly share code, notes, and snippets.

@mfrederickson
Created March 27, 2019 18:22
Show Gist options
  • Save mfrederickson/51c9232fab5d050e970750fa074960db to your computer and use it in GitHub Desktop.
Save mfrederickson/51c9232fab5d050e970750fa074960db to your computer and use it in GitHub Desktop.
vanco nvp encrypt/decrypt in ruby aes256-ecb
# encrypt example in ruby
require 'base64'
require 'openssl'
require 'zlib'
key = 'ContactVancoForYourEncryptionKey'
plaintext = 'requesttype=efttransparentredirect&requestid=554433&clientid=ES12345&urltoredirect=http://www.testurl__.com'
deflated = Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(plaintext, Zlib::FINISH)
padding_needed = 16 - (deflated.length % 16)
padded = deflated + (padding_needed == 16 ? '' : ' ' * padding_needed)
c = OpenSSL::Cipher.new('aes-256-ecb')
c.encrypt
c.key = key
c.padding = 0
encrypted = c.update(padded) + c.final
encoded = Base64.urlsafe_encode64(encrypted)
# "VsQGYX2HJZqDsHNEANM0t4liMjdQX7pxfdzkWnCx_jkfxqkkFNHRZwTXau49lvIb8oroofXTHLvlpw-HgZVxnlbBFm5wEMm8QDJbt7tiGBDXrft-LEThW8X5MjJjtc3R"
# decrypt example in ruby
require 'base64'
require 'openssl'
require 'zlib'
key = 'ContactVancoForYourEncryptionKey'
encoded = "VsQGYX2HJZqDsHNEANM0t4liMjdQX7pxfdzkWnCx_jkfxqkkFNHRZwTXau49lvIb8oroofXTHLvlpw-HgZVxnlbBFm5wEMm8QDJbt7tiGBDXrft-LEThW8X5MjJjtc3R"
encrypted = Base64.urlsafe_decode64(encoded)
c = OpenSSL::Cipher.new('aes-256-ecb')
c.decrypt
c.key = key
c.padding = 0
decrypted = c.update(encrypted) + c.final
inflated = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(decrypted)
# "requesttype=efttransparentredirect&requestid=554433&clientid=ES12345&urltoredirect=http://www.testurl__.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment