PHP's several commonly used cryptographic functions, PHP commonly used cryptographic functions _php tutorial

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

PHP's several commonly used cryptographic functions, PHP commonly used cryptographic functions


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);/* It is more recommended 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 second $ with a third $ The eight characters are generated by PHP, each time the refresh is changed */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, the above two characters will intercept the first two */echo ""; Echo crypt ($password, ' $1$jellybool$ ');//output: $1$jellyboo$ dxh7wf7sygrpwb6xbbgfh//* Crypt encryption function has a variety of salt-valued encryption support, the above example shows that the MD5 hash as the salt value, the way the salt value is added in the form of $1$$, such as the example of the Jellybool added in the next two $ characters, Exceeding eight characters will intercept the first eight bits, the total length is 12 bits; crypt is this form by default. */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);//output: C98885c04c1208fd4d0b1dadd3bd2a9ff4d042caecho "";//Of course, Multiple encryption algorithms can be mixed using echo MD5 (SHA1 ($my _intro));//output: 94f25bf9214f88b1ef065a3f9b5d9874//This way 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 for hidden $my of plaintext data in URLs _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= \& @!  The output echo "" of $my_urlencode was restored; $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 right, 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 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 Base64decode (String $encodeddata)
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.base64encode () is bidirectional encrypted and can be decrypted with Base64decode ()

Echo Base64_encode ($my _intro), echo "";/* Output: smvsbhlcb29s5piv5lia5liq6lqr5p2q5pyj6auy5bqmloicqeiggoaciewuvew6pizog7jogozmninljprluqys5ocd5ooz5pyj5rex5bqm55qe5zu95a 625ywn5qoa5lquqee6p+s8moi0qos8quwjjeerr0lu55s35bgm5lid*/echo Base64_decode (' smvsbhlcb29s5piv5lia5liq6lqr5p2q5pyj6auy5bqmloicqeiggoaciewuvew6pizog7jogozmninljprluqys5ocd5ooz5pyj5rex5bqm55qe5zu95a625 Ywn5qoa5lquqee6p+s8moi0qos8quwjjeerr0lu55s35bgm5lid ');/* Output: Jellybool is a stature with height, shoulder width, chest muscle thickness, The mind has a depth of national exemption five A-class high quality pseudo front end it man cock Silk * *

An example of a picture:

<?php/* application Example of a picture */$filename = "Https://worktile.com/img/index/index_video.png"; $data =file_get_contents ($ filename), echo base64_encode ($data);/* Then you can view the Web page source code will get a large string of base64 strings, and then use Base64_decode () restore to get the picture * *

This article was created by Jellybool, the original

http://www.bkjia.com/PHPjc/1098973.html www.bkjia.com true http://www.bkjia.com/PHPjc/1098973.html techarticle PHP's several commonly used cryptographic functions, PHP commonly used cryptographic functions MD5 encryption: String MD5 (String $str [, bool $raw _output = false]) 1.MD5 () by default in 32-character hexadecimal number form ... /c6>

  • Related Article

    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.