In the development process of the website, we often need to encrypt some data (such as user password), this article mainly introduces several common cryptographic functions of PHP:
MD5 Encryption:
String MD5 (string $str [, bool $raw _output = false])
1.MD5 () returns the hash value by default as a 32-character hexadecimal number, which accepts two parameters, the first is the string to encrypt, the second is a Boolean value of Raw_output, the default is False, and if set to TRUE,MD5 () returns the original 16 BITS binary Format Message summary
2.MD5 () is a one-way encryption, there is no reverse decryption algorithm, but still can be some common string through collection, enumeration, collision and other methods to crack
<?php $username = Jellybool '; $password = ' jellybool.com '; /* simply MD5 encrypt the string */echo MD5 ($ username); echo "echo MD5 ( $password); echo "/* more recommended is to encrypt important sensitive data multiple times to avoid being easily cracked */echo MD5 (MD5 ( $password)); /* above output: username:4f5436e5d72608fb647b691e8edcf42e password:7bf02cf0f4af6da4accbc73d2a175476 Password (two-time encryption): 864704bb35754f8cd0232cba6b91521b */
Crypt Encryption:
String crypt (String $str [, String $salt])
1.crypt () accepts two parameters, the first is a string that needs to be encrypted, the second is a salt value (which is the encryption interference value, if not provided, it is automatically generated by PHP); Returns the hashed string or a string less than 13 characters, which is to distinguish the salt value.
2.crypt () is one-way encryption, just like MD5.
<?php$password =' Jellybool.com ';Echo Crypt ($password);Output: $1$fe0.qr5. $WOhkI 4/5vpo7n7tnxhh5k/* The eight characters between the second $ and the third $ are generated by PHP and change once per refresh */echo "echo Crypt ( $password, " Jellybool "); //output: Je7fniu1knaes /* when we want to add a custom salt value, As the example of Jellybool is added as the second parameter directly, the top two bits exceeding two characters will be intercepted */echo "echo Crypt ( $password, $1$ Jellybool$ '); //output: $1$jellyboo$dxh7wf7sygrpwb6xbbgfh//* Crypt encryption function has a variety of salt value encryption support, the above example shows that the MD5 hash as the salt value, in this way the salt value in the form of $1$$, such as the example of the Jellybool plus the next two $ characters, beyond the eight-character will intercept the first eight, the total length of 12 bits; Crypt is this form by default. */echo "//crypt also has a variety of salt value encryption support, see the manual
SHA1 Encryption:
String SHA1 (String $str [, bool $raw _output = false]
1. Unlike MD5, the difference is that SHA1 () returns a hash value of 40 characters by default, passing in a parameter, the first is an encrypted string, the second is a Boolean value of Raw_output, the default is False, and if set to TRUE,SHA1 () returns the original 20 Bit RAW Format Message summary
2.SHA1 () is also a single-line encryption, no reverse decryption algorithm
<?php $my _intro="Jellybool"; Echo SHA1 ($my _intro); //output: C98885C04C1208FD4D0B1DADD3BD2A9FF4D042CA echo "//Of course, multiple cryptographic algorithms can be mixed using echo MD5 (SHA1 ($my _intro)); / /output: 94f25bf9214f88b1ef065a3f9b5d9874 //This method of double encryption can also improve the security of data
UrlEncode Encryption:
String UrlEncode (String $str)
1. A parameter, passing in the string to be encrypted (usually applied to the encryption of the URL),
2.urlencode is two-way encryption, can be encrypted with urldecode (strictly speaking, not true encryption)
3. Returns the string, in addition to-_, in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+).
<?php//urlencode () is typically used to hide plaintext data in a URL$my _urlencode="Jellybool.com?jellybool=true + 4-3%5= \&@!"; Echo UrlEncode ($my _urlencode);Output: jellybool.com%3fjellybool%3dtrue+%2b+4-3%255%3d+%5c%26+%40%21 Echo"$my _urldecode="Jellybool.com%3fjellybool%3dtrue+%2b+4-3%255%3d+%5c%26+%40%21 "; Echo UrlDecode ($my _urldecode);Output: Jellybool.com?jellybool=true +4-3%5= \&@! Restore theOutput echo of the $my _urlencode"$my _urldecode="Http://www.baidu.com/s?word=jellybool+%e8%a7%89%e7%b4%af%e4%b8%8d%e7%88%b1&tn=98236947_hao_pg&ie=utf-8 "; Echo UrlDecode ($my _urldecode);/* Output: http://www.baidu.com/s?word=jellybool feel tired not love &tn=98236947_hao_pg&ie=utf-8 Yes, this is in Baidu search Jellybool feel tired not love */* ========================================================================= solve the second classic problem ========================== =============================================== */$pre _url_encode= "jellybool.com?username=jellybool&password=jelly";//In actual development, we often have to construct this URL, which is no problem $url _decode = "jellybool.com?username=jelly&bool&password=jelly"; /* Note the difference between the above two variables: the first Username=jellybool, the second is username=jelly&bool in this case with $_get () to accept the problem, this can be solved by the following method */$ Username= "Jelly&bool"; $url _decode = "Jellybool.com?username=". UrlEncode ($username). " &password=jelly "; //This is a good solution to the problem/* summarize the Common urlencode () conversion characters? = =%3f = = = %3d% = %25 & = %26 \ = %5c + = %2b space + + */
Base64 encoded encryption:
String Base64_decode (String $encoded _data)
1.base64_encode () accepts a parameter, which is the data to be encoded (not the string here, because many times base64 is used to encode the image)
2.base64_encode () is bidirectional encrypted and can be decrypted with Base64_decode ()
<?php $my _intro=echo base64_encode ( $my _intro); echo "/* output: Smvsbhlcb29s5piv5lia5liq6lqr5p2q5pyj6auy5bqmloicqeiggoaciewuvew 6PIZOG7JOGOZMNINLJPRLUQYS5OCD5OOZ5PYJ5REX5BQM55QE5ZU95A625YWN5QOA5LQ Uqee6p+s8moi0qos8quwjjeerr0lu55s35bgm5lid * /echo base64_decode ( Smvsbhlcb29s5piv5lia5liq6lqr5p2q5pyj6auy5bqmloicqeigg Oaciewuvew6pizog7jogozmninljprluqys5ocd5ooz5pyj5rex5bqm55qe5zu95a6 25ywn5qoa5lquqee6p+ S8moi0qos8quwjjeerr0lu55s35bgm5lid '); /* output: Jellybool is a stature has height, shoulder width, chest muscle has thickness, thought has the depth of the national inspection-exempt five A-level high-quality pseudo front-end it male cock silk */
An example of a picture:
<?php/ * A picture of the application example */ $filename ="Https://worktile.com/img/index/index_video.png"; $data =file_get_contents ($filename); Echo Base64_encode ($data); /* Then you view the source code will get a large string of base64 strings, and then use Base64_decode () restore can get the picture * *
Several common cryptographic functions of PHP