Summary of PHP encryption methods

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

There are 4 ways to encrypt PHP, in addition to the encryption and decryption of URLs. Hope can be useful for your development.

Incidentally, I will be in the back of the whole set of Css3,php,mysql development Notes pack to Baidu Cloud, there is a need to go directly to the Baidu cloud download, so that after you develop can directly turn the notes without Baidu search so troublesome.

Note Link: Http:// Password: pvj2

First, MD5 () encryption technology

String MD5 (string $str [, bool $raw _output=false]): Calculates the MD5 hash value of STR

$STR: Raw String

$raw _output: If the optional raw_output is set to true, then the MD5 digest will be returned in the original binary format of 16 bytes in length. Returns the hash value returned as a 32-character hexadecimal number.

Note: The MD5 is irreversible, so it can only force decoding. This means that after encryption, the password cannot be parsed by other functions, only the corresponding original characters can be resolved by the 32-bit characters of the third-party matching database.

  Decrypt website:


<?phpheader ("content-type:text/html;charset= ' UTF8 '"); Set the encoding echo MD5 ("DFA");//1b014086a5cf92eb3238d0d45c8c61a4echo "<br>"; Echo MD5 ("DFA", $raw _output=true); Output: @???? 28?? \?a?5owv6k++572rlwtpbg== $STR =1;echo MD5 (MD5 ($STR)); Can be encrypted multiple times if ($STR) {echo "Registration successful, 3 seconds after the jump to the login page"; echo "<meta http-equiv= ' refresh ' content= ' 3;url=login.html '/>"; 3 seconds after the page is refreshed, and then jump to Login.html}else{echo "registration failed, please re-register"; echo "<meta http-equiv= ' refresh ' content= ' 1;url=reg.html '/ > ";}? >


Second, crypt encryption algorithm
String crypt (String $str [, String $salt]);
Returns a hash string based on the standard Unix des algorithm or other available alternative algorithms on the system.
$STR: Confidential plaintext required
$salt: Encrypt the interference string, make the encoding more secure

If the $salt parameter is not added to the encryption, a disturbance string is randomly generated, otherwise the encryption text will be refreshed.


<?phpecho crypt ("Imooc"), echo "


Three, SHA1 () encryption algorithm
String SHA1 (String $str [, bool $raw _output=false]): Computes the SHA1 hash value of a string
$STR: Encrypted string
$raw _output: If the optional raw_output parameter is set to True, the SHA1 digest is returned in the original format of 20-character length, otherwise the return value is a 40-character hexadecimal number.
Returns a SHA1 hash value string.


<?phpecho SHA1 ("LEOHC"); echo "


Four, URL encoding encryption technology
  1.urlencode (String $str): encoded URL string
$STR: The string to encode
Return value: Returns the encoded string

  2.urldecode (String $str): Decodes the encoded URL string.
$STR: the string to decode
Return value: Returns the decoded string

Encoding specification: All non-alphanumeric characters except-_ in this string are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+)

  3.rawurlencode (String $str): Encode URLs by RFC1738
$STR: The URL to encode
Return value: Return string, encode space as%20

  4.rawurldecode (String $str): Decodes an encoded URL string
$STR: the URL to decode
Return value: Returns a string in which the percent percent in this string followed by a two-bit hexadecimal sequence is replaced with the literal character


<?php/* source code conversion code? %3f=%3d space +%%25&%26\%5c*/$str = "h e l lo World"; Echo UrlEncode ($STR); echo "


Five, BASE64 information coding encryption Technology
  1.base64_encode (String $data): Encode data using base64
$data: The data to encode

  2.base64_decode (String $data [, bool $strict =false]): Decodes data encoded with MIME base64

$strict: Returns False if the input data exceeds the Base64 alphabet;


<?php$data= "-kin", Echo Base64_encode ($data), echo "


Vi. Classification and summary of Information encryption technology
Information encryption:
  1. Single Hash Encryption
Single Hash Encryption technology introduction:
One-way hash encryption refers to the calculation of the length of a fixed-length output by hashing the information of different input lengths, and the hash calculation process is unidirectional, that is, the output of fixed length cannot be computed and the input information is obtained.

PlainText-----> One-way hashing algorithm--Ciphertext--->

2. Symmetric hashing encryption
Symmetric encryption Technology Introduction:
When you heap encryption, the keys used for encryption and decryption are the same key or can be extrapolated to each other.
Salt key
PlainText-----> Encryption algorithm--Ciphertext---> Decryption algorithm----clear

3. Asymmetric Hash Encryption
Asymmetric encryption and decryption use a key that is not the same key, one of which is public, called the public key, and the other is known only by the owner.

Encryption Key Decryption key
PlainText-----> Encryption algorithm--Ciphertext---> Decryption algorithm----clear

Hope to be helpful during the development process.

attached Note Link: Http:// Password: pvj2

Summary of PHP encryption methods

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