Several common cryptographic functions of PHP

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

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.

turn from: Https://jellybool.com/post/php-password-hash-in-the-right-way

MD5 encryption:

String MD5 (string $str [, bool $raw _output = false])

1.md5 () returns the hash value as a 32-character hexadecimal number by default, which accepts two arguments, the first character to be encrypted String, the second is a Boolean value of Raw_output, the default is False, and if set to TRUE,MD5 () returns the original 16-bit binary format report digest to

2.md5 () is one-way encryption, there is no reverse decryption algorithm, However, it is possible to crack

on some common strings by collecting, enumerating, colliding, and so on.
<?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 (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 in the example of Jellybool as the second parameter is added directly, more than two characters will intercept the first two bits */echo  " $password,  ' $1$jellybool$ ');  Output: $1$jellyboo$dxh7wf7sygrpwb6xbbgfh//* Crypt encryption function has a variety of salt value encryption support, the above example shows the MD5 hash as the salt value, In this way, the salt value is added in the form of $1$$, such as the example of Jellybool added between the last two $, the above eight characters will intercept the first eight bits, the total length is 12 bits; Crypt default is this form. */echo  " Crypt also has a variety of salt value encryption support, see 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, as in the case of an incoming parameter, the first 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 original format digest to

2.sha1 () is also a single-line encryption with no reverse decryption algorithm

<?php $my _intro=echo SHA1 ( $my _intro); echo  "//of course, a variety of cryptographic algorithms can be mixed using echo MD5 (SHA1 (  $my _intro)); //output: 94f25bf9214f88b1ef065a3f9b5d9874// Double encryption in this way can also improve the security of the 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%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 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 with 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 (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=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/* a picture application example */$ Filename= "https://worktile.com/img/index/index_video.png";  $filename);  $data);  

Several common cryptographic functions for PHP

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.