Home  >  Article  >  Backend Development  >  An explanation of how PHP uses custom keys to encrypt and decrypt data

An explanation of how PHP uses custom keys to encrypt and decrypt data

jacklove
jackloveOriginal
2018-07-06 17:45:212520browse

This article mainly introduces how PHP uses custom keys to encrypt and decrypt data. It involves PHP's related techniques for converting and intercepting strings to implement encryption and decryption functions. Friends who need it can refer to it

The example in this article describes how PHP uses a custom key to encrypt and decrypt data. Share it with everyone for your reference, the details are as follows:

When the client and server communicate, there is a very common scenario where an id is passed back and forth as a url parameter. Assuming that the current business only has this ID identifier, then slightly more secure communication is needed to encrypt and transmit this ID, and then decrypt it to the server. Here you need a key that is kept secret by the server, and this key is used for encryption and decryption.

The encryption and decryption methods are as follows: $str is the string that needs to be encrypted and decrypted, $key is a key defined by yourself

// 加密
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);
}
// 解密
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);
}

Worth it One thing to mention is:

If it is applied to the scenario of id in the URL, then after encryption, it is base64 encoding. It is recommended to urlencode() to remove the influence of the number.

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Text online encryption Decryption tools (including AES, DES, RC4, etc.):
http://tools.jb51.net/password/txt_encode

MD5 online encryption tool :
http://tools.jb51.net/password/CreateMD5Password

Online hashing/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 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:

Explanation of the simple four arithmetic operation calculator function implemented in PHP


Related explanation of how to implement a non-fixed number of parameters in Laravel routing


Laravel Framework Blade Template Beginner Tutorial


The above is the detailed content of An explanation of how PHP uses custom keys to encrypt and decrypt data. For more information, please follow other related articles on the PHP Chinese website!

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