威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
- <?php
- /**
- * DES加密解密类
- */
- if (!class_exists('DesCrypt')):
- class DesCrypt{
- var $key = '';
- var $cipherText = '';
- var $HcipherText = '';
- var $decrypted_data ='';
- function DesCrypt(){}
- /**
- * 加密函数
- * @param $str (需要加密的字符串)
- * @return 加密后的字符串
- */
- function encrypt($str) {
-
- $cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
- $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB), MCRYPT_RAND);
-
- if (mcrypt_generic_init($cipher, substr($this->key,0,8), $iv) != -1){
- $this->cipherText = mcrypt_generic($cipher,$this->pad($str));
- mcrypt_generic_deinit($cipher);
-
- $this->HcipherText=bin2hex($this->cipherText);
-
- }
- mcrypt_module_close($cipher);
-
- return strtoupper($this->HcipherText);
- }
- /**
- * 解密函数
- * @param $str (需要解密的字符串)
- * @return 解密后的字符串
- */
- function decrypt($str)
- {
- $str = pack('H*', $str);
- $cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
- $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB), MCRYPT_RAND);
- if (mcrypt_generic_init($cipher, substr($this->key,0,8), $iv) != -1)
- {
- $this->decrypted_data = mdecrypt_generic($cipher,$str);
- mcrypt_generic_deinit($cipher);
- return $this->decrypted_data;
- }
- mcrypt_module_close($cipher);
- return $this->unpad($this->decrypted_data);
- }
-
- private function pad ($data)
- {
- $data = str_replace("\n","",$data);
- $data = str_replace("\t","",$data);
- $data = str_replace("\r","",$data);
- return $data;
- }
-
- private function unpad ($text)
- {
- $pad = ord($text{strlen($text) - 1});
- if ($pad > strlen($text)) {
- return false;
- }
- if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
- return false;
- }
- return substr($text, 0, - 1 * $pad);
-
- }
- ?>
复制代码 |
|