Files
annnj-company 130c1026c4 first commit
2026-04-17 18:29:53 +08:00

35 lines
1.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
require_once '../vendor/autoload.php';
use Rtgm\sm\RtSm4;
$key = "0123456789abcdef";
$iv = '1234567887654321';
$sm4 = new RtSm4($key);
$data = '我1爱你ILOVEYOU!!!';
$data = str_repeat('abc',7);
//sm4 的ecb 与cbc加密有补齐16*nl
// sm4->encrypt($data, $type = 'sm4', $iv = '', $format = 'hex')
// openssl_encrypt ,和服务器openssl版本PHP版本有关有些服务器可能不支持sm4-* 相关的对称加密算法,
echo "==== test sm4 sm4-cbc============";
echo "\nphp sm4: ".$hex = $sm4->encrypt($data,'sm4',$iv); //default is cbc
echo "\nphp decode: ".$sm4->decrypt($hex,'sm4',$iv,'hex');
echo "\n==== test sm4-ecb============";
echo "\nphp sm4-ecb: ".$hex = $sm4->encrypt($data,'sm4-ecb');
echo "\nphp decode: ".$sm4->decrypt($hex,'sm4-ecb','','hex');
echo "\n==== test sm4-ofb============";
echo "\nphp sm4-ofb: ".$hex = $sm4->encrypt($data,'sm4-ofb',$iv);
echo "\nphp decode: ".$sm4->decrypt($hex,'sm4-ofb',$iv,'hex');
echo "\n==== test sm4-cfb============";
echo "\nphp sm4-cfb: ".$hex = $sm4->encrypt($data,'sm4-cfb',$iv);
echo "\nphp decode: ".$sm4->decrypt($hex,'sm4-cfb',$iv,'hex');
echo "\n==== test sm4-ctr============";
echo "\nphp sm4-ctr: ".$hex = $sm4->encrypt($data,'sm4-ctr',$iv);
echo "\nphp decode: ".$sm4->decrypt($hex,'sm4-ctr',$iv,'hex');