Crypt () function

Source: Internet
Author: User
Tags crypt md5 hash printable characters rounds

Data encryption

Data encryption uses cryptography technology to encrypt information, realize the concealment of information, and thus play a role in protecting information security. It uses the encryption algorithm and the encryption key to process the original plaintext file or data, so that its written unreadable code, known as "ciphertext", to achieve the security of data protection.

Crypt () function

The crypt () function returns a string that is encrypted using the DES, Blowfish, or MD5 algorithms. On different operating systems, the crypt () function behaves differently, and some operating systems support more than one type of algorithm. At the time of installation, PHP recalls checking what algorithms are available and what algorithms are used.

Syntax for the crypt () function:

String crypt (string str[,string Salt]);

Parameter str: required. is a string that needs to be encrypted.

Parameter salt: Optional. Used to increase the number of encrypted characters (interference string) to make the character more secure. If the salt parameter is not provided, a disturbance string is randomly generated each time the function is called.

Algorithm for the Crypt () function:

[Crypt_salt_length] The default encryption length. Uses standard DES encryption with a length of 2
[Crypt_std_des] Hashes based on the standard DES algorithm use the two characters in the "./0-9a-za-z" character as the salt value. Using illegal characters in salt values will cause crypt () to fail.
[Crypt_ext_des] Extended DES algorithm-based hashing. A string with a salt value of 9 characters, consisting of 1 underscores followed by 4-byte cycles and 4-byte salt values. They are encoded into printable characters, each character 6 bits, and the effective bit is the least preferred. 0 to 63 are encoded as "./0-9a-za-z". Using illegal characters in salt values will cause crypt () to fail.
[CRYPT_MD5] The MD5 hash uses a string salt value of 12 characters starting with $1$.
[Crypt_blowfish] The Blowfish algorithm uses the following salt values: "$2a$", a two-bit cost parameter, "$", and a string of 64 bits that are combined by the characters in "./0-9a-za-z". Using a character outside of this range in the Salt value causes crypt () to return an empty string. The two-bit cost parameter is the logarithm of the number of cycles with a base of 2, and its range is 04-31, and exceeding this range will cause crypt () to fail.
crypt_sha256 The SHA-256 algorithm uses a 16-character string salt value that begins with $5$ to hash. If the salt string begins with "rounds=<n>$", the numeric value of N is used to specify the number of executions of the hash loop, much like the cost parameter of the Blowfish algorithm. The default number of cycles is 5000, the minimum is 1000, and the maximum is 999,999,999. N that exceeds this range will be converted to the nearest value.
crypt_sha512 The SHA-512 algorithm uses a 16-character string salt value that begins with $6$ to hash. If the salt string begins with "rounds=<n>$", the numeric value of N is used to specify the number of executions of the hash loop, much like the cost parameter of the Blowfish algorithm. The default number of cycles is 5000, the minimum is 1000, and the maximum is 999,999,999. N that exceeds this range will be converted to the nearest value.

On systems where the function supports multiple algorithms, set to "1" if the above constants are supported, otherwise set to "0".

<?php         $str = "This is example";            The value before Echo ' encryption is: '. $str;         $crypttostr =crypt ($STR);         Echo ' <p> $str value after encryption is: '. $crypttostr;?>

The running results show:

From two run result graphs, it can be found that the encryption results generated after the refresh are different, which makes it a problem to judge the encrypted data. And we also know that the crypt () function is a one-way function, and ciphertext cannot be reverted to plaintext. And the data after each encryption is different, this is the salt parameter to solve the problem. The crypt () function encrypts the plaintext with the salt parameter, and when it is judged, the output information is encrypted again using the same salt parameter, and the judgment of the result is compared with two times of encryption.

Note: the crypt () function does not have a corresponding decoding function, it is a one-way cryptographic function.

Crypt () function

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.