Home > Article > Backend Development > The role of php encryption
The role of php encryption
Encryption: In order to encrypt data, the plain text will not be seen.
In the development process of PHP, it is often necessary to encrypt some data (such as user passwords)
1. Encryption type:
1. One-way hash encryption
is to hash calculation of information of any length to obtain a fixed-length output. This hash calculation process is one-way, that is, the fixed-length output information cannot be calculated to obtain Enter information.
(1) Features: avalanche effect, fixed-length output and irreversibility.
(2) The function is to ensure the integrity of the data.
(3) Encryption algorithm: md5 (standard key length 128 bits), sha1 (standard key length 160 bits), md4, CRC-32
2. Symmetric hash encryption
Symmetric encryption means that encryption and decryption use the same key, or can be calculated from each other.
(1) The encryption party and the decryption party use the same key.
(2) The encryption and decryption speed is relatively fast and is suitable for long-term data use.
(3) The key transmission process is unsafe and easy to be cracked, and key management is also troublesome.
(4) Encryption algorithms: DES (Data Encryption Standard), 3DES, AES (Advanced Encryption Standard, supports encryption of 128, 192, 256, and 512-bit keys), Blowfish.
3. Asymmetric hash encryption
Asymmetric encryption and decryption use different keys. One of them is public and is called the public key. The other is known only to the owner and is called the public key. as private key.
(1) Each user owns a pair of keys for encryption: public key and private key.
(2) Public key encryption, private key decryption; private key encryption, public key decryption.
(3) The process of public key transmission is unsafe and can be easily stolen and replaced.
(4) Since the key length used by the public key is very long, the encryption speed of the public key is very slow, and it is generally not used for encryption.
(5) A certain user uses his private key to encrypt, and other users use their public key to decrypt to achieve the function of digital signature.
(6) Another function of public key encryption is to realize key exchange.
(7) Encryption and signature algorithms: RSA, ELGamal.
(8) Public key signature algorithm: DSA.
Notes:
(1) RSA: Invented by RSA Company, it is a public key algorithm that supports variable-length keys. The length of the file block that needs to be encrypted is also variable;
(2) DSA (Digital Signature Algorithm): Digital signature algorithm, a standard DSS (Digital Signature Standard);
(3) Because asymmetric encryption algorithms run faster than symmetric encryption algorithms The encryption algorithm is much slower. When we need to encrypt a large amount of data, it is recommended to use a symmetric encryption algorithm to increase the encryption and decryption speed.
Symmetric encryption algorithms cannot implement signatures, so signatures can only be asymmetric algorithms.
(4) Since the key management of the symmetric encryption algorithm is a complex process, the management of the key directly determines its security. Therefore, when the amount of data is small, we can consider using an asymmetric encryption algorithm. .
(5) In the actual operation process, the way we usually use is: use an asymmetric encryption algorithm to manage the key of a symmetric algorithm, and then use a symmetric encryption algorithm to encrypt the data, so that we integrate the two types of The advantages of the encryption algorithm are that it not only achieves the advantages of fast encryption speed, but also realizes the advantages of safe and convenient key management.
2. Commonly used encryption functions in php:
1.MD5 encryption:
string md5 ( string $str [, bool $raw_output = false ] )
(1) md5() defaults to 32 Returns the hash value in character hexadecimal digital form. It accepts two parameters. The first is the string to be encrypted, and the second is the Boolean value of raw_output.
The default is false. If set to true, md5() will return the original 16-bit binary format message digest
(2) md5() is a one-way encryption and has no reverse decryption algorithm, but it can still collect some common strings , enumeration, collision and other methods to crack
2.Crypt encryption:
string crypt ( string $str [, string $salt ] )
(1)crypt() accepts two parameters, the first is the string that needs to be encrypted, the second Is the salt value (which is the encryption interference value, if not provided, it is automatically generated by PHP by default);
Returns a hashed string or a string of less than 13 characters, the latter is to distinguish the salt value .
(2)crypt() is one-way encryption, the same as md5.
3.Sha1 encryption:
string sha1 ( string $str [, bool $raw_output = false ])
(1) is very similar to md5. The difference is that sha1() returns a 40-character hash value by default, and the properties of the parameters passed in are the same. The first is the encrypted string,
The second is the Boolean value of raw_output, the default is false, if set to true, sha1() will return the original 20-bit original format message digest
(2)sha1() is also a single-line encryption, and there is no reverse decryption algorithm
4.Urlencode encryption:
string urlencode ( string $str )
(1) One parameter, pass in the string to be encrypted ( Usually used to encrypt URLs)
(2) urlencode is two-way encryption and can be encrypted with urldecode (strictly speaking, it is not a real encryption)
(3) Return characters string, all non-alphanumeric characters in this string except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits, and spaces are encoded as plus signs ( ).
(3) Common urlencode() conversion characters
? => %3F = => %3D % => %25 & => %26 \ => %5C 空格 => %5C
5.base64编码加密:
string base64_decode ( string $encoded_data )
(1)base64_encode()接受一个参数,也就是要编码的数据(这里不说字符串,是因为很多时候base64用来编码图片)
(2)base64_encode()为双向加密,可用base64_decode()来解密
The above is the detailed content of The role of php encryption. For more information, please follow other related articles on the PHP Chinese website!