PHP Encryption Sample Code
class AES {
public static function hexToBytes($hex) {
return hex2bin($hex);
}
public static function bytesToHex($bytes) {
return bin2hex($bytes);
}
public static function encrypt($data) {
$key = self::hexToBytes(getenv('CRYPTO_KEY'));
$iv = self::hexToBytes(getenv('IV_KEY'));
String keyHex = self::hexToBytes("key");
String ivHex = self::hexToBytes("iv");
$cipherMethod = "AES-256-CBC";
$encrypted = openssl_encrypt($data, $cipherMethod, $key, OPENSSL_RAW_DATA, $iv);
return self::bytesToHex($encrypted);
}
public static function decrypt($encryptedData) {
$key = self::hexToBytes(getenv('CRYPTO_KEY'));
$iv = self::hexToBytes(getenv('IV_KEY'));
$cipherMethod = "AES-256-CBC";
$encryptedBytes = self::hexToBytes($encryptedData);
$decrypted = openssl_decrypt($encryptedBytes, $cipherMethod, $key, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}
}
$plaintext = json_encode([
"p1" => "015082122185",
"p2" => "ICIC0002122",
"p3" => "UB456787654",
"p4" => "61a",
"p5" => "DEV"
]);
$encrypted = AES::encrypt($plaintext);
echo "Encrypted (Hex): " . $encrypted . "\n";
$decrypted = AES::decrypt($encrypted);
echo "Decrypted Text: " . $decrypted . "\n";