Crypt.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Crypt();
  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 = 'sdEKw2wJCnctEG09';
  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. if(!$key) return;
  39. $len = strlen($key);
  40. if ($len < 16) $key = str_pad($key, 16, '.');
  41. if($len > 16) $key = substr($key, 0, 16);
  42. $this->securityKey = $key;
  43. return $this;
  44. }
  45. /**
  46. * 设置iv字符串
  47. */
  48. public function setIv($iv)
  49. {
  50. if(strlen($iv) > 16) $iv = substr($iv, 0, 16);
  51. if(strlen($iv) < 16) $iv = str_pad($iv, 16, '.');
  52. $this->iv = $iv;
  53. }
  54. /**
  55. * 获取iv字符串
  56. */
  57. public function getIv()
  58. {
  59. $this->iv = $this->iv;
  60. if(!$this->iv < 16) $this->iv = str_pad($this->iv, 16, '.');
  61. if(strlen($this->iv) == 16) return $this->iv;
  62. return substr($this->iv, 0, 16);
  63. }
  64. /**
  65. * 加密字符
  66. * @param $string
  67. * @return string
  68. */
  69. public function encrypt($string)
  70. {
  71. $string = time() . $string;
  72. $passcrypt = openssl_encrypt($string, 'aes-256-cbc', $this->securityKey, OPENSSL_RAW_DATA, $this->getIv());
  73. return $this->getVerifyString(base64_encode($passcrypt));
  74. }
  75. /**
  76. * 解密字符
  77. *
  78. * @param $string
  79. * @return string
  80. */
  81. public function decrypt($string)
  82. {
  83. $string = str_replace(' ', '+', $string);
  84. $string = base64_decode($this->verifyString($string));
  85. $passcrypt = openssl_decrypt($string, 'aes-256-cbc', $this->securityKey, OPENSSL_RAW_DATA, $this->getIv());
  86. return substr($passcrypt, 10);
  87. }
  88. /**
  89. * 将字符串做数字签名
  90. *
  91. * @param $string
  92. * @return string
  93. */
  94. public function getVerifyCode($string)
  95. {
  96. return substr(md5($string), -1 * $this->keyLength);
  97. }
  98. /**
  99. * 生成签名字符串并返回 签名+字符串
  100. *
  101. * @param $string
  102. * @return string
  103. */
  104. public function getVerifyString($string)
  105. {
  106. return $this->getVerifyCode($string) . $string;
  107. }
  108. /**
  109. * 验证字符创的数字签名,如果没有通过就返回空字符,否则返回去掉签名的字符
  110. *
  111. * @param $string
  112. * @param $code
  113. * @return bool
  114. */
  115. public function verifyString($string)
  116. {
  117. $verifyCode = substr($string, 0, $this->keyLength);
  118. if ($this->getVerifyCode(substr($string, $this->keyLength)) != $verifyCode) return '';
  119. return substr($string, $this->keyLength);
  120. }
  121. }