Euclid algorithm and RSA

Source: Internet
Author: User

The first algorithm in history seems to be the Euclidean algorithm. In fact, it is what everyone on Earth knows. Don't underestimate her. She is very beautiful.

A simple description is that gcd (a, B) indicates the maximum public factor of non-negative integers A and B, so: gcd (a, B) = gcd (B, A % B) or gcd (A, 0) = gcd (0, a) =.

Writing a program is simple, whether using recursion or loop:

Int gcd (int A, int B)
{
If (A = 0)
Return B;
If (B = 0)
Return;
Return gcd (B, A % B );
}

There are two numbers num1 and num2. Suppose num1 is relatively large. Returns the remainder r = num1 % num2.
When r = 0, num1 can be divisible by num2. Obviously, num2 is the maximum common divisor of these two numbers.
When r! When the value is 0, set num1 to num2 (divisor to divisor), num2 to R (Remainder to divisor), and then R to num1 % num2. Recursion until r = 0.
The above mathematical principles can be analyzed with two specific numbers, which is easy to understand.

Code Implementation (calculate the maximum public approx ):

Not only is the algorithm simple, but it is highly efficient. I don't know how complicated it is, but I only know that it is highly efficient ;)

The day before yesterday, I looked at the RSA algorithm, which is a standard non-symmetric encryption algorithm. In fact, the algorithm is very simple:
Find two prime numbers P, Q, and then find another number R, so that gcd (R, P-1) (q-1) = 1, that is, reciprocal, and then find another number M, make Rm = 1 (mod (p-1) (q-1), then multiply n = PQ, and then drop PQ, preferably so that no one knows, include yourself (to avoid being heard when talking), and then get R, M, N, R is private key, only you know, and m, n is public key. Set the information to a. the encryption process is a ^ r = B (mod N), B is the ciphertext, And the decryption process is B ^ m = a (mod n). In turn, M is used for encryption, it is the same to decrypt data using R.

The book says that by gcd (R, (p-1) (q-1) = 1 to evaluate M, making Rm = 1 (mod (PM) (q-1) very easy, it took me a long time to think of a method.

Problem: If gcd (a, B) = 1, evaluate X to make AX = 1 (mod B)
From gcd (a, B) = 1, we can see that x exists, because the prefix is equivalent to the existence of such X, and y causes AX + by = 1, take by as AX =-Yb + 1, that is, Ax = 1 (mod B)

I made R0 = A, R1 = B, and started to split
R0 = q2r1 + R2
R1 = q3r2 + r3
......
R (S-1) = Q (S + 1) R (s) + R (S + 1), R (S + 1) = 1 (there must be an R (S + 1) = 1)

Write the remainder aside:
R0 =
R1 = B
R2 = r0-q2r1
R3 = r1-q3r2
......
1 = R (S + 1) = R (S-1)-Q (S + 1) R (s)

The formula below is about the polynomial of the previous formula, and the first is A and B. The last formula proves that there must be 1 = AX + by. They are all about, how to calculate the X of a polynomial of B? Take the previous formula child to the back, one by one, but you will find that it is very complicated and not easy to find, so I think of the same method for iteration.

Let's get R (n) = x (n) A + Y (n) B from the replacement of the previous formula.
R (n + 1) = R (n-1)-Q (n + 1) R (N)
= X (n-1) A + Y (n-1) B-Q (n + 1) (x (n) A + Y (n) B)
= (X (n-1)-Q (n + 1) x (n) A + (...) B

So we get the iteration formula of x (n): x (n) = x (n-2)-Q (n) x (n-1), and the initial value X0 = 1, X1 = 0, q (n) = [R (n-2)/R (n-1)], so x (n) is deterministic. A small problem is that the obtained X may be a negative number. It is very simple. In mod B, you only need to add B.

Code:
# Include <assert. h>
# Include <iostream. h>

Int EUC (INT R1, int R2, int X1, int x2)
{
If (R2 = 1)
Return X2;
If (R2 = 0)
Return 0;
Return EUC (R2, R1 % R2, X2, x1-r1/R2 * x2 );
}

Int Euclid (int A, int B)
{
Assert (a> 0 & B> 0 );
Int x = EUC (B, A % B, 0, 1 );
If (x <0)
X + = B;
Return X;
}

Int main (void)
{
Int A, B, X;
Cin> A> B;
X = Euclid (A, B );
If (x = 0)
Cout <"gcd (a, B )! = 1 "<Endl;
Else
Cout <"x =" <x <Endl;
Return 0;
}

The performance of the algorithm is the same as that of the Euclid algorithm, but it is far from RSA. The security of RSA is based on the decomposition of the large prime number of N = PQ. The teacher usually chooses several hundred bits. Therefore, all of the above needs to be rewritten and a large number computing database is required to support four arithmetic operations. This is nothing. The Euclid algorithm will soon converge, and the key is the operation during encryption/decryption, because of the large computing capacity, RSA is generally used to encrypt very small data, such as DES keys.

On the other hand, I think it is difficult to select p and q among the large numbers and find R. I don't know what algorithm to use. If it is difficult to calculate, I can make a large prime number table, each time you select a few items from the table, the table security is not low.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.