PHPackages                             evit/php-gm-crypto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. evit/php-gm-crypto

ActiveLibrary[Security](/categories/security)

evit/php-gm-crypto
==================

Fully compatible with the sm4-cbc and sm4-ecb state secret SM algorithms of openssl. When openssl &gt;= 1.1.1 supports the state secret algorithm, directly call openssl for SM4 encryption and decryption, otherwise call the custom algorithm.

1.0.4(2y ago)163.6k↑25%11MITPHP

Since Apr 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Endy-c/php-gm-crypto)[ Packagist](https://packagist.org/packages/evit/php-gm-crypto)[ RSS](/packages/evit-php-gm-crypto/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)DependenciesVersions (6)Used By (1)

国密SM4对称加密算法
===========

[](#国密sm4对称加密算法)

- PHP后端：`composer require evit/php-gm-crypto`
- 对应前端：`npm install evit-gm-crypt`

php-gm-crypto
=============

[](#php-gm-crypto)

[![PHP Style Guide](https://camo.githubusercontent.com/6205a4c253a9315c8b040eab3ec54a9d621912b01ec4869fe5cb4fab00e175ed/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c616e67756167652d5048502d627269676874677265656e2e737667)](https://www.php-fig.org/psr/)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

基于`PHP`的国密加密算法实现。

Implement of Chinese encrypt algorithm in PHP.

完全兼容openssl的sm4-cbc和sm4-ecb国密SM算法，openssl &gt;= 1.1.1支持国密算法时直接调用openssl进行SM4加解密，否则调用自定义算法。

Fully compatible with the sm4-cbc and sm4-ecb state secret SM algorithms of openssl. When openssl &gt;= 1.1.1 supports the state secret algorithm, directly call openssl for SM4 encryption and decryption, otherwise call the custom algorithm.

新增SM3实现，openssl &gt;= 1.1.1支持国密算法时直接调用openssl进行SM3杂凑，否则调用自定义算法。

与[`openssl_encrypt`](https://www.php.net/manual/en/function.openssl-encrypt.php)和[`openssl_decrypt`](https://www.php.net/manual/en/function.openssl-decrypt.php)保持一致性，当密码长度小于16时，静默填充NUL；当密码长度大于16时，静默截断。

Consistent with openssl\_encrypt and openssl\_decrypt, `NUL` is silently filled when the password length is less than 16; When the password length is greater than 16, it is truncated silently.

要使用openssl国密算法，openssl库需 &gt;= 1.1.1。

To use openssl state secret algorithm, openssl library needs &gt; = 1.1.1.

Roadmap
-------

[](#roadmap)

- SM4
- SM3
- SM2

Documentation
-------------

[](#documentation)

### Install

[](#install)

```
composer require evit/php-gm-crypto
```

### SM4

[](#sm4)

#### Init

[](#init)

```
