27 lines
948 B
PHP
27 lines
948 B
PHP
<?php
|
|
|
|
namespace App\Support\Danal;
|
|
|
|
final class DanalAes256CbcHex
|
|
{
|
|
public function encrypt(string $plain, string $hexKey, string $hexIv): string
|
|
{
|
|
$iv = hex2bin($hexIv) ?: '';
|
|
$key = hex2bin($hexKey) ?: '';
|
|
$raw = openssl_encrypt($plain, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
|
|
if ($raw === false) throw new \RuntimeException('openssl_encrypt failed');
|
|
return base64_encode($raw);
|
|
}
|
|
|
|
public function decrypt(string $base64Cipher, string $hexKey, string $hexIv): string
|
|
{
|
|
$iv = hex2bin($hexIv) ?: '';
|
|
$key = hex2bin($hexKey) ?: '';
|
|
$raw = base64_decode($base64Cipher, true);
|
|
if ($raw === false) throw new \RuntimeException('base64_decode failed');
|
|
$plain = openssl_decrypt($raw, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
|
|
if ($plain === false) throw new \RuntimeException('openssl_decrypt failed');
|
|
return $plain;
|
|
}
|
|
}
|