Decryption of payment data using Visa Checkout

I receive information from Visa Checkout in an encrypted format. The manual on its website contains the following instructions:

First, you must decrypt the dynamic key (encKey), and then use the decrypted value of the dynamic key to decrypt the payload of the payment data (encPaymentData).

Follow these four steps to decrypt encKey:

  • Base64-decode encKey.
  • Delete the first 32 bytes of the decoded value. This is HMAC (Hash Message Authentication Code). Calculate SHA-256 HMAC
    the rest of the decoded data using your shared secret API and compare it
    to HMAC from the first 32 bytes.
  • The next 16 bytes should be deleted and used as IV (Initialization Vector) for the decryption algorithm.
  • Decrypt the remaining data using AES-256-CBC, IV from step 3 and the SHA-256 hash of your secret API.

Follow these four steps to decrypt encPaymentData using decrypted encKey:

  • Base64-decode encPaymentData.
  • Delete the first 32 bytes of the decoded value. This is HMAC. Calculate the HMAC SHA-256 for the rest of the decoded data using decKey decrypt and compare it with the HMAC from the first 32 bytes.
  • The next 16 bytes should be deleted and used as IV for the decryption algorithm.
  • Decrypt the rest of the encPaymentData payload using AES-256-CBC, IV from step 3, and the SHA256 hash of the decrypted encKey.

I tried using ColdFusion, but I lost a bit of encryption problems and could not fix the code. Below I have what is required. I got stuck in steps 3 and 4 where they say comparing it and then decrypting it. Can someone point out what can be done to fix this?

enckey:

2M2WWOD4wANsGwWTmPqQIQYdz9WPwgiR0ntJHGaxm23b5a1sWUtCBcUQUMMtc9hvcYavJ6yqPgETOGZgDOdd9qjDwIb2aV9DLZT1iIcB3zNN5Ddhxd9iiui6TAlJxU/O 

encPaymentData:

 X2TXp0ZmwHrtfzSP5TPjUOjdZb0rjsHeDSqr8TwIF/VR8sMQhWN5hP4IRhQxWT CZcQhxZoUHP 0g/E/ot sjREAJ8YQf7c7jSzKsXRH/wrew5rQit2wJBlVSSZ YoLeIHbLrTz CfIoFv09hixl7ff27u0YCyV0zjP5vNfCBEIwfqyriqwXK2J QEOxQiKzDUW4br3o1t31aymCQC9eBBpoVKjFfSKlNXM9QEdNZBcLMZ8Wlv8lF/ua bnwshbM9u7Uhudqvut94RZEW NzkRD8M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zr3D56VpDBeDeQjk3mc/0zmuFAPEXoAQoQKfD6HEuajvWJebQ6QIPgA TshqsnPlktbpftr4lsuB1tHS/W8D7SYVFMC/Kxy9QuYWs0cmRTtzfWEKIRHeDElOTQCX5JB5PgzVhhi5kYTi488Ba8j4zvNUw55hEoMxONYO7eMjJosmNjULsT492LGw3EfAgmgx9h3yFLQRZgfylg0h4PfLlcPOAdsnVX9/yLElD xu7Atwc4S7pBWTHvwue7PpRvWpTeqkU5sqiX4KcV5x8rk mBtxm48a8fsmp GNf 4IjwXu9cQaU9WLipiEnkqFsYo7/aAsmmKWBETyQg9BFXYK 165vrzSX8WTsv6ZZDnVjcE1n4Ov8Jl2cnAigoQbB0ROPpIRzZ3zH2diUv1vzlSuh9gbEJf3uQRKlYRVUbpboC0RbQ/7jgznfJAWyLykyDQ0EB8fVEOtbP1l4JEz39QwAU18ph3btnWWuKEV4 ghYvNG4m1DYntSF57s2ajRS6rPtR oYvGjrJL9zbHBhKHlfkIPC0TKotOCi96mqpikbBEfIZSomHxYgDwYCSvt60zaDIjlBxZ1UBdK JL0554Wia9W3Wg91bmYS9Q4SXMT8r4xGYB7OutEV24n7p088rVm/w2SZSiqlLqai539k6WGkzEQf19ytPtIE81a N z7aijTjy 7FCuVPF90svI5/NoGpSINqv84HUcMU71BvXUIT53Ea6CCpiWvvOPpo/XZar44emlIG0UgeB kfP6C6sis= 

Secret code:

 zRf7WZ3nM7ON{U0E6J5S} KpVm@k2ReDyq #1lG9go 

CF Code:

 <cfset str = "2M2WWOD4wANsGwWTmPqQIQYdz9WPwgiR0ntJHGaxm23b5a1sWUtCBcUQUMMtc9hvcYavJ6yqPgETOGZgDOdd9qjDwIb2aV9DLZT1iIcB3zNN5Ddhxd9iiui6TAlJxU/O"> <cfset tobas = tobase64(str)> <cfset getFirst32bytes = Left(tobas,32)> <cfset tobas2 = RemoveChars(tobas,1,32)> <cfdump var="#tobas2#"> <cfset key = "zRf7WZ3nM7ON{U0E6J5S} KpVm@k2ReDyq ##1lG9go"> <cfset x = hmac("#tobas2#","#key#","HMACSHA256")> <cfset y = hmac("#getFirst32bytes#","#key#","HMACSHA256")> <cfset decalgo = Left(x,16)> <cfset decremainingData = RemoveChars(x,1,16)> <cfset getDec = Decrypt(decalgo,"#key#","AES")> <cfdump var="#x#"><br> <cfdump var="#y#"><br> <cfdump var="#decalgo#"> <cfdump var="#decremainingData#"> <cfdump var="#getDec#"> 

This is the java example they have on their site:

 private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; private static final String HASH_ALGORITHM = "SHA-256"; private static final String HMAC_ALGORITHM = "HmacSHA256"; private static final int IV_LENGTH = 16, HMAC_LENGTH = 32; private static final Charset utf8 = Charset.forName("UTF-8"); private static final Provider bcProvider; static { bcProvider = new BouncyCastleProvider(); if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { Security.addProvider(bcProvider); } } private static byte[] decrypt(byte[] key, byte[] data) throws GeneralSecurityException { byte[] decodedData = Base64.decode(data); if (decodedData == null || decodedData.length <= IV_LENGTH) { throw new RuntimeException("Bad input data."); } byte[] hmac = new byte[HMAC_LENGTH]; System.arraycopy(decodedData, 0, hmac, 0, HMAC_LENGTH); if (!Arrays.equals(hmac, hmac(key, decodedData, HMAC_LENGTH, decodedData.length– HMAC_LENGTH))) { throw new RuntimeException("HMAC validation failed."); } byte[] iv = new byte[IV_LENGTH]; System.arraycopy(decodedData, HMAC_LENGTH, iv, 0, IV_LENGTH); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, bcProvider); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(hash(key), "AES"), new IvParameterSpec(iv)); return cipher.doFinal(decodedData, HMAC_LENGTH + IV_LENGTH, decodedData.length– HMAC_LENGTH– IV_LENGTH); } private static byte[] hash(byte[] key) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); md.update(key); return md.digest(); } private static byte[] hmac(byte[] key, byte[] data, int offset, int length) throws GeneralSecurityException { Mac mac = Mac.getInstance(HMAC_ALGORITHM, bcProvider); mac.init(new SecretKeySpec(key, HMAC_ALGORITHM)); mac.update(data, offset, length); return mac.doFinal(); } 
+5
source share
1 answer

The important thing to understand the code example is that it refers to bytes. Your CF code uses characters. This may seem like a trivial difference, but these are completely different things that will give very different results. For successful decryption, you need to work with bytes (or binary) of these strings, and not with characters.

Although you can control binary arrays using basic CF functions such as arraySlice () , the syntax becomes a little cumbersome / awkward at times. The reason is because binary arrays are a different type of object than your standard CF array, i.e. Byte [] compared to java.util.List . Therefore, depending on what functions are used, you may need javacast to force the variables to the expected type. With that in mind.

Part I - Decrypt encKey

  • Base64-decode encKey.
  • Delete the first 32 bytes of the decoded value. This is HMAC (Hash Message Authentication Code). Calculate the HMAC SHA-256 for the rest of the decoded data using your common API and compare it to the HMAC from the first 32 bytes.

First, convert the base64 string to binary using binaryDecode . Then extract the appropriate number of bytes from the returned array. This is the expected HMAC value:

 hmacSize = 32; binaryToDecrypt = binaryDecode(encryptedKey, "base64"); expectedHMAC = binaryEncode( javacast("byte[]", arraySlice(binaryToDecrypt, 1, hmacSize)) , "hex" ); 

Then extract all the remaining bytes and use them to calculate the actual HMAC. Check it for the expected value. If they do not match, something went wrong.

 remainData = arraySlice(binaryToDecrypt, hmacSize + 1); actualHMAC = hmac( javacast("byte[]", remainData ), sharedSecret, "HMACSHA256"); if (compare(actualHMAC, expectedHMAC) != 0) { throw("ERROR: Invalid HMAC ["& actualHMAC &"]. Expected ["& expectedHMAC &"]"); } 
  1. The next 16 bytes should be deleted and used as IV (Initialization Vector) for the decryption algorithm.

The remaining bytes contain an IV followed by an encrypted value. Before you can decrypt the latter, you need to extract and separate two:

 ivSize = 16; ivValue = javacast("byte[]", arraySlice(remainData, 1, ivSize)); encryptedValue = javacast("byte[]", arraySlice(remainData, ivSize + 1)); 
  1. Decrypt the remaining data using AES-256-CBC, IV from step 3 and the SHA-256 hash of your secret API.

The final step before you can decrypt is to generate a decryption key by hashing the shared secret. Unfortunately, the CF hash () function always returns a hexadecimal string. Therefore, it must be converted to base64 format in order to be compatible with the decryption function.

 keyHex = hash(sharedSecret, "SHA-256", "utf-8"); keyBase64 = binaryEncode(binaryDecode(keyHex, "hex"), "base64"); 

Finally, use all three values ​​to decrypt. The returned binary will contain the encryption key used in Part II.

 decryptedKeyBinary = decryptBinary( encryptedValue , keyBase64 , "AES/CBC/PKCS5Padding" , ivValue); 


Part II - Decrypt data encPaymentData h2>

Use the same process as in the first part, just replace the variables:

  • Use encPaymentData instead of encryptedKey
  • Use decryptedKeyBinary instead of sharedSecret .

The final, decrypted result will be binary. Use charsetEncode to convert it back to a human-readable string:

 result = charsetEncode(decryptedResult, "utf-8"); 

NB: The sample values ​​chosen were apparently broken because they do not even work with the java example. The above steps lead to the correct result when using valid values ​​(key, data, etc.).

+1
source

Source: https://habr.com/ru/post/1263749/


All Articles