1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (!in_array('sm4-cbc', openssl_get_cipher_methods())) {
printf("不支持 sm4\n");
}

$key = 'her-cat.com';
$iv = random_bytes(openssl_cipher_iv_length('sm4-cbc'));

$plaintext = '她和她的猫';

$ciphertext = openssl_encrypt($plaintext, 'sm4-cbc', $key, OPENSSL_RAW_DATA , $iv);

printf("加密结果: %s\n", bin2hex($ciphertext));

$original_plaintext = openssl_decrypt($ciphertext, 'sm4-cbc', $key, OPENSSL_RAW_DATA , $iv);

printf("解密结果: %s\n", $original_plaintext);

运行结果:

1
2
加密结果: 45cd787b0a84603ae8fd443b81af4d17
解密结果: 她和她的猫