当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > crypt()函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 crypt()函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 509 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

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_SALT_LENGTH] - The length of the default encryption. With standard DES encryption, the length is 2
    [CRYPT_SALT_LENGTH] –默认的加密长度。标准的DES长度为2
  • [CRYPT_STD_DES] - Value is 1 if the standard DES algorithm is supported, 0 otherwise. The Standard DES-based encryption has a two character salt
    [CRYPT_STD_DES] –如果支持标准的DES运算法则,那么值为1;如果不支持,值为0。标准的基于DES的加密方法包含两个字符salt
  • [CRYPT_EXT_DES] - Value is1 if the extended DES algorithm is supported, 0 otherwise. The Extended DES encryption has a nine character salt
    [CRYPT_EXT_DES] -如果支持标准的DES运算法则,那么值为1;如果不支持,值为0。标准的基于DES的加密方法包含九个字符salt
  • [CRYPT_MD5] - Value is 1 if the MD5 algorithm is supported, 0 otherwise. The MD5 encryption has a 12 character salt starting with $1$
    [CRYPT_MD5] -如果支持标准的DES运算法则,那么值为1;如果不支持,值为0。MD5加密方法包含12个字符salt,起始字符为“$1$”
  • [CRYPT_BLOWFISH] - Value is 1 if the Blowfish algorithm is supported, 0 otherwise. The Blowfish encryption has a 16 character salt starting with $2$ or $2a$
    [CRYPT_BLOWFISH] -如果支持标准的DES运算法则,那么值为1;如果不支持,值为0。Blowfish加密方法包含16个字符salt,起始字符为“$2$”和“$2a$”

Syntax
语法

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随机产生


Tips and Notes
注意点

Note: There is no decrypt function. The crypt() function uses a one-way algorithm.
注意:它不是加密函数。crypt()函数使用的是单行道式的运算法则[one-way algorithm]。


Example 1
案例1

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. 

评论 (0) All

登陆 | 还没注册?