Crypt.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Qii\Library;
  3. /**
  4. * \Qii\Library\Crypt
  5. * @author Jinhui Zhu<zhujinhui@zhangyue.com>2015-11-09 16:07
  6. *
  7. * 加密类
  8. * 用法:
  9. * $crypt = new \Qii\Library\BootstrapCrypt();
  10. * 设置密钥
  11. * $crypt->setSecurityKey('密钥');
  12. * 加密字符串
  13. * echo $crypt->encrypt('加密字符串');
  14. * 解密字符串
  15. * echo $crypt->decrypt('解密字符串');
  16. */
  17. class Crypt
  18. {
  19. const VERSION = '1.2';
  20. //密匙
  21. private $securityKey = 'qii.v.1.3';
  22. private $keyLength = 4;
  23. private $iv = 'w2wJCnctEG09danPPI7SxQ==';
  24. public function __construct()
  25. {
  26. if (!function_exists('openssl_encrypt')) {
  27. throw new \Exception(\Qii::i(1008, 'openssl_encrypt'), __LINE__);
  28. }
  29. $this->setSecurityKey($this->securityKey);
  30. return $this;
  31. }
  32. /**
  33. * 设置用于加密解码的密匙
  34. * @param $key
  35. */
  36. public function setSecurityKey($key)
  37. {
  38. $len = strlen($key);
  39. if ($len < 16) $key = str_pad($key, 16, '.');
  40. $this->securityKey = $key;
  41. return $this;
  42. }
  43. /**
  44. * 设置iv字符串
  45. */
  46. public function setIv($iv)
  47. {
  48. if(strlen($iv) > 16) $iv = substr(0, 16);
  49. if(strlen($iv) < 16) $iv = str_pad($iv, 16, '.');
  50. $this->iv = $iv;
  51. }
  52. /**
  53. * 获取iv字符串
  54. */
  55. public function getIv()
  56. {
  57. if(!$this->iv < 16) $this->iv = str_pad($this->iv, 16, '.');
  58. if(strlen($this->iv) == 16) return $this->iv;
  59. return substr($this->iv, 0, 16);
  60. }
  61. /**
  62. * 加密字符
  63. * @param $string
  64. * @return string
  65. */
  66. public function encrypt($string)
  67. {
  68. $string = time() . $string;
  69. $passcrypt = openssl_encrypt($string, 'aes-256-cbc', $this->securityKey, OPENSSL_RAW_DATA, $this->getIv());
  70. return $this->getVerifyString(base64_encode($passcrypt));
  71. }
  72. /**
  73. * 解密字符
  74. *
  75. * @param $string
  76. * @return string
  77. */
  78. public function decrypt($string)
  79. {
  80. $string = base64_decode($this->verifyString($string));
  81. $passcrypt = openssl_decrypt($string, 'aes-256-cbc', $this->securityKey, OPENSSL_RAW_DATA, $this->getIv());
  82. return substr($passcrypt, 10);
  83. }
  84. /**
  85. * 将字符串做数字签名
  86. *
  87. * @param $string
  88. * @return string
  89. */
  90. public function getVerifyCode($string)
  91. {
  92. return substr(md5($string), -1 * $this->keyLength);
  93. }
  94. /**
  95. * 生成签名字符串并返回 签名+字符串
  96. *
  97. * @param $string
  98. * @return string
  99. */
  100. public function getVerifyString($string)
  101. {
  102. return $this->getVerifyCode($string) . $string;
  103. }
  104. /**
  105. * 验证字符创的数字签名,如果没有通过就返回空字符,否则返回去掉签名的字符
  106. *
  107. * @param $string
  108. * @param $code
  109. * @return bool
  110. */
  111. public function verifyString($string)
  112. {
  113. $verifyCode = substr($string, 0, $this->keyLength);
  114. if ($this->getVerifyCode(substr($string, $this->keyLength)) != $verifyCode) return '';
  115. return substr($string, $this->keyLength);
  116. }
  117. }