Home > Article > Backend Development > php returns a string encrypted using DES and Blowfish and MD5 algorithms function crypt()
Definition and Usage
The crypt() function returns a string encrypted using the DES, Blowfish or MD5 algorithm.
This function behaves differently on different operating systems, and some operating systems support more than one algorithm type. At installation time, PHP checks what algorithms are available and what algorithms are used.
The exact algorithm depends on the format and length of the salt parameter. Salt can make encryption more secure by increasing the number of strings generated from a specific string with a specific encryption method.
Here are some constants used with the crypt() function. These constant values are set by PHP during installation.
Constant:
[CRYPT_SALT_LENGTH] - Default encryption length. Use standard DES encryption with a length of 2
Note: There is no corresponding decryption function. The crypt() function uses a one-way algorithm.
Syntaxcrypt(str,salt)Parameters Descriptionstr Required. Specifies the string to be encoded. salt Optional. A string used to increase the number of characters being encoded to make the encoding more secure. If no salt argument is provided, one will be randomly generated each time the function is called. Technical detailsReturn value: Returns the encrypted string, or if it fails, returns a string less than 13 characters and guaranteed to be different from the salt. PHP version: 4+
更新日志: 在 PHP 5.3.7 中,新增了 $2x$ 和 $2y$ Blowfish 模式,用来处理潜在的高位攻击。
在 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 自带的算法实现。
实例
实例 1
在本实例中,我们将测试不同的算法:
<?php // 2 character salt if (CRYPT_STD_DES == 1) { echo "Standard DES: ".crypt('something','st')."n<br>"; } else { echo "Standard DES not supported.n<br>"; } // 4 character salt if (CRYPT_EXT_DES == 1) { echo "Extended DES: ".crypt('something','_S4..some')."n<br>"; } else { echo "Extended DES not supported.n<br>"; } // 12 character salt starting with $1$ if (CRYPT_MD5 == 1) { echo "MD5: ".crypt('something','$1$somethin$')."n<br>"; } else { echo "MD5 not supported.n<br>"; } // Salt starting with $2a$. The two digit cost parameter: 09. 22 characters if (CRYPT_BLOWFISH == 1) { echo "Blowfish: ".crypt('something','$2a$09$anexamplestringforsalt$')."n<br>"; } else { echo "Blowfish DES not supported.n<br>"; } // 16 character salt starting with $5$. The default number of rounds is 5000. if (CRYPT_SHA256 == 1) { echo "SHA-256: ".crypt('something','$5$rounds=5000$anexamplestringforsalt$')."n<br>"; } else { echo "SHA-256 not supported.n<br>"; } // 16 character salt starting with $5$. The default number of rounds is 5000. if (CRYPT_SHA512 == 1) { echo "SHA-512: ".crypt('something','$6$rounds=5000$anexamplestringforsalt$'); } else { echo "SHA-512 not supported."; } ?>
上面的代码输出如下(取决于操作系统):
Standard DES: stqAdD7zlbByI Extended DES: _S4..someQXidlBpTUu6 MD5: $1$somethin$4NZKrUlY6r7K7.rdEOZ0w. Blowfish: $2a$09$anexamplestringforsaleLouKejcjRlExmf1671qw3Khl49R3dfu SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9 SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/ oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0.
一、代码
<?php $str = '应用crypt()函数进行单向加密!'; //声明字符串变量$str echo '加密前$str的值为:'.$str; $crypttostr = crypt($str); //对变量$str加密 echo '<p>加密后$str的值为:'.$crypttostr; //输出加密后的变量 ?>
二、运行结果
参数不带salt,每次加密得出的密文都不一样。
加密前$str的值为:应用crypt()函数进行单向加密!
加密后$str的值为:$1$Re4.Gg4.$D.yd00xX0fFfIfp6KrKGN0
The above is the detailed content of php returns a string encrypted using DES and Blowfish and MD5 algorithms function crypt(). For more information, please follow other related articles on the PHP Chinese website!