Heim >Backend-Entwicklung >PHP-Tutorial >PHP加密解密的两种方法
<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>
<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>