PHP的gmssl库是一个基于国密算法(SM2、SM3、SM4)的加密库,它提供了一系列的加密、解密、签名和验证等功能。以下是一些使用PHP gmssl库的示例:
- 生成SM2密钥对
generateKeyPair(); echo "公钥: " . $keyPair['publicKey'] . "\n"; echo "私钥: " . $keyPair['privateKey'] . "\n"; ?>
- SM2加密和解密
generateKeyPair(); $publicKey = $keyPair['publicKey']; $privateKey = $keyPair['privateKey']; $data = "https://www.yisu.com/ask/Hello, world!"; $encryptedData = https://www.yisu.com/ask/$sm2->encrypt($data, $publicKey); echo "加密后的数据: " . $encryptedData . "\n"; $decryptedData = https://www.yisu.com/ask/$sm2->decrypt($encryptedData, $privateKey); echo "解密后的数据: " . $decryptedData . "\n"; ?>
- SM2签名和验证
generateKeyPair(); $publicKey = $keyPair['publicKey']; $privateKey = $keyPair['privateKey']; $data = "https://www.yisu.com/ask/Hello, world!"; $signature = $sm2->sign($data, $privateKey); echo "签名: " . $signature . "\n"; $isValid = $sm2->verify($data, $signature, $publicKey); echo "验证签名: " . ($isValid ? "有效" : "无效") . "\n"; ?>
- SM3哈希计算
hash($data); echo "哈希值: " . $hash . "\n"; ?>
- SM4加密和解密
encrypt($data, $key); echo "加密后的数据: " . $encryptedData . "\n"; $decryptedData = https://www.yisu.com/ask/$sm4->decrypt($encryptedData, $key); echo "解密后的数据: " . $decryptedData . "\n"; ?>
请注意,这些示例需要先安装PHP的gmssl库。你可以通过Composer来安装:
composer require gmssl/gmssl