Summary: Cryptography is a technical science that studies the preparation password (we are referred to as encryption: Encode) and decipher password (we call it decrypt: decode). The objective law of the password change is applied to the preparation of passwords to keep communication secrets. It is called coding; applied to the deciphering password to obtain communication intelligence, referred to as decipherive learning, generally called cryptography. Normally, people will be able to understand the text called clear text; refer to the text-converted text-converted text called ciphertext. The process of transforming the plain text into a ciphertext is called encryption; its inverse process, that is, the process of transforming the ciphertext into a clear text. What data encryption is provided in PHP? PHP provides a CRYPT () function completes encryption: String Crypt (String Input_String [, String Salt]) This function completes the function called a single-way encryption, that is, it can encrypt some codes, but cannot password Convert to the original codes. Once one-way encrypted passwords have fallen into third-party people, because they cannot be reduced as plaintext, there is no big use. When verifying the password entered by the user, the user's input is also a one-way algorithm. If the input is matched to the stored encrypted password, the input password must be correct. The input_string parameter of this function is a string that needs to be encrypted. The second parameter SALT is a bit string that can affect the encrypted dark code and further exclude the likelihood that is called pre-counting attacks. By default, PHP uses a 2-character DES interference string, if your system uses MD5, it uses a 12-character interference string. We can find the length of the interference string to be used by performing the following command: Print "The length of the interference string used is:". Crypt_salt_length; crypt () supports four algorithms, the following is the algorithm it supports and the corresponding SALT The length of the parameter: [Note: The following table] 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 $ PHP data encryption function to User Authentication? We use CRYPT () to implement user authentication. For example, we use a PHP program to restrict access to a directory, only allow registered users to access this directory. We put the data store a table (this data table name members): mysql> Create Table Members (-> Username Char (14) Not Null, -> Password Char (32) Not Null, -> Primary Key UserName) ->); then we can enter users' data into this table: Username Password Tom Kelod1c377lke John Ba1t7vnz9awgk Bill Paluvrwsrlz4u These encrypted passwords The codes are Tom, John and Bill, respectively.
We will create interference strings based on the first two letters of password: $ salt = substr ($ EnteredPassword, 0, 2); $ userpswd = crypt ($ EnteredPassword, $ SALT); // $ usrpswd then and username Crypt () and Apache stored together in mysql - response verification system Application Php $ host = "localhost"; // Host $ usrname = "Tom"; // User name $ passwd = "Hello World"; // Password $ db = "users"; // Database name // Set whether to pass the verification flag, whether the default is $ authorization = 0; // prompts the user to enter account and password IF (isset ($ PHP_AUTH_USER) && isset ($ PHP_AUTH_PW )) {Mysql_pconnect ($ Host, $ Username, $ Passwd) or Die ("Cannot connect to MySQL Server!"); Mysql_select_db ($ dB) or Die ("You can't choose Database!"); // During encryption $ SALT = substr ($ PHP_AUTH_PW, 0, 2); $ encrypted_pswd = crypt ($ PHP_AUTH_PW, $ salt); // SQL query $ query = "SELECT username FROM members WHERE username = / '$ PHP_AUTH_USER /' AND password = / '$ Encrypted_pswd / '"; // Execute Query IF (mysql_numrows) == 1) {$ Authorization = 1;}}}} (! $ authorization) {header (/' www-authenticate: Basic realm =" User Authentication" / '); Header (/' http / 1.0 401 unauthorized / '); Print "Unable to pass verification"; exit;} else {print "Encrypted";}?> Crypt () used in the default state is not The safest, so if you need a higher security, you need more than other better algorithms, such as MD5 (), which uses the MD5 hash algorithm. How to encrypt in MD5? The function encrypted in the PHP has MD5 (), one of its effects is mixed. A mixed function can convert a variable length to a fixed length that is mixed, also referred to as "information", which is useful because a fixed length string can be used to check File integrity and verification digital signature and user identity. The PHP built-in MD5 () mixed function will convert a variable length information to a 128-bit (32 characters) information.