Steps:
Randomly select two large prime numbers p and q,p not equal to Q, calculate N=PQ.
According to Euler function, r= (p-1) (q-1) is obtained.
Select an integer w that is less than R and Coprime, and obtain a modulo inverse of w about modulo R, named D. (DW mod n = 1).
The records of P and Q are destroyed.
Public key: N, W
Private key: N, D
Encryption: Ciphertext c = m^w mod n
Decryption: Clear Text m = c^d mod n
The algorithm used:
1. Coprime: coprime is defined as if the maximum common factor of two numbers for two numbers is 1
int isopprime (int w, int.)
{
if (on = = 0)
return w;
else
return isopprime (on, w);
}
When return 1 is coprime
2. Modulo inverse: known DW mod n = 1 can be used simply by brute: DW = nk + 1if NK + 1 mod w = = 0;d = nk +1 Div w
3. Power-Seeking mode: Seeking M^W mod n
by AB mod n = (a mod n) * (b mod n) mod n i.e. ab = (a mod n) (b mod n) (mod n);
AB mod n = (a-n) (b-n) mod n
Binary for w: Bn * B (n-1) *...*b1*b0 w = b0*2^0 + b1*2^1 +...+b (n-1) *2^ (n-1) + bn*2^n
M^w =m ^ (b0*2^0) * m ^ (b0*2^1) * ... * m ^ (b (n-1) *2^ (n-1)) * m ^ (bn*2^n (mod n))
Make A0 = m (mod n), Ai = (Ai-1) ^ 2 (mod n)
Then m^w = A0 * A1 *...* an (mod n)
A0 = m;
A1 = m^2 mod n;
A2 = a1^2 mod n;
...
When bi = 0, ai = 1 and Ai < N/2
Example:
p = Notoginseng, q = 43;
n = PQ = 1591;
R = (p-1) (q-1);
Take the integer w = 29 with R coprime, then the DW mod r = 1, and d = 365;
Set clear m = 12394;
Ciphertext c = m^w mod n = 1234^29 mod 1591;
w = 29 of Bits 11101
A0 = 1234 (> 1591/2 = 795) (mod 1591)
= 1234-1591 = -357 (mod 1591)
A1 = ( -357) ^2 = 169 (mod 1591)
A2 = 169^2 = 1514 (> 1591/2 = 795) (mod 1591)
= 1514-1591 = -77 (mod 1591)
A3 = ( -77) ^2 = 1156 = -435 (mod 1591)
A4 = ( -435) ^2 = 1487 = -104 (mod 1591)
c = ( -357) * ( -77) * ( -435) * ( -104) = 442* ( -435) * ( -104) = 241* ( -104) = 392 (mod 1591)
by C = 392, d = 365, also reversible push m = 1234, slightly