RSA encryption algorithm overview process + pseudocode

rsa encryption algorithm

Introduction:

The RSA algorithm is based on a very simple number theory fact: it is very easy to multiply two large prime numbers, but it is extremely difficult to factorize the product, so the product can be made public as an encryption key.
The RSA algorithm is the most widely used public key cryptography algorithm today, and it is also known as the most secure encryption algorithm on earth. Before understanding the RSA algorithm, familiarize yourself with the following terms
. According to the use of the key, the password can be divided into symmetric encryption and public key encryption Symmetric
encryption : Encryption and decryption using the same key
Public key encryption : Encryption and decryption Different cryptographic methods are used, so public key cryptography is often also called asymmetric cryptography.

1. Overview of encryption algorithm

2. Diagram of algorithm principle

insert image description here

flow chart

insert image description here

Number theory knowledge involved in RSA

prime number:

Also known as a prime number, a positive integer greater than 1 is not divisible by other integers except 1 and itself.
Coprime relationship:
If two positive integers have no other common factors except 1, the two numbers are said to be coprime.

inference:

Any two prime numbers form a coprime relationship.
Two numbers that are prime numbers must have a coprime relationship.

Same surplus:

Given a positive integer m, if there are two integers a and b satisfying that ab can be divisible by m, that is (ab) mod m = 0, then the integers a and b are said to be congruent modulo m, written as: a≡b (mod m) , same as 3: a mod m = b

Euler function:

  • Definition : For a given positive integer n, calculate how many positive integers less than or equal to n form a coprime relationship with n. For example: φ(8) = 4 .

Inference :

  • If n can be split into the product of two relatively prime positive integers, such as n = p × q, then: φ(n) = φ(pq) = φ§φ(q)
  • For a prime number m, there is: φ(m) = m-1 .

  • If n can be decomposed into the product of two prime numbers p and q, then: φ(n) = (p-1)(q-1) .

  • Euler's theorem : If two positive integers a and n are mutually prime, then the Euler function φ(n) of n has:
    φ ( n ) = φ ( pq ) = φ § φ ( q ) φ(n) = φ( pq) = φ§φ(q)φ ( n )=φ ( pq )=φ § φ ( q )
    for prime number m , we have: φ(m) = m-1 .

If n can be decomposed into the product of two prime numbers p and q, then: φ(n) = (p-1)(q-1) .

Euler's theorem: If two positive integers a and n are mutually prime, then the Euler function φ(n) of n has:
a φ ( n ) ≡ 1 ( modn ) a φ (n)≡1(modn)and φ ( n )1 ( m o d n )

It means that the remainder of the φ(n) power of a divided by n is 1.

Reverse elements

If two positive integers a and n are relatively prime, then there must be an integer b such that: ab-1 can be divisible by n, or the remainder of ab divisible by n is 1, recorded as: ab≡1(mod n)

principle:

insert image description here

Encryption process:

  1. Select two different large prime numbers p and q, calculate their product n=pq, and find the Euler function φ(n)=(p-1)(q-1).

  2. Select an integer e smaller than φ(n) and coprime with φ(n) as the public key, that is, the public key is (n, e).

  3. Find the integer d that is congruent with e modulo φ(n), that is, d*e=1 mod φ(n), d is the private key, and the private key is (n,d).

  4. Convert the plaintext M to an integer m such that m<n.

  5. Encrypt the plaintext and calculate the ciphertext C=m^e mod n.

  6. Send ciphertext C.
    insert image description here

Decryption process:

insert image description here

pseudocode:

// 生成公钥和私钥
int p = 61;
int q = 53;
int n = p * q;
int phi = (p - 1) * (q - 1);
int e;
do {
    
    
e = random(2, phi - 1); // 生成一个与phi互质的整数e
} while (gcd(e, phi) != 1); // 判断e与phi是否互质
int d = modInverse(e, phi);

// 加密过程
int m = 123;
int c = modPow(m, e, n); // 计算密文

// 解密过程
int m2 = modPow(c, d, n); // 计算明文

// 辅助函数
int gcd(int a, int b) {
    
    
if (b == 0) {
    
    
return a;
} else {
    
    
return gcd(b, a % b);
}
}

int modInverse(int a, int m) {
    
    
int m0 = m;
int y = 0, x = 1;
if (m == 1) {
    
    
return 0;
}
while (a > 1) {
    
    
int q = a / m;
int t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) {
    
    
x += m0;
}
return x;
}

int modPow(int a, int b, int m) {
    
    
int res = 1;
while (b > 0) {
    
    
if ((b & 1) == 1) {
    
    
res = (res * a) % m;
}
a = (a * a) % m;
b >>= 1;
}
return res;
}

Guess you like

Origin blog.csdn.net/m0_68089732/article/details/130694677