Encrypt in PHP

Source: Internet
Author: User

PHP provides the possibility of using its crypt () function to complete a one-way encryption function.
String crypt (String input_string [, string salt])
The input_string parameter is a string that needs to be encrypted, and the second optional salt is a
A single string that can affect the encrypted cipher and further exclude the possibility of being called an estimated attack.
Of By default, PHP uses a 2-character des jamming string if your system is using a
MD5, it will use a 12-character jamming string. By the way, you can do this by following your life
To find the length of the jamming string that the system will use:
Print "My system salt size is:". Crypt_salt_length;
The system may also support other cryptographic algorithms. Crypt () supports four algorithms, the following is its support
The algorithm and the corresponding salt parameter
Length of the number:
Algorithm Salt length
Crypt_std_des 2-character (Default)
Crypt_ext_des 9-character
Crypt_md5 12-character beginning with $1$
Crypt_blowfish 16-character beginning with $2$
Implementing user authentication with crypt ()
As an example of the crypt () function, consider a situation where you want to create a section of PHP
The Scripting program restricts access to a directory, allowing only the correct username and password to be provided
User to access this directory. I'll store the data in a table in MySQL, my favorite database. Below
Let's start with our example of creating this table called Members:
Mysql>create TABLE Members (
->username CHAR () not NULL,
->password CHAR (not NULL),
->primary KEY (username)
->);
We then assume that the following data is already stored in the table:
User name password
Clark Kelod1c377lke
Bruce Ba1t7vnz9awgk
Peter Paluvrwsrlz4u
These encrypted passwords correspond to the plaintext of Kent, banner and Parker. Take a look at every
The first two letters of the password, which is because I use the following code, according to the first two words of the password
The parent creates the interfering string:
$enteredPassword.
$salt = substr ($enteredPassword, 0, 2);
$userPswd = Crypt ($enteredPassword, $salt);
$USERPSWD is then stored in MySQL with the user name
I will use the Apache password-answer authentication configuration to prompt the user for a username and password, a
The little-known information about PHP is that it can take Apache's password-answering system to enter the user
The name and password are identified as $php_auth_user and $PHP_AUTH_PW, and I will use this in the authentication script
To these two variables.

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.