PHP uses a custom key, the method of data encryption and decryption

Source: Internet
Author: User

When communicating with the client and the server, there is a common scenario that is passed back and forth through an ID as a URL parameter. Assuming that there is only this ID identified in the business, then a slightly more secure communication is required, and the ID is encrypted and transmitted to the server for decryption. This requires a confidential key from the server, which is used to encrypt and decrypt the key.

The decryption method is as follows: $str is a string that needs to be decrypted, $key is a key of its own definition

//EncryptfunctionENCRYPTSTR ($str,$key){    $block= Mcrypt_get_block_size (' des ', ' ECB '); $pad=$block- (strlen($str) %$block); $str.=str_repeat(CHR($pad),$pad); $enc _str= Mcrypt_encrypt (Mcrypt_des,$key,$str,MCRYPT_MODE_ECB); return Base64_encode($enc _str);}//decryptionfunctionDECRYPTSTR ($str,$key){    $str=Base64_decode($str); $str= Mcrypt_decrypt (Mcrypt_des,$key,$str,MCRYPT_MODE_ECB); $block= Mcrypt_get_block_size (' des ', ' ECB '); $pad=Ord($str[($len=strlen($str))-1]); return substr($str, 0,strlen($str) -$pad);}

It is worth mentioning that:

If the URL is applied to the ID of this scene, then after the encryption is Base64 encoding, it is recommended to UrlEncode (), remove the + number of the effect.

PHP uses a custom key, the method of data encryption and decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.