Several commonly used cryptographic functions of PHP "reprint"

Source: Internet
Author: User
Tags crypt format message md5 encryption md5 hash sha1 sha1 encryption urlencode alphanumeric characters

Transfer from Https://jellybool.com/post/php-encrypt-functions

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 '; echo MD5 ( $username);  " echo MD5 ( $password);  " /* It is more recommended to encrypt important sensitive data multiple times to prevent it from 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 (that is, the encryption interference value, if not provided, it is automatically generated by PHP, the salt parameters are optional. However, if not salt ,crypt () will create a weak password. PHP 5.6 and later will throw an E_notice level error without it. For better security, be sure to specify a salt value that is sufficiently strong ), return a hashed string, or a string that is less than 13 characters in order 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, such as the example of Jellybool as the second parameter is added directly, more than two characters will intercept the first two */echo " Echo Crypt ($password,' $1$jellybool$ ');  Output: $1$jellyboo$dxh7wf7sygrpwb6xbbgfh//* Crypt cryptographic functions have a variety of salt-valued encryption support, the above example shows that the MD5 hash as the salt value, in this way the salt value is added in the form of $1$$, If the Jellybool in the example is added between the last two $ characters, the top eight bits will be truncated, the total length is 12 bits, and the default is this form, which exceeds the eight-bit character. */echo " Crypt also has a variety of salt-valued 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);//输出:c98885c04c1208fd4d0b1dadd3bd2a9ff4d042caecho "//当然,可以将多种加密算法混合使用echo md5(sha1($my_intro));//输出:94f25bf9214f88b1ef065a3f9b5d9874//这种方式的双重加密也可以提高数据的安全性

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%21echo"$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 **========================================================================= solves 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 two variables above: the first Username=jellybool, The second is username=jelly&bool in this case with $_get () to accept the problem, which can be solved in the following way */$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 (no strings are said here, Because many times base64 is used to encode pictures)

2.base64_encode () is bidirectional encryption and can be decrypted with Base64_decode ()

 <?php $my _intro=< Span class= "hljs-string" > "Jellybool is a stature has height, shoulder width, chest muscle has thickness, thought has the depth of the national exemption five a class high quality pseudo front end it male cock silk"; echo base64_encode ( $my _intro);  " /* Output: SMVSBHLCB29S5PIV5LIA5LIQ6LQR5P2Q5PYJ6AUY5BQMLOICQEIGGOACIEWUVEW6PIZOG7JOGOZMNINLJPRLUQYS5OCD5OOZ5PYJ5REX5BQM55QE5ZU9 5a625ywn5qoa5lquqee6p+s8moi0qos8quwjjeerr0lu55s35bgm5lid*/echo Base64_decode (/* 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/*一个图片的应用例子*/$filename="https://worktile.com/img/index/index_video.png";$data=file_get_contents($filename);echo base64_encode($data);/*然后你查看网页源码就会得到一大串base64的字符串,再用base64_decode()还原就可以得到图片*/

Several commonly used cryptographic functions of PHP "reprint"

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.