Several common encryption functions in PHP and common encryption functions in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags crypt format message md5 hash sha1 encryption alphanumeric characters
Several common encryption functions in PHP and common encryption functions in PHP. Several common encryption functions in PHP. MD5 encryption in PHP: stringmd5 (string $ str [, bool $ raw_outputfalse]) 1. by default, md5 () is a 32-character hexadecimal number. several common encryption functions of PHP are supported.

MD5 encryption:

String md5 (string $ str [, bool $ raw_output = false])

1. by default, md5 () returns a hash value in the hexadecimal format of 32 characters. it accepts two parameters: the first is the string to be encrypted, and the second is the Boolean value of raw_output, the default value is false. if it is set to true, md5 () will return the original 16-bit binary message digest.

2. md5 () is one-way encryption, and there is no reverse decryption algorithm. However, it can still be used to crack common strings through methods such as collection, enumeration, and collision.

<? Php $ username = 'jellybool '; $ password = 'jellybool. com ';/* encrypt strings in md5 mode */echo md5 ($ username); echo ""; echo md5 ($ password); echo ""; /* we recommend that you encrypt important sensitive data multiple times to prevent cracking. */echo md5 (md5 ($ password);/* the output is username: authorization password: 7bf02cf0f4af6da4accbc73d2a175476 password (encrypted twice): 864704bb35754f8cd0232cba6b91521b */

Crypt encryption:

String crypt (string $ str [, string $ salt])

1. crypt () accepts two parameters: the first is the string to be encrypted, and the second is the salt value (that is, the encrypted interference value. if not provided, it is automatically generated by PHP by default ); returns the hash string or a string less than 13 characters, which is used to distinguish the salt value.
2. crypt () is one-way encryption, which is the same as md5.

<? Php $ password = 'jellybool. com '; echo crypt ($ password); // output: $1 $ Fe0.qr5. $ WOhkI4/5VPo7n7TnXHh5K/* the eight characters between the second $ and the third $ are generated by PHP and changed once every refresh */echo ""; echo crypt ($ password, "jellybool"); // output: je7fNiu1KNaEs/* when we want to add a custom salt value, for example, jellybool in the example is directly added as the second parameter, the first two characters */echo ""; echo crypt ($ password, '$1 $ jellybool $') will be intercepted. // output: $1 $ jellyboo $ dx1_wf7sygrpwb6xbbgfh // * The crypt encryption function supports encryption of multiple salt values. the preceding example shows the MD5 hash as the salt value, in this method, the salt value is added as $1 $. for example, the value of jellybool in the example is Between the last two $ characters, the first eight characters are intercepted and the total length is 12 characters. crypt is in this form by default. */Echo ""; // crypt supports encryption of multiple salt values. for details, see the manual.

Sha1 encryption:

String sha1 (string $ str [, bool $ raw_output = false]

1. unlike md5, sha1 () returns a 40-character hash value by default. the input parameter is of the same nature, and the first is an encrypted string, the second is a Boolean value of raw_output. the default value is false. if it is set to true, sha1 () will return the original 20-bit original format message digest.
2. sha1 () is also single-line encryption, with no reverse decryption algorithm

<? Php $ my_intro = "jellybool"; echo sha1 ($ my_intro); // output: c98885c04c1208fd4d0b1dadd3bd2a9ff4d042caecho ""; // of course, multiple Encryption algorithms can be mixed with echo md5 (sha1 ($ my_intro); // output: 94f25bf9214f88b1ef065a3f9b5d9874 // this method of double encryption can also improve data security

Urlencode encryption:

String urlencode (string $ str)
1. a parameter that is used to pass in the string to be encrypted (usually used to encrypt the URL ),
2. urlencode is bidirectional encryption, which can be encrypted using urldecode (strictly speaking, it is not actually encrypted)
3. returns a string -_. all other non-alphanumeric characters will be replaced with a semicolon (%) followed by two hexadecimal numbers, and spaces will be encoded as the plus sign (+ ).

<? Php // urlencode () is usually used to hide plaintext data in a URL $ my_urlencode = "jellybool.com? Jellybool = true + 4-3% 5 = \&@! "; Echo urlencode ($ my_urlencode); // output: jellybool.com % 3 Fjellybool % 3 Dtrue + % 2B + 4-3% 255% 3D + % 5C % 26 + % 40% 21 echo ""; $ my_urldecode = "jellybool.com % 3 Fjellybool % 3 Dtrue + % 2B + 4-3% 255% 3D + % 5C % 26 + % 40% 21"; echo urldecode ($ my_urldecode ); // output: jellybool.com? Jellybool = true + 4-3% 5 = \&@! // Restored the output echo of $ 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 Tired and unloving & tn = 98242547_hao_pg & ie = UTF-8 yes, this is why Baidu search for jellybool. * // * = ========================================================== =========solve the second Classic problem ================================== ========================================================== =====*/$ pre_url_encode = "jellybool.com? Username = jellybool & password = jelly "; // in actual development, we often need 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, $ _ GET () is used to accept the request, this can be solved using the following method */$ username = "jelly & bool"; $ url_decode = "jellybool.com? Username = ". urlencode ($ username)." & password = jelly "; // This is a good solution/* summarize the conversion characters of common urlencode? ==>% 3F ==>%3d %=>%25 ==>%26 \=> % 5C +=> % 2B space =>+ */

Base64 encryption:

String base64decode (string $ encodeddata)
1. base64_encode () accepts a parameter, that is, the data to be encoded (this is not a string here, because base64 is often used to encode images)
2. base64encode () is bidirectional encryption and can be decrypted using base64decode ().

Echo base64_encode ($ my_intro); echo "";/* output: Response + response */echo base64_decode ('audio A5LqUQee6p + response ');/* output: jellyBool is a body with height, width on the shoulders, thickness on the chest muscles, and depth of thinking 5 A-level quality fake front-end IT male silk free of inspection */

Example of an image:

<? Php/* example of an Image App */$ filename = "https://worktile.com/img/index/index_video.png"; $ data = file_get_contents ($ filename); echo base64_encode ($ data ); /* then you can view the source code of the webpage to get a large string of base64, and then restore it with base64_decode () to get the image */

This article was created by JellyBool.

Encryption MD5: string md5 (string $ str [, bool $ raw_output = false]) 1. md5 () is a 32-character hexadecimal number by default...

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.