


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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
