1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- namespace app\common\library;
- use think\facade\Config;
- /**
- * 国密加密
- */
- class Gm
- {
- /*
- * 国密4加密
- * str 加密字符串
- */
- public static function encrypt($key, $str)
- {
- // $key = Config::get('gm.key');
- if($str) {
- $iv = str_repeat("\0", openssl_cipher_iv_length('SM4-CBC'));
- $ciphertext = openssl_encrypt($str, 'SM4-CBC', $key, OPENSSL_RAW_DATA, $iv);
- return base64_encode($ciphertext);
- }
- return $str;
-
- }
- /*
- * 国密4解密
- * str 解密字符串
- */
- public static function decrypt($key, $str)
- {
- if($str) {
- $iv = str_repeat("\0", openssl_cipher_iv_length('SM4-CBC'));
- return openssl_decrypt(base64_decode($str), 'SM4-CBC', $key, OPENSSL_RAW_DATA, $iv);
- }
- return $str;
- }
- }
|