1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| class AES {
function encrypt(string $message, string $key, string $iv)
{
$encoding = 'UTF-8';
$chiperAlgorithm = 'aes-256-cbc';
$utf8Key = mb_convert_encoding($key, $encoding, mb_detect_encoding($key));
$hashedKey = hash('sha256', $key);
$bytesKey= hex2bin($hashedKey);
$utf8Message = mb_convert_encoding($message, $encoding, mb_detect_encoding($message));
$utf8Iv = mb_convert_encoding($iv, $encoding, mb_detect_encoding($iv));
$encrypted = openssl_encrypt($utf8Message, $chiperAlgorithm, $bytesKey, OPENSSL_RAW_DATA, $utf8Iv);
return strtoupper(bin2hex($encrypted));
}
function decrypt(string $encrypted, string $key, string $iv)
{
$encoding = 'UTF-8';
$chiperAlgorithm = 'aes-256-cbc';
$utf8Key = mb_convert_encoding($key, $encoding, mb_detect_encoding($key));
$hashedKey = hash('sha256', $key);
$bytesKey= hex2bin($hashedKey);
$utf8Iv = mb_convert_encoding($iv, $encoding, mb_detect_encoding($iv));
return openssl_decrypt(hex2bin($encrypted), $chiperAlgorithm, $bytesKey, OPENSSL_RAW_DATA, $utf8Iv);
}
}
|