qhweb / think-crypto
There is no license information available for the latest version (1.0.0) of this package.
Code encryption and decryption
1.0.0
2025-11-19 06:54 UTC
Requires
- php: >=7.1
- ext-mbstring: *
- ext-openssl: *
This package is auto-updated.
Last update: 2025-11-20 08:07:10 UTC
README
介绍
该插件是实现PHP程序的SM3、SM4的加密解密工具。支持ECB、CBC的加密解密使用简单。
安装教程
componser require qhweb/think-cropty
使用说明
支持文件夹批量加密和解密,只针对`ThinkPHP`框架
配置加密模式
Thinkphp中使用crypto.php统一配置
return [
//密钥16位字符串
'key' => '1234567890abcdef',
//初始化向量16位字符串
'iv' => 'abcdefghijklmnop',
//加密模式,普通加密AES,国密SM
'mode' => 'sm',
//国密SME加密类型,可分为ECB/CBC
'sm4_type' => 'cbc',
//版权信息,加密后文件顶部注释信息
'copyright' => '// +----------------------------------------------------------------------
// | 版权所有 2014~2021 某某科技有限公司
// +----------------------------------------------------------------------'
];
实例化插件
think\ctypto\service\Crypto代码中那个有默认配置,也可以自己传入值。
use think\ctypto\service\Crypto;
$key = '1234567890abcdef';//16位
$iv = 'ghijklmnoprstyvw';//16位
$mode = 'cbc';//加密模式,默认ecb可不传
$crypto = new Crypto($key,$iv,$mode);
SM3加密(签名加密)
use think\ctypto\service\Crypto;
$crypto = new Crypto();
$sign = $crypto->hash('Hello');//加密字符串
$sign = $crypto->sign(['a'=>1,'b'=>2]);//数组加密
SM4加密(对称加密)
use think\ctypto\service\Crypto;
$key = '1234567890abcdef';//16位
$iv = 'ghijklmnoprstyvw';//16位
$mode = 'cbc';//加密模式,默认ecb可不传
$crypto = new Crypto($key,$iv,$mode);
//加密
$encode = $crypto->encrypt('Hello',$mode);
//解密
$decode = $crypto->decrypt($encode,$mode);
加密文件夹
use think\ctypto\service\Crypto;
$key = '1234567890abcdef';//16位
$iv = 'ghijklmnoprstyvw';//16位
$mode = 'cbc';//加密模式,默认ecb可不传
$crypto = new Crypto($key,$iv,$mode);
$crypto->encryptDir('app','encode');
解密文件夹
use think\ctypto\service\Crypto;
$key = '1234567890abcdef';//16位
$iv = 'ghijklmnoprstyvw';//16位
$mode = 'cbc';//加密模式,默认ecb可不传
$crypto = new Crypto($key,$iv,$mode);
$crypto->decryptDir('encode','app');
解密文件
use think\ctypto\service\Crypto;
$key = '1234567890abcdef';//16位
$iv = 'ghijklmnoprstyvw';//16位
$mode = 'cbc';//加密模式,默认ecb可不传
$crypto = new Crypto($key,$iv,$mode);
$crypto->decryptFile($path);
快捷使用
// SM3加密
sm3('加密字符');
// SM4加密
sm4Encrypt('加密字符');
// SM4解密
sm4Decrypt('加密字符');
// 文件解密
decryptFile('文件路径',false);
前端JS加密解密
引入包内提供的js插件
<script type="text/javascript" src="./dist/gm-crypto.min.js"></script>
SM2
<script>
const { sm2 } = window.gmCrypto
//生成公钥、私钥
let keypair = sm2.generateKeyPairHex()
let publicKey = keypair.publicKey // 公钥
let privateKey = keypair.privateKey // 私钥
//console.log('公钥',publicKey)
//console.log('私钥',privateKey)
// 验证公钥
let verifyResult = sm2.verifyPublicKey(publicKey)
// console.log('验证公钥',verifyResult)
const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
const msgString = "Hello"
let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果
let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果
// console.log('加密结果',encryptData)
// console.log('解密结果',decryptData)
SM3
//SM3 加密
const { sm3 } = window.gmCrypto
// 字符加密
let encryptDataSm3 = sm3('待加密字符串')
//console.log('SM3加密',encryptDataSm3)
SM4
const { sm4 } = window.gmCrypto
const msg = 'hello world!' // 可以为 utf8 串或字节数组
const key = '1234567890abcdef'; // 16 字节
const iv = 'abcdefghijklmnop'; // 16 字节
//SM4 cbc 模式
let encryptDataSm4 = sm4.encrypt(msg, key, {mode: 'cbc', iv}) // 加密,cbc 模式
let decryptDataSm4 = sm4.decrypt(encryptDataSm4, key, {mode: 'cbc', iv}) // 解密,cbc 模式
//console.log('SM4加密结果cbc',encryptDataSm4)
//console.log('SM4解密结果cbc',decryptDataSm4)
//SM4 ecb 模式
let encryptDataecb = sm4.encrypt(msg, key) // 加密,ecb 模式
let decryptDataecb = sm4.decrypt(encryptDataecb, key) // 解密,ecb 模式
//console.log('SM4加密结果ecb',encryptDataecb)
//console.log('SM4解密结果ecb',decryptDataecb)
</script>