File: /var/www/html/sarvodayahospital/app/ccAvenueCrypto.php
<?php
// phpcs:disable
namespace App;
/**
* ccAvenue Crypto details.
*/
class ccAvenueCrypto
{
// phpcs:enable
/**
* Construct funtion.
*/
public function __construct()
{
//pass
}
/**
* Encrypt data to be sent to ccAvenue.
*
* @param string $plainText Plain String
* @param string $key Working key provided by CCAvenue
*
* @return string encryptedText String
*/
public function encrypt($plainText, $key)
{
$key = $this->hextobin(md5($key));
$initVector = pack('C*', 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
$openMode = openssl_encrypt($plainText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector);
$encryptedText = bin2hex($openMode);
return $encryptedText;
}
/**
* Decrypted data from ccAvenue.
*
* @param string $encryptedText Encrypted String
* @param string $key Working key provided by CCAvenue
*
* @return string Plain String
*/
public function decrypt($encryptedText, $key)
{
$key = $this->hextobin(md5($key));
$initVector = pack('C*', 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
$encryptedText = $this->hextobin($encryptedText);
$decryptedText = openssl_decrypt($encryptedText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector);
return $decryptedText;
}
/**
* Convert hexa-decimal code to binary safe string.
*
* @param string $hexString Hexadecimal code
*
* @return string Binary safe string
*/
public function hextobin($hexString)
{
$length = strlen($hexString);
$binString = '';
$count = 0;
while ($count < $length) {
$subString = substr($hexString, $count, 2);
$packedString = pack('H*', $subString);
if ($count == 0) {
$binString = $packedString;
} else {
$binString .= $packedString;
}
$count += 2;
}
return $binString;
}
}