PHP uses a custom key to explain how to encrypt and decrypt data

Source: Internet
Author: User
Tags decrypt
This article mainly introduces PHP using a custom key implementation of data encryption and decryption methods, involving PHP for string conversion, interception and other operations to implement cryptographic decryption function, the need for friends can refer to the following

This example describes how PHP uses a custom key to decrypt data encryption. Share to everyone for your reference, as follows:

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

Cryptographic function Encryptstr ($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);} Decrypt function Decryptstr ($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 the Base64 encoding, it is recommended to urlencode() remove the + number of the effect.

PS: About encryption and decryption of interested friends can also refer to the site online tools:

Text online encryption and decryption tool (includes AES, DES, RC4, etc.):
Http://tools.jb51.net/password/txt_encode

MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password

Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

Online md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160 Encryption Tool:
Http://tools.jb51.net/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
Http://tools.jb51.net/password/sha_encode

Articles you may be interested in:

PHP implementation of simple arithmetic calculator function Example explained

Explanation of how to implement the non-fixed number of parameters in Laravel routing

Laravel Framework Blade Template Beginner's Tutorial

Related Article

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.