PHP加密解密的两种方法

WBOY
Release: 2016-06-20 13:04:41
Original
1200 people have browsed it

一、利用md5和字符串处理函数

<p><?php  </p><p>$key = "This is supposed to be a secret key !!!";  </p><p>function keyED($txt,$encrypt_key)  { </p><p> $encrypt_key = md5($encrypt_key); </p><p> $ctr=0;  $tmp = ""; </p><p> for ($i=0;$i<strlen($txt);$i++)  {</p><p>  if ($ctr==strlen($encrypt_key)) $ctr=0; </p><p> $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); </p><p> $ctr++;</p><p>  } </p><p> return $tmp;</p><p>  }  </p><p>function encrypt($txt,$key)  { </p><p> srand((double)microtime()*1000000); </p><p> $encrypt_key = md5(rand(0,32000)); </p><p> $ctr=0; </p><p> $tmp = "";  </p><p>for ($i=0;$i<strlen($txt);$i++)  { </p><p> if ($ctr==strlen($encrypt_key)) $ctr=0; </p><p> $tmp.= substr($encrypt_key,$ctr,1) .  (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));  </p><p>$ctr++; </p><p> }  </p><p>return keyED($tmp,$key);  </p><p>}  </p><p>function decrypt($txt,$key)  { </p><p> $txt = keyED($txt,$key); </p><p> $tmp = ""; </p><p> for ($i=0;$i<strlen($txt);$i++)  { </p><p> $md5 = substr($txt,$i,1); </p><p> $i++;  </p><p>$tmp.= (substr($txt,$i,1) ^ $md5); </p><p> }  </p><p>return $tmp; </p><p> }  </p>$string = "Hello World !!!";  <br />// encrypt $string, and store it in $enc_text  $enc_text = encrypt($string,$key);  <br />// decrypt the encrypted text $enc_text, and store it in $dec_text  $dec_text = decrypt($enc_text,$key);  <br /><p>print "Original text : $string <Br>";  </p><p>print "Encrypted text : $enc_text <Br>";  </p><p>print "Decrypted text : $dec_text <Br>"; </p><p> ?></p>
Copy after login

二、利用md5和base64_encode及json_encode函数

<p><?php</p><p>// 表单验证密匙$form_api_secret = 'Wuuh7ZL8n5gdhCFS2wtm6FmU/M0=';</p><p>$options = array();</p><p>$options['expiration'] = time()+600;</p><p>$options['save-key'] = '/{year}/{mon}/{day}/upload_{filename}{.suffix}';</p><p>$policy = base64_encode(json_encode($options));</p>$signature = md5($policy.'&'.$form_api_secret);<br />?><br /><html>  <head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  </head>  <body><br /><p><form action="" method="post" enctype="multipart/form-data">      </p><p><input type="hidden" name="policy" value="<?php echo $policy?>">      </p><p><input type="hidden" name="signature" value="<?php echo $signature?>">      </p><p><input type="submit" value="提交">     </p><p></form></p>  </body></html>
Copy after login


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