Share a set of PHP encryption and decryption functions

  • 2021-06-28 12:04:56
  • OfStack


<?php
/**
* Function: Encrypt strings 
* parameter 1 : Content that needs to be encrypted 
* parameter 2 : Key 
*/
function passport_encrypt($str,$key){ // Encryption function 
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)?0:$ctr;
$tmp.=$encrypt_key[$ctr].($str[$i] ^ $encrypt_key[$ctr++]);
}
return base64_encode(passport_key($tmp,$key));
}
/**
* Function: Decrypt strings 
* parameter 1 : ciphertext that needs to be decrypted 
* parameter 2 : Key 
*/
function passport_decrypt($str,$key){ // decryption 
$str=passport_key(base64_decode($str),$key);
$tmp='';
for($i=0;$i<strlen($str);$i++){
$md5=$str[$i];
$tmp.=$str[++$i] ^ $md5;
}
return $tmp;
}
/**
* 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)?0:$ctr;
$tmp.=$str[$i] ^ $encrypt_key[$ctr++];
}
return $tmp;
}
$str=' Author: WWW.JB51.NET;
$key='jb51net';
$encrypt=passport_encrypt($str,$key);
$decrypt=passport_decrypt($encrypt,$key);
echo ' Original: ',$str."<br><hr>";
echo ' Ciphertext: ',$encrypt."<br><hr>";
echo ' Translation: ',$decrypt."<br><hr>";
?>


Related articles: