PHP uses a custom key to encrypt and decrypt data.
This example describes how PHP uses a custom key to encrypt and decrypt data. We will share this with you for your reference. The details are as follows:
When the client communicates with the server, it is common to use an id as a url parameter for back-and-forth transmission. Assume that the current business only has this id, so we need to communicate with each other in a bit more secure, encrypt the id, and decrypt it on the server. A secret key must be used by the server to encrypt and decrypt the key.
The encryption and decryption method is as follows: $ str is the string to be decrypted, and $ key is a custom key.
// Encryption 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 );} // decryption 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 this scenario is applied to the id in the url, It is base64 encoded after encryption.urlencode()
Remove the influence of the plus sign (+.
PS: if you are interested in encryption and decryption, refer to the online tools on this site:
Online text encryption and decryption tools (including AES, DES, and RC4 ):
Http://tools.jb51.net/password/txt_encode
MD5 online encryption tool:
Http://tools.jb51.net/password/CreateMD5Password
Online hash/hash 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 tools:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode