


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
- ##[CRYPT_STD_DES] - Standard DES based encryption with a 2 character salt from the alphabet "./0-9A-Za-z ". Using invalid characters in salt will cause the function to fail.
- [CRYPT_EXT_DES] - Extended DES-based encryption with a 9-character salt consisting of an underscore, followed by a 4-byte iteration number and a 4-byte salt. . These are encoded as printable characters, 6 bits each, least significant character first. Values 0 to 63 are encoded as "./0-9A-Za-z". Using invalid characters in salt will cause the function to fail.
- [CRYPT_MD5] - MD5 encryption with a 12-character salt, starting with $1$.
- [CRYPT_BLOWFISH] - Blowfish encryption has a salt starting with $2a$, $2x$ or $2y$, a two-digit cost parameter "$", and from letters 22 characters in the table "./0-9A-Za-z". Using characters outside the alphabet will cause the function to return a string of length 0. The "$" parameter is the base 2 logarithm of the number of iterations of the Blowfish hashing algorithm and must be in the range 04-31. Values outside this range will cause the function to fail.
- [CRYPT_SHA_256] - SHA-256 encryption with a 16-character salt, starting with $5$. If the salt string starts with "rounds=
$", the numeric value of N is used to represent the number of times the hashing round is executed, similar to the cost parameter in Blowfish. The default number of loops is 5000, the minimum value is 1000, and the maximum value is 999,999,999. Any value of N outside this range will be converted to the nearest boundary value. - [CRYPT_SHA_512] - SHA-512 encryption with a 16-character salt, starting with $6$. If the salt string starts with "rounds=
$", the numeric value of N is used to represent the number of times the hashing round is executed, similar to the cost parameter in Blowfish. The default number of loops is 5000, the minimum value is 1000, and the maximum value is 999,999,999. Any value of N outside this range will be converted to the nearest boundary value.
Note: There is no corresponding decryption function. The crypt() function uses a one-way algorithm.
Syntaxcrypt(str,salt)
更新日志: 在 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
