Method: ActiveMerchant::Billing::MitGateway#decrypt

Defined in:
lib/active_merchant/billing/gateways/mit.rb

#decrypt(val, keyinhex) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_merchant/billing/gateways/mit.rb', line 37

def decrypt(val, keyinhex)
  # Splits the first 16 bytes (the IV bytes) in array format
  unpacked = val.unpack('m')
  iv_base64 = unpacked[0].bytes.slice(0, 16)
  # Splits the second bytes (the encrypted text bytes) these would be the
  # original message
  full_data = unpacked[0].bytes.slice(16, unpacked[0].bytes.length)
  # Creates the engine
  engine = OpenSSL::Cipher.new('aes-128-cbc')
  # Set engine as decrypt mode
  engine.decrypt
  # Converts the key from hex to bytes
  engine.key = [keyinhex].pack('H*')
  # Converts the ivBase64 array into bytes
  engine.iv = iv_base64.pack('c*')
  # Decrypts the texts and returns the original string
  engine.update(full_data.pack('c*')) + engine.final
end