The last article described the principle of symmetrical encryption, but its biggest problem was that the key to encryption and decryption was the same, and there was no guarantee that the key would be delivered safely to both sides, even if it was delivered safely to both sides, so that there would be an internal undercover presence
Asymmetric encryption
Since there's a symmetric encryption, it's natural to think of asymmetric encryption. The core of asymmetric encryption is that encryption and decryption use different keys, and how do we use different keys
For example, i have a deposit box that can only be opened with a key, and you'll normally have to put the change in it, but i'm the only one who has the key. The coins placed in the storage tanks can be seen as encrypted content and only the key can be used to remove the encrypted coins。
So we can give anyone the key to encryption, and we can secure our data by keeping the key to decryption。
There are many asymmetric algorithms: rsa, elgamal, backpack algorithms, rabin, d-h, ecc, and so on。
Rsa algorithm
Rsa public key encryption algorithm: 1977 by ronald river, adi shamir and leonard leoNard adleman). It was first published in 1987, when all three of them were working at mit. Rsa is a combination of their initials。
Rsa is currently the most influential public key encryption algorithm, able to resist the vast majority of password attacks known to date and has been recommended by iso as a public key data encryption standard。
Rsa encryption & rsa decryption formula
/note: the actual calculation process can be converted through ascii code, which is explicit. Secret = (specify e) %n; /e and n are our public keys
express = (d ^ n; / where d and n are our private keys
Calculate & public keys (e), private keys (d) and numbers

Public and private keys are not just a few numbers. They are calculated by strict mathematical formulas。
1, random preparation of two prime numbers p and q to calculate the product to n
N = p *q;
2. Calculating l
L = (p-1) * (q-1) / // fig password technology says that the minimum common multiple after the product has been calculated, but the code is not accurate, so who knows the trouble message
3. Calculate e (public key) for encryption
/ e needs to meet both
1. 1
4. Calculating d (private key) for decryption
/d needs to meet the formula below
(e*d) %l = 1; // want to make sure the result is one, e and l have to be reciprocal!
This is the whole calculation, and p and q will select a very large number (1024 bits or larger) for the security reality of the data
Rsa encryption and decryption
The methods of encryption and decryption have already been mentioned, and let's use specific numerical practices to deepen understanding。
1 n (p*q)
Assumptions: p = 7, q = 11 (both prime)
so: n = p * q = 7 * 11 = 77
L((p-1)* (q-1))
L = (p-1) * (q-1) = 6 * 10 = 60
3. Request e
One
4. Calculate d(((e*d) %l = 1)
(23 *d) %60 = 1;
d = 47;

So i got the public key
Encrypt & decrypt
We assume the numbers need to be encrypted:12
Formula: secret = (explicit e) % n;
12 ^ 23 % 77 = 66244737694923711208 % 77 = 45;
This 45 is our encrypted secret
Decrypt
Formula: express = (secret ^d) %n;
^47 %77 =
5023280800139658 1962666445943507066767676767442752246852525252525252552001953125 % 77 = 12;
Original language: 12
Example of php

Here's an example of encryption and decryption that i'm using for your information
Funcing is primenummer
$k = 0;/defined number of variables
for ($i = 1; $i 1; $i--)
other organiser
if (bcmod ($a, $i) = 0 & bcmod ($b, $i) = 0)
other organiser
return $i; / / if echo $i is used here; output result is 432; therefore a distinction should be made between echo and return
♪ i'm sorry ♪
♪ i'm sorry ♪
return 1;
♪ i'm sorry ♪
do it!
// random 1 prime p
$p = mt rand (101, 197);
{\chffffff}{\ch00ffff} while you're here? Do it!
// randomly a primeq
$q = mt rand (101, 197);
{\chffffff}{\ch00ffff} while you're here?
$n = bcmul ($p, $q);
/ $l =minmultiple ($p - 1, $q - 1); // not available after testing
$l = bcmul ($p-1, $q-1);
do {
$e = mt rand (2, $l-1);
} } } } } } } }
$d = 1;
while (bcmod($e,+$d), $l) = 1) {
♪ i'm sorry ♪
i don't know, echo'p:'. $p. Php eol;
i don't know, echo'q:'. $q. Php eol;
i don't know, echo'n:'. $n. Php eol;
i don't know, echo'l:'. $l. Php eol;
i don't know, echo'e:' ...$e . Php eol;
i don't know, echo'd:'. $d. Php eol;
echo "public key: e=$e}, n=$n}". Php eol;
echo "personal key: d=$d}, n=$n}". Php eol;
/ encrypt
function encode ($e, $n, $string)
$enstring=♪ i'm sorry ♪';
$len =strlen ($string);
for ($i = 0; $i 'l)', $mod;
♪ i'm sorry ♪
i'm sorry.
♪ i'm sorry ♪
// decrypt
funct decode ($d, $n, $string){
$destring=♪ i'm sorry ♪';
$string = unpack'l*', $string;
$len = count ($string);
for ($i = 1; $i 'welcome attention 'for pretending to be a programmer' public sign';
echo "original:".
$encodestring = encode ($e, $n, $string);
echo "steering.
$decodestring = decode ($d, $n, $encodestring);
echo "after decryption:". $codestring. Php eol;
$endtime = microtime;
"total:.".'s.'. Php eol;
There is no absolutely safe encryption
There's no encryption that can keep absolutely safe, especially the commonly used md5, if you still have a password in your database or if you use md5 hash not to laugh at someone who keeps it directly。
Google has recently announced the decryption of sha-1, and as computing capacity increases, sha-256, rsa, etc。










