Home > Backend Development > PHP Tutorial > Detailed explanation of PHP encryption and decryption functions_php skills

Detailed explanation of PHP encryption and decryption functions_php skills

WBOY
Release: 2016-05-16 20:05:46
Original
1122 people have browsed it

Share a PHP encryption and decryption function. This function implements the function of encrypting some variable values.
The encryption code is as follows:

/* 
*功能:对字符串进行加密处理 
*参数一:需要加密的内容 
*参数二:密钥 
*/ 
function passport_encrypt($str,$key){ //加密函数 
  srand((double)microtime() * 1000000); 
  $encrypt_key=md5(rand(0, 32000)); 
  $ctr=0; 
  $tmp=''; 
  for($i=0;$i<strlen($str);$i++){ 
    $ctr=$ctr==strlen($encrypt_key)&#63;0:$ctr; 
    $tmp.=$encrypt_key[$ctr].($str[$i] ^ $encrypt_key[$ctr++]); 
  } 
  return base64_encode(passport_key($tmp,$key)); 
} 
Copy after login

The decryption code is as follows:

/* 
*功能:对字符串进行解密处理 
*参数一:需要解密的密文 
*参数二:密钥 
*/ 
function passport_decrypt($str,$key){ //解密函数 
  $str=passport_key(base64_decode($str),$key); 
  $tmp=''; 
  for($i=0;$i<strlen($str);$i++){ 
    $md5=$str[$i]; 
    $tmp.=$str[++$i] ^ $md5; 
  } 
  return $tmp; 
} 
Copy after login

Auxiliary function:

/* 
*辅助函数 
*/ 
function passport_key($str,$encrypt_key){ 
  $encrypt_key=md5($encrypt_key); 
  $ctr=0; 
  $tmp=''; 
  for($i=0;$i<strlen($str);$i++){ 
    $ctr=$ctr==strlen($encrypt_key)&#63;0:$ctr; 
    $tmp.=$str[$i] ^ $encrypt_key[$ctr++]; 
  } 
  return $tmp; 
} 
Copy after login

is used as follows:

$str='作者:余浩苗;电话:13611972365;电子邮件:123cvbz@163.com'; 
 
$key='hacker'; 
$encrypt=passport_encrypt($str,$key); 
$decrypt=passport_decrypt($encrypt,$key); 
 
echo '原文:',$str."<br><hr>"; 
echo '密文:',$encrypt."<br><hr>"; 
echo '译文:',$decrypt."<br><hr>"; 
Copy after login

The above is the PHP encryption and decryption function shared with you. I hope you like it and can apply it to your own learning.

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