(Advanced article) several common cryptographic functions of PHP

Source: Internet
Author: User
Tags crypt format message 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.

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 "

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 "

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 "

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 $my_urlencode= "Jellybool.com?jellybool=true + 4-3%5= \& @!" in plaintext data in a URL; Echo UrlEncode ($my _urlencode);//output: Jellybool.com%3fjellybool%3dtrue+%2b+4-3%255%3d+%5c%26+%40%21echo "

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= "Jellybool is a stature has height, the shoulder has the width, the chest muscle has the thickness, the thought has the depth national exemption five a class high quality pseudo front end it male cock silk"; Echo base64_encode ($my _intro); 

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 * *

Above is (Advanced article) PHP several commonly used cryptographic function content, more related content please concern topic.alibabacloud.com (www.php.cn)!

  • 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.