Home  >  Article  >  Backend Development  >  Asking for the use of PHP encryption and decryption algorithm

Asking for the use of PHP encryption and decryption algorithm

WBOY
WBOYOriginal
2016-07-11 09:17:421057browse

Encryption PHP algorithm

Recent project requirements require PHP to be used to generate an encrypted string from some ordinary information through an encryption algorithm. The encrypted string can parse out the previous information through the decryption algorithm (reverse). Since I have never done it before, I would like to ask friends here if they have any ideas for ready-made encryption and decryption algorithms. The algorithm needs to be complex enough and not easy to be cracked. It would be better if there is a specific code

Reply content:

http://blog.shiniv.com/2013/11/use-aes-encryption-algorithm-to-encrypt-data-in-php/

http://www.soso.io/article/32937.html

Commonly used encryption/decryption string functions:
/**

  • Reversible string encryption function
  • @param int $txtStream String content to be encrypted
  • @param int $password encryption password
  • @return string encrypted string*/ public static function enCrypt($txtStream,$password){ //Secret lock string, no repeated characters, contains A-Z,a-z,0-9,/,=,,_,

$lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdef IJK6/7nopqr89LMmGH012345uv';
//Find a random number and find a secret lock value from the secret lock string
$lockLen = strlen($lockstream);
$lockCount = rand(0,$lockLen-1);
$randomLock = $lockstream[$lockCount];
//Combined with the random encryption value to generate the MD5 password
$password = md5($password.$randomLock);
//Start encrypting the string
$txtStream = base64_encode($txtStream);
$tmpStream = '';
$i=0;$j=0;$k = 0;
for ($i=0; $i $k = ($k == strlen($password)) ? 0 : $k;
$j = (strpos($lockstream,$txtStream[$i]) $lockCount ord($password[$k]))%($lockLen);
$tmpStream .= $lockstream[$j];
$k ;
}
return $tmpStream.$randomLock;

}

header("content-type:text/html;charset=utf8");
$str = "My name is Jack Ma and I won’t tell anyone else!"; //Encrypted content
$key = "9545646"; //Key. Make your own key
$cipher = MCRYPT_DES; //Password type
$modes = MCRYPT_MODE_ECB; //Password mode
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);//Initialization vector
echo "Encrypted plain text: ".$str."

";
$str_encrypt =base64_encode( mcrypt_encrypt($cipher,$key,$str,$modes,$iv)); //Encryption function
echo "Encrypted ciphertext: ".$str_encrypt."

";
$str_decrypt = mcrypt_decrypt($cipher,$key,base64_decode($str_encrypt),$modes,$iv); //Decryption function
echo "Restore:".$str_decrypt;
?>

Statement:
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