Gm.php 876 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace app\common\library;
  3. use think\facade\Config;
  4. /**
  5. * 国密加密
  6. */
  7. class Gm
  8. {
  9. /*
  10. * 国密4加密
  11. * str 加密字符串
  12. */
  13. public static function encrypt($key, $str)
  14. {
  15. // $key = Config::get('gm.key');
  16. if($str) {
  17. $iv = str_repeat("\0", openssl_cipher_iv_length('SM4-CBC'));
  18. $ciphertext = openssl_encrypt($str, 'SM4-CBC', $key, OPENSSL_RAW_DATA, $iv);
  19. return base64_encode($ciphertext);
  20. }
  21. return $str;
  22. }
  23. /*
  24. * 国密4解密
  25. * str 解密字符串
  26. */
  27. public static function decrypt($key, $str)
  28. {
  29. if($str) {
  30. $iv = str_repeat("\0", openssl_cipher_iv_length('SM4-CBC'));
  31. return openssl_decrypt(base64_decode($str), 'SM4-CBC', $key, OPENSSL_RAW_DATA, $iv);
  32. }
  33. return $str;
  34. }
  35. }