I'm trying to implement greenpay payment service website link : https://support.greenpay.me/hc/es-mx/articles/360020166071-Proceso-de-tokenizaci%C3%B3n-sin-formulario
i'm success in getting token response
(
[session] => 2b0905d7-ed02-48de-80b2-63b1020afa67
[token] => b5a2176f-1ac8-4373-b188-979ca3f95ab4
)
I'm getting probelm in encryption which is for making ld and lk
{
"session": "session obtained in 'Create a tokenization order'",
"ld": "data of the card or token encrypted with AES-CTR-128 in HEX format",
"lk": "Keys AES encrypted with public key in base64 format"
}
Below are the steps to follow to encrypt the card information.
Generate dynamically a pair of AES keys (Key, Counter), which will be used to encrypt the card information.
Each key is an array of 16 bytes.
The key contains the key to encrypt in AES. The Counter is the counter for the CTR mode of AES.
Encrypt the JSON object with the data of the card, and convert the result of the encryption to hexadecimal format .
The string in hexadecimal format corresponds to the value "ld" that must be sent in the "body" of the payment request The value "ld" that you must send in the body of the payment request, corresponds to the string in hexadecimal format that is obtained when you encrypt the JSON object that contains the information of the card with AES-CTR-128 Encrypt with RSA the AES keys (key, counter) with the public key provided by "GreenPay support team".
The string in base64 format obtained by encrypting the AES key pair corresponds to the "lk" value that must be sent in the "body" of the payment request.
The value "lk" that must be sent in the body of the payment request corresponds to the coding in base64 of the encryption of the AES keys.
Node js code
link for code < https://gitlab.com/gp-examples/tokenize/tokenize_nodejs/blob/develop/tokenize.js >
function generateAESPairs () {
var key = [] var iv for (let k = 0; k < 16; k++) { key.push(Math.floor(Math.random() * 255)) } for (let k = 0; k < 16; k++) { iv = Math.floor(Math.random() * 255) }
return { k: key, s: iv }
}
// Creates an JSON object with the card data and AES key Pair encrypted
function pack (obj, session, pair_) {
var pair = (pair_ !== undefined) ? pair_ : generateAESPairs() var textBytes = aesjs.utils.utf8.toBytes(JSON.stringify(obj)) // eslint-disable-next-line new-cap
var aesCtr = new aesjs.ModeOfOperation.ctr(pair.k, new aesjs.Counter(pair.s))
var encryptedBytes = aesCtr.encrypt(textBytes) var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes)
var returnObj = { session: session, ld: encryptedHex, lk: rsa_.encrypt(JSON.stringify(pair)) }
console.log("Pack: ", JSON.stringify(returnObj), "\n")
return returnObj
}
Some php code i have used to make encryption
private function EncryptData($source,$pub_key)
{
//RSA encryption
// $fp=fopen("/etc/httpd/conf/ssl.crt/server.crt","r");
// $pub_key=fread($fp,8192);
// fclose($fp);
$encrypted = null;
// echo "source: $source";
// $pubkey = openssl_get_publickey($pub_key);
openssl_public_encrypt($source,$encrypted, $pub_key );
// echo "\n encrypted: \n";
// print_r($encrypted);
return(base64_encode($encrypted));
}
private function generateAESPairs() {
$key = array();
$iv;
for ($k = 0; $k < 16; $k++) {
array_push($key,floor(mt_rand(0, 255)));
}
for ($k = 0; $k < 16; $k++) {
$iv = floor(mt_rand(0, 255));
}
return array('k'=>$key,'s'=>$iv);
}
//in calling function
$temp_pair=$this->generateAESPairs();
$encryption_key=implode(',',$temp_pair['k']);
$iv=$temp_pair['s'];
echo "encryption_key: $encryption_key \n";
$pair=json_encode($temp_pair);
// Create some data to encrypt
$str_len = strlen($cardDataJson);
$data = "";
// Converting String to binary for encryption
for ($i=0;$i<$str_len;$i++)
$data .= decbin(ord($cardDataJson[$i]));
// $data = $cardDataJson;
// echo "\n encryption iv: $iv \n";
// echo "\n temp_pair: \n";
// print_r($temp_pair);
echo "\n json_encode pair: $pair \n";
$EncryptDataPairBase64= $this->EncryptData($pair,$public_key);
// echo "\n EncryptDataPairBase64: $EncryptDataPairBase64 \n";
// print_r($EncryptDataPairBase64);
echo "\nBefore encryption: $data\n";
// Encrypt $data using aes-256-cbc cipher with the given encryption key and
// our initialization vector. The 0 gives us the default options, but can
// be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
$encrypted = openssl_encrypt($data, AES_128_CTR, $encryption_key, 0, $iv);
$encryptedHexa= bin2hex($encrypted);
echo "Encrypted: $encrypted\n";
echo "encryptedHexa: $encryptedHexa\n";