PHP
💡 Important Notes​
💡
Implementation Tips
The encryption and decryption snippets are placed separately in the documentation for clarity. Make sure to choose the correct PHP kit version based on your server's PHP version to ensure compatibility and optimal performance.
🔧 Quick Start​
âš¡
Basic Usage
You can find Request and Response classes in the kit. Use the encrypt() and decrypt() methods from the HesabeCrypt class:
<?php
$encrypted = HesabeCrypt::encrypt($requestDataJson, $encryptionKey, $ivKey);
$decrypted = HesabeCrypt::decrypt($checkoutResponseContent, $encryptionKey, $ivKey)
🔒 Encryption Implementation​
🔒
HesabeCrypt Class - Encryption
The encryption method uses AES-256-CBC algorithm for secure data transmission:
<?php
namespace App\Libraries;
class HesabeCrypt {
// AES Encryption Method Starts
public static function encrypt($str, $key, $ivKey)
{
$str = self::pkcs5_pad($str);
$encrypted = openssl_encrypt($str, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING, $ivKey);
$encrypted = base64_decode($encrypted);
$encrypted = unpack('C*', ($encrypted));
$encrypted = self::byteArray2Hex($encrypted);
$encrypted = urlencode($encrypted);
return $encrypted;
}
private static function pkcs5_pad($text)
{
$blocksize = 32;
$pad = $blocksize - (strlen($text) % $blocksize);
return $text.str_repeat(chr($pad), $pad);
}
private static function byteArray2Hex($byteArray)
{
$chars = array_map("chr", $byteArray);
$bin = join($chars);
return bin2hex($bin);
}
🔓 Decryption Implementation​
🔓
HesabeCrypt Class - Decryption
The decryption method for AES algorithm to process response data:
// Decryption Method for AES Algorithm Starts
public static function decrypt($code, $key, $ivKey)
{
if (!(ctype_xdigit($code) && strlen($code) % 2 == 0)) {
return false;
}
$code = self::hex2ByteArray(trim($code));
$code = self::byteArray2String($code);
$iv = $key;
$code = base64_encode($code);
$decrypted = openssl_decrypt($code, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING, $ivKey);
return self::pkcs5_unpad($decrypted);
}
private static function hex2ByteArray($hexString)
{
$string = hex2bin($hexString);
return unpack('C*', $string);
}
private static function byteArray2String($byteArray)
{
$chars = array_map("chr", $byteArray);
return join($chars);
}
private static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, -1 * $pad);
}
}