PHP加密解密的类

WBOY
Release: 2016-06-20 13:04:02
Original
1004 people have browsed it

分享一个php加密解密的类,在用户注册的时候发送邮件验证的时候估计会用的到的。

代码如下:

<p>class SysCrypt{</p>	private $crypt_key='http://www.scutephp.com';//密钥<br />	public function __construct($crypt_key){<br />		$this->crypt_key=$crypt_key;<br />	}<br />	public function encrypt($txt){<br />		srand((double)microtime()*1000000);<br />		$encrypt_key=md5(rand(0,32000));<br />		$ctr=0;<br />		$tmp='';<br />		for($i=0;$i<strlen($txt);$i++){<br />			$ctr=$ctr==strlen($encrypt_key)?0:$ctr;<br />			$tmp.=$encrypt_key[$ctr].($txt[$i]^$encrypt_key[$ctr++]);<br />		}<br />		return base64_encode(self::__key($tmp,$this->crypt_key));<br />	}<br />	public function decrypt($txt){<br />		$txt=self::__key(base64_decode($txt),$this->crypt_key);<br />		$tmp='';<br />		for($i=0;$i<strlen($txt);$i++){<br />			$md5=$txt[$i];<br />			$tmp.=$txt[++$i]^$md5;<br />		}<br />		return $tmp;<br />	}<br />	private function __key($txt,$encrypt_key){<br />		$encrypt_key=md5($encrypt_key);<br />		$ctr=0;<br />		$tmp='';<br />		for($i=0;$i<strlen($txt);$i++){<br />			$ctr=$ctr==strlen($encrypt_key)?0:$ctr;<br />			$tmp.=$txt[$i]^$encrypt_key[$ctr++];<br />		}<br />		return $tmp;<br />	}<br />	public function __destruct(){<br />		$this->crypt_key=NULL;<br />	}<br /><p>}</p>
Copy after login

该类使用方法:

<p>$sc=new SysCrypt('http://www.scutephp.com');</p>$text='yhm.1234@163.com';<br />$test1=$sc->encrypt($text);<br />echo '原文:',$text;<br />echo '<br />';<br />echo '密文:',$test1;<br />echo "<br/>";<br /><p>echo '解密:',</p><p>$sc->decrypt($test1);</p>
Copy after login

输出结果类似:

原文:yhm.1234@163.com

密文:VSQBZFRpVysCZFVlAWICYghKVGNQMwRkU31bZVFsV28=
解密:yhm.1234@163.com


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template