Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP crypt() Function

PHP crypt() Function


PHP String Reference Manual PHP String Reference Manual


Definition and Usage

The crypt() function returns an encrypted string using the DES, Blowfish or MD5 algorithm.

On different operating systems, this function behaves differently; some operating systems support more than one algorithm type. At install time, PHP checks which algorithms are available and which to use.

The exact algorithm depends on the format and length of the salt parameter. The salt can make the encryption more secure by increasing the number of strings generated by a specific string combined with a specific encryption method.

Here are some constants used with the crypt() function. These constant values are set by PHP at install time.

Constants:

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

**Note:** There is no corresponding decryption function. The crypt() function uses a one-way algorithm.


Syntax

crypt(

str,salt

)

Parameters Description
str Required. Specifies the string to encode.
salt Optional. A string used to increase the number of encoded characters, making the encoding more secure. If the salt parameter is not provided, a random one is generated each time the function is called.

Technical Details

Return Value: Returns the encrypted string, or on failure a string of less than 13 characters that is guaranteed to be different from the salt.
PHP Version: 4+
Changelog: In PHP 5.3.7, the $2x$ and $2y$ Blowfish modes were added to handle potential high-bit attacks.
 在 PHP 5.3.2 中,新增了常量 SHA-256 和 SHA-512。       
   
 自 PHP 5.3.2 起,Blowfish 在无效的循环将返回 "failure" 字符串("*0" 或 "*1"),而不是后退到 DES。       
   
 自 PHP 5.3.0 起,PHP 自带 MD5 加密实现、标准 DES 实现、扩展 DES 实现以及 Blowfish 算法。如果系统不支持上述的算法,将使用 PHP 自带的算法实现。 |

Example

Example 1

Example 2

Use htpasswd for crypt() encryption:

Example 1

In this example, we use with different hash types:

The output of the above code is as follows (depending on the operating system):


Standard DES: rl.3StKT.4T8M
Extended DES: _J9..rasmBYk8r9AiWNc
MD5:          $1$rasmusle$rISCgZzpwk3UhDidwXvin0
Blowfish:     $2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi
SHA-256:      $5$rounds=5000$usesomesillystri$KqJWpanXZHKq2BOB43TSaYhEWsQ1Lr5QNyPCDH/Tp.6
SHA-512:      $6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21

PHP String Reference Manual PHP String Reference Manual