当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > crypt()函数
The crypt() function returns a string encrypted using DES, Blowfish, or MD5 algorithms.
crypt()函数的作用是:使用DES、Blowfish或MD5返回一个不可逆加密(散列)。
This function behaves different on different operating systems, some operating systems supports more than one type of encryption. PHP checks what algorithms are available and what algorithms to use when it is installed.
这个函数的属性是随着操作系统的不同而不同的。有些操作系统支持多于一中的加密方法。PHP将确认什么运算法则是有效的以及到底该使用什么运算法则。
The exact algorithm depends on the format and length of the salt parameter. Salts help make the encryption more secure by increasing the number of encrypted strings that can be generated for one specific string with one specific encryption method.
精确的运算法则取决于salt参数的格式和长度。Salts可以通过增加加密字符串的数量保证加密更加安全(这些额外增加的字符将通过一种指定的加密运算方法产生)。
There are some constants that are used together with the crypt() function. The value of these constants are set by PHP when it is installed.
下面列举了一系列常量,它们是与crypt()函数一起使用的。当PHP安装完毕后,这些常量将自动被设置好。
Constants:
常量:
crypt(str,salt) |
Parameter参数 | Description描述 |
---|---|
str | Required. Specifies the string to be encoded 必要参数。指定需要被编码的字符串 |
salt | Optional. A string used to increase the number of characters encoded, to make the encoding more secure. If the salt argument is not provided, one will be randomly generated by PHP each time you call this function. 可选参数。用来增加编码字符的字符串,它可以使设置的密码更加安全。如果不支持slat自变量,那么它将在你请求这个函数时通过PHP随机产生 |
Note: There is no decrypt function. The crypt() function uses a one-way algorithm.
注意:它不是加密函数。crypt()函数使用的是单行道式的运算法则[one-way algorithm]。
In this example we will test the different algorithms:
下面的案例将测试不同的运算法则:
<?php if (CRYPT_STD_DES == 1) { echo "Standard DES: ".crypt("hello world")."n<br />"; } else { echo "Standard DES not supported.n<br />"; } if (CRYPT_EXT_DES == 1) { echo "Extended DES: ".crypt("hello world")."n<br />"; } else { echo "Extended DES not supported.n<br />"; } if (CRYPT_MD5 == 1) { echo "MD5: ".crypt("hello world")."n<br />"; } else { echo "MD5 not supported.n<br />"; } if (CRYPT_BLOWFISH == 1) { echo "Blowfish: ".crypt("hello world"); } else { echo "Blowfish DES not supported."; } ?> |
The output of the code above could be (depending on the operating system):
上述代码将输出下面的结果(根据操作系统的不同而不同):
Standard DES: $1$r35.Y52.$iyiFuvM.zFGsscpU0aZ4e. Extended DES not supported. MD5: $1$BN1.0I2.$8oBI/4mufxK6Tq89M12mk/ Blowfish DES not supported. |