The interview is also a wrong answer, the interviewer asked me what are the classical algorithms in asymmetric encryption algorithm? At that time I froze a bit, because I confuse the asymmetric encryption with the concept of single-hash encryption, so let alone what is the asymmetric encryption algorithm in the classical algorithm, the result of course also lets the interviewer Leng a bit, so today spend some time to talk about the information encryption technology in PHP
Classification of information encryption technology
Single hash encryption technology (irreversible encryption)
belongs to the digest algorithm, is not a cryptographic algorithm, the function is to change any long input string into a fixed long output string of a function
MD5
String MD5 (string $str [, bool $raw _output = false]); MD5 encryption, enter any length string to return a unique 32-bit character
MD5 () is a one-way encryption, there is no reverse decryption algorithm, but still can be some common string through collection, enumeration, collision and other methods to crack; So in order to make it more troublesome to crack, so we generally add a little salt value (salt) and double MD5;
MD5 (MD5 ($password). ' Sdva ');
Sdva is the salt value, the salt value should be random, such as MD5 commonly used in password encryption, so at the time of registration I will randomly generate this string, and then through the above method to double encryption;
Crypt
It is rare to see someone using this function, if it is to be used in a symmetric or asymmetric algorithm, to understand both;
String crypt (String $str [, String $salt])//First is a string that needs to be encrypted, the second is a salt value (which is the encryption interference value, if not provided, it is automatically generated by PHP); Returns the hashed string or a string less than 13 characters. The latter in order to distinguish the salt value
Asymmetric encryption
The asymmetric encryption algorithm requires two keys for encryption and decryption, both of which are the public key (publicly key, the public key) and the private key, or the secret key (private key);
, the secure transmission of important information is accomplished by using asymmetric encryption between A and B.
Party B generates a pair of keys (public and private) and exposes the public key to other parties.
The party that obtains the public key uses the key to encrypt the confidential information before sending it to party B.
Party B decrypts the encrypted information with another private key (private key) that it saves. Party B can only use its private key (private key) to decrypt the information encrypted by the corresponding public key.
In the transmission process, even if the attacker intercepts the transmitted ciphertext and obtains the public key of B, the cipher cannot be cracked, because only the private key of B can decrypt the text.
Similarly, if B to reply to the encrypted information to a, then need to publish a public key to B for encryption, a self-preservation of a private key for decryption.
The main algorithms used in asymmetric cryptography are: RSA, Elgamal, knapsack algorithm, Rabin, D-h, ECC (elliptic curve encryption algorithm), etc. one of the most common algorithms we see is the RSA algorithm
Here is an excerpt from the Internet a PHP algorithm for asymmetric encryption via OpenSSL
_keypath = $path; }/** * Create the key pair,save the key to $this->_keypath */Public Function CreateKey () { $r = Openssl_pkey_new (); Openssl_pkey_export ($r, $privKey); File_put_contents ($this->_keypath. Directory_separator. ' Priv.key ', $privKey); $this->_privkey = Openssl_pkey_get_public ($privKey); $RP = Openssl_pkey_get_details ($r); $pubKey = $rp [' key ']; File_put_contents ($this->_keypath. Directory_separator. ' Pub.key ', $pubKey); $this->_pubkey = Openssl_pkey_get_public ($pubKey); }/** * Setup the private key */Public Function Setupprivkey () {if (Is_resource ($this->_pri Vkey)) {return true; } $file = $this->_keypath. Directory_separator. ' Priv.key '; $PRK = file_get_contents ($file); $this->_privkey = openssl_pkey_get_private ($PRK); return true; }/** * Setup the public key */Public Function Setuppubkey () {if (Is_resource ($this->_pubkey)) {return true; } $file = $this->_keypath. Directory_separator. ' Pub.key '; $puk = file_get_contents ($file); $this->_pubkey = Openssl_pkey_get_public ($PUK); return true; }/** * Encrypt with the private key */Public Function Privencrypt ($data) {if (!is_string ($dat A) {return null; } $this->setupprivkey (); $r = Openssl_private_encrypt ($data, $encrypted, $this->_privkey); if ($r) {return base64_encode ($encrypted); } return null; }/** * Decrypt with the private key */Public Function Privdecrypt ($encrypted) {if (!is_string ($encrypted)) {return null; } $this->setupprivkey (); $encrypted = Base64_decode ($encrypted); $r = Openssl_private_decrypt ($encrypted, $decrypted, $this->_privkey); if ($r) {return $decrypted; } return null; }/** * Encrypt with public key */Public Function Pubencrypt ($data) {if (!is_string ($data)) { return null; } $this->setuppubkey (); $r = Openssl_public_encrypt ($data, $encrypted, $this->_pubkey); if ($r) {return base64_encode ($encrypted); } return null; }/** * Decrypt with the public key */Public Function Pubdecrypt ($crypted) {if (!is_string ($CR ypted)) {return null; } $this->setuppubkey (); $crypted = Base64_decode ($crypted); $r = Openssl_public_decrypt ($crypted, $decrypted, $this->_pubkey); if ($r) {return $decrypted; } return null; Public Function __destruct () {@fclose ($this->_privkey); @fclose ($this->_pubkey); }}//The following is a simple test demo, if not required please delete $rsa = new RSA (' Ssl-key '); Private key encryption, public key decryption echo ' Source: I'm an old turtle.
'; $pre = $rsa->privencrypt (' I am an old turtle '); Echo ' Private encrypted:
' . $pre. '
'; $pud = $rsa->pubdecrypt ($pre); Echo ' Public decrypted: '. $pud. '
'; Public key encryption, private key decryption echo ' Source: Dry it
'; $pue = $rsa->pubencrypt (' dry it '); echo ' Public encrypt:
' . $pue. '
'; $PRD = $rsa->privdecrypt ($pue); Echo ' Private decrypt: '. $PRD;?> Symmetric encryption algorithm
Symmetric encryption (also known as private key encryption) refers to encrypting and decrypting cryptographic algorithms that use the same key. Sometimes called a traditional cryptographic algorithm, the encryption key can be inferred from the decryption key, and the decryption key can also be inferred from the encryption key. In most symmetric algorithms, the encryption key and decryption key are the same, so the encryption algorithm is also called the secret key algorithm or single key algorithm. It requires the sender and receiver to agree on a key before communicating securely. The security of a symmetric algorithm relies on the key, which means that anyone can decrypt the message they send or receive, so the confidentiality of the key is critical to the communication.
the common algorithms for symmetric encryption are: des algorithm, 3DES algorithm, Tdea algorithm, Blowfish algorithm, RC5 algorithm , Idea algorithm.
There is also a symmetric cryptographic function in PHP that is well encapsulated
Urlencode/urldecode string urlencode (String $str)/* 1. A parameter that passes in the string to be encrypted (usually applied to the encryption of the URL) 2. UrlEncode is two-way encryption, Can be encrypted with urldecode (strictly speaking, not real encryption, more like a coding method) 3. Returns a string, in addition to-_, in this string. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+). */
Use the UrlEncode function to resolve the questions that are caused by the & character in the link:
Zhou [gang] = [Password] = Zhou) * ////as follows: $username = "Zhou&gang"; $url _decode= " Zhougang.com?username= ". UrlEncode ($username)." &password=zhou ";?> common UrlEncode () conversion characters ? = =%3f = = = =%3d% =%25 & =%26 \ = =%5c Base64 string Base64_decode (String $encoded _data) Base64_encode () accepts a parameter, that is, the data to be encoded (not to mention the string here, because many times base64 is used to encode the picture) Base64_encode () is two-way encryption, usable base64_decode () To decrypt the $data =file_get_contents ($filename); Echo Base64_encode ($data);/* and then you look at the Web page source will get a bunch of base64 strings, and then use base64_ Decode () restore to get the picture. This can also be used as one of the processing options for uploading pictures on the mobile side (but not recommended) */
Strictly speaking. These two functions are not really encrypted, more like a serialization of a format
Here are the symmetric encryption algorithms commonly used in our PHP program
discuz Classic Algorithm
0) && substr ($result, ten, +) = = substr (MD5 ($result, $keyb), 0, +) {return substr ($re Sult, 26); } else {return '; }} else {//To keep the dynamic key in the ciphertext, which is why the same plaintext, the production of different ciphertext can be decrypted after the reason//because the encrypted ciphertext may be some special characters, the copy process may be lost, so the base64 encoding Return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result)); } } Add decryption function Encrypt ()
$string: A string to encrypt and decrypt, $operation: To determine whether to encrypt or decrypt, E for encryption, and d for decryption; $key: Key
function Encrypt ($string, $operation, $key = ") {
$key =md5 ($key);
$key _length=strlen ($key);
$string = $operation = = ' D '? Base64_decode ($string): substr (MD5 ($string. $key), 0,8). $string;
$string _length=strlen ($string);
$rndkey = $box =array ();
$result = ";
for ($i =0; $i <=255; $i + +) {
$rndkey [$i]=ord ($key [$i% $key _length]);
$box [$i]= $i;
}
for ($j = $i =0; $i <256; $i + +) {
$j = ($j + $box [$i]+ $rndkey [$i])%256;
$tmp = $box [$i];
$box [$i]= $box [$j];
$box [$j]= $tmp;
}
for ($a = $j = $i =0; $i < $string _length; $i + +) {
$a = ($a + 1)%256;
$j = ($j + $box [$a])%256;
$tmp = $box [$a];
$box [$a]= $box [$j];
$box [$j]= $tmp;
$result. =CHR (Ord ($string [$i]) ^ ($box [($box [$a]+ $box [$j])%256]);
}
if ($operation = = ' D ') {
if (substr ($result, 0,8) ==substr (MD5 (substr ($result, 8). $key), 0,8)) {
Return substr ($result, 8);
}else{
Return ';
}
}else{
return str_replace (' = ', ' ', Base64_encode ($result));
}
}
?>