中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

php gmssl庫的使用案例有哪些

PHP
小樊
116
2024-09-09 02:07:11
欄目: 云計算

PHP的gmssl庫是一個基于國密算法(SM2、SM3、SM4)的加密庫,它提供了一系列的加密、解密、簽名和驗證等功能。以下是一些使用PHP gmssl庫的示例:

  1. 生成SM2密鑰對
<?php
require_once 'vendor/autoload.php';
use Gmssl\SM2;

$sm2 = new SM2();
$keyPair = $sm2->generateKeyPair();
echo "公鑰: " . $keyPair['publicKey'] . "\n";
echo "私鑰: " . $keyPair['privateKey'] . "\n";
?>
  1. SM2加密和解密
<?php
require_once 'vendor/autoload.php';
use Gmssl\SM2;

$sm2 = new SM2();
$keyPair = $sm2->generateKeyPair();
$publicKey = $keyPair['publicKey'];
$privateKey = $keyPair['privateKey'];

$data = "Hello, world!";
$encryptedData = $sm2->encrypt($data, $publicKey);
echo "加密后的數據: " . $encryptedData . "\n";

$decryptedData = $sm2->decrypt($encryptedData, $privateKey);
echo "解密后的數據: " . $decryptedData . "\n";
?>
  1. SM2簽名和驗證
<?php
require_once 'vendor/autoload.php';
use Gmssl\SM2;

$sm2 = new SM2();
$keyPair = $sm2->generateKeyPair();
$publicKey = $keyPair['publicKey'];
$privateKey = $keyPair['privateKey'];

$data = "Hello, world!";
$signature = $sm2->sign($data, $privateKey);
echo "簽名: " . $signature . "\n";

$isValid = $sm2->verify($data, $signature, $publicKey);
echo "驗證簽名: " . ($isValid ? "有效" : "無效") . "\n";
?>
  1. SM3哈希計算
<?php
require_once 'vendor/autoload.php';
use Gmssl\SM3;

$sm3 = new SM3();
$data = "Hello, world!";
$hash = $sm3->hash($data);
echo "哈希值: " . $hash . "\n";
?>
  1. SM4加密和解密
<?php
require_once 'vendor/autoload.php';
use Gmssl\SM4;

$sm4 = new SM4();
$key = '0123456789abcdeffedcba9876543210'; // 16字節的密鑰

$data = "Hello, world!";
$encryptedData = $sm4->encrypt($data, $key);
echo "加密后的數據: " . $encryptedData . "\n";

$decryptedData = $sm4->decrypt($encryptedData, $key);
echo "解密后的數據: " . $decryptedData . "\n";
?>

請注意,這些示例需要先安裝PHP的gmssl庫。你可以通過Composer來安裝:

composer require gmssl/gmssl

0
栾城县| 张家界市| 耿马| 富平县| 双峰县| 南华县| 晋城| 乌拉特前旗| 长寿区| 和政县| 安龙县| 华阴市| 罗田县| 灌云县| 阿拉善盟| 垦利县| 北辰区| 齐河县| 高雄县| 淮阳县| 辛集市| 方城县| 哈巴河县| 长垣县| 道真| 建平县| 宁明县| 酉阳| 建德市| 沁阳市| 汝阳县| 防城港市| 静宁县| 基隆市| 华池县| 扶风县| 揭阳市| 柳江县| 土默特右旗| 宝清县| 华坪县|