Easily learn the principles of RSA encryption algorithms

Source: Internet
Author: User
Tags decrypt

Previously also contacted RSA encryption algorithm, feel this thing is too mysterious, is a mathematician thing, and I have nothing to do. However, after looking at a lot of information on the principles of RSA encryption algorithm, I found that the principle is not as complex as we imagined, and then found that the original is just this.
Friends who have learned the algorithm know that the algorithm in the computer is actually a mathematical operation. Therefore, before explaining the RSA encryption algorithm, it is necessary to understand some essential mathematical knowledge. Let's start with the math knowledge.

Essential Mathematical Knowledge
In RSA encryption algorithm, only a few simple mathematical knowledge such as prime number, Inma, exponential arithmetic and modulo operation are used. So, we also need to understand these concepts.

Prime

The prime number is also called Prime, which is the number of natural numbers greater than 1 that cannot be divisible by other natural numbers except 1 and the integer itself. This concept, we have learned in junior high school, even elementary school, here will not explain too much.

Inma

Baidu Encyclopedia on the explanation is: the public factor only 1 of two number, called Inma. The Wikipedia explanation is: coprime, also known as mutual-vegetarian. If the maximum common factor for n integers is 1, it is called the n integer coprime.

The common Inma judgment methods are as follows:

Two different prime numbers must be Inma. For example, 2 and 7, 13, and 19.
One prime number, the other is not a multiple of it, and these two numbers are Inma. For example, 3 and 10, 5, and 26.
The adjacent two natural numbers are Inma. such as 15 and 16.
The adjacent two odd numbers are Inma. such as 49 and 51.
The larger number is the two number of prime numbers that are Inma. such as 97 and 88.
Decimals are prime numbers, the large number is not a multiple of the decimal number of two numbers is Inma. For example 7 and 16.
2 and any odd number is Inma. For example 2 and 87.
1 is not a prime or a composite, it is Inma with any natural number. such as 1 and 9908.
The method of dividing.
Exponential arithmetic

Exponential operation is also called power calculation, the result of calculation is called exponentiation. NM refers to n squared m times. Think of NM as the result of a power, called "N's M powers" or "N's M-squares". where n is called "base" and M is called "exponent".

Modulo operations

Modulo operation is to find the remainder operation. "Modulo" is a transliteration of "Mod". A concept closely related to modulo operations is "congruence". Mathematically, when two integers are divided by the same positive integer, the remainder is equal to two integers.

Two integers a, b, if they divide by the positive integer m to get the remainder equal, then a, b for the modulo m congruence, recorded as: A≡b (mod m), read: A with the remainder of the model M, or, A and b about modulo m congruence. Example: 26≡14 (mod 12).

RSA encryption algorithm
A brief history of RSA encryption algorithms

RSA was introduced in 1977 by Ronald Leevist (Ron rivest), Adi Samor (Adi Shamir) and Lennard Adman (Leonard Adleman). At the time, all three of them worked at MIT. RSA is the first letter of their three surnames made together.

The creation of public keys and keys

Suppose Alice wants to receive a private message from Bob through an unreliable medium. She can generate a public key and a private key in the following ways:

Randomly select two large prime numbers p and q,p not equal to Q, calculate N=PQ.
Based on Euler function, r = (p-1) (q-1) is obtained
Select an integer e that is less than R, and find the e-modulo inverse element of module R, named D. (modulo inverse element exists, when and only if E with R coprime)
The records of P and Q are destroyed.
(n,e) is the public key, (N,D) is the private key. Alice passes her public key (N,e) to Bob and hides her private key (N,d).

Encrypt messages

Suppose Bob wanted to send Alice a message m, and he knew that Alice produced N and E. He used the first date with Alice to convert m to an integer n less than n, for example, he could convert each word to the Unicode code of the word, and then connect the numbers together to form a number. If his message is very long, he can divide the message into several paragraphs and then convert each section to n. Using the following formula, he can encrypt N to c:

Ne≡c (mod N)

Computing c is not complicated. Bob can pass it to Alice after he has calculated C.

Decrypting messages

After Alice gets Bob's message C, she can use her key D to decode it. She can use the following formula to convert C to n:

Cd≡n (mod n)

After getting N, she can restore the original message m.

The decoding principle is:

Cd≡n e D (mod n)

As well as ed≡1 (mod p-1) and ed≡1 (mod q-1). Proved by Fermat theorem (since p and q are prime numbers)

n e d≡n (mod p) and n e d≡n (mod q)

This indicates (because p and q are different prime numbers, so p and Q coprime)

n e d≡n (mod PQ)

Signing messages

RSA can also be used to give a message a signature. If a wants to send a signed message to B, then she can calculate a hash value for her message (Message digest), then use her key (private key) to encrypt the hash value and add the "signature" to the message. This message can only be decrypted with her public key. After obtaining this message, you can decrypt the hash value with the public key of a, and then compare the data to the hash value that he has computed for the message. If the two match, then he can know the sender holds a key, and this message has not been tampered with in the transmission path.

Programming practices

Now, let's start with our focus: programming practices. Before we start programming, we use calculations to determine the public key and the key.

Calculating public keys and keys
Suppose P = 3, q = One (P,q are primes.) ), then n = PQ = 33;
R = (p-1) (q-1) = (3-1) (11-1) = 20;
Based on the formula of the modulo inverse element, we can conclude that e d≡1 (mod 20), i.e. e d = 20n+1 (n is a positive integer); we assume N=1, then e d = 21. E, D is a positive integer, and E is coprime with R, then E = 3,d = 7. (two numbers can also be exchanged for a bit.) )
Here, the public key and key have been determined. The public key is (n, e) = (33, 3), and the key is (n, D) = (33, 7).

Programming implementation
Here we use Java to implement the encryption and decryption process. The specific code is as follows:

RSA algorithm implementation:

[Java] View plaincopy
<span style= "FONT-SIZE:14PX;" >package Security.rsa;

public class RSA {

/**
* Encryption, decryption algorithm
* @param key Public key or key
* @param message data
* @return
*/
public static long RSA (int basenum, int key, long message) {
if (Basenum < 1 | | Key < 1) {
return 0L;
}
Data after encryption or decryption
Long rsamessage = 0L;

Cryptographic core algorithms
Rsamessage = Math.Round (MATH.POW (message, key))% Basenum;
return rsamessage;
}



public static void Main (string[] args) {
Base
int basenum = 3 * 11;
Public
int keye = 3;
Secret key
int keyd = 7;
Data that is not encrypted
Long msg = 24L;
Data after the encryption
Long encodemsg = RSA (Basenum, Keye, msg);
Decrypted data
Long decodemsg = RSA (Basenum, Keyd, encodemsg);

System.out.println ("Before encryption:" + msg);
SYSTEM.OUT.PRINTLN ("After encryption:" + encodemsg);
System.out.println ("After decryption:" + decodemsg);

}
</span>

}
RSA algorithm results:
Before encryption: 24
After encryption: 30
After decryption: 24
(See the program most clearly, for the number m to encrypt, M^e%n=c, C is encrypted after the ciphertext.) C^d%n=m, you can decrypt it. m)
Security of RSA encryption algorithm

When P and Q are a large prime number, the decomposition factor p and Q from their product PQ is a recognized mathematical problem. However, although RSA's security relies on the factorization of large numbers, it does not theoretically prove that the difficulty of deciphering RSA is equivalent to the difficulty of large number decomposition.

1994 Peter Sul (Peter Shor) proves that a quantum computer can decompose factoring in polynomial time. If a quantum computer can someday become a viable technology, then the algorithm can eliminate RSA and related derivative algorithms. (i.e., a cryptographic algorithm that relies on the decomposition of large integer difficulties)

In addition, if the length of n is less than or equal to 256 bits, then a personal computer can decompose its factor within a few hours. In 1999, hundreds of computers collaborated to decompose a 512-bit long n. 1997 years after the development of the system, the user should use a 1024-bit key, certification authority application 2048 or more.

Disadvantages of RSA encryption algorithm
Although RSA encryption algorithm, as one of the best public key schemes, has experienced various attacks and gradually accepted them for more than 30 years of publication. However, it is not that RSA has no shortcomings. There is no theoretical proof of the equivalence between the difficulty of deciphering RSA and the difficulty of large number decomposition. So, the major flaw of RSA is that it can't theoretically grasp how its secrecy performance is. In practice, RSA has some drawbacks:

It is troublesome to generate the key, which is limited by the technology of the prime number, so it is difficult to do it once.
The packet length is too large, in order to ensure security, n at least more than bits, so that the cost of the operation is very high, especially the slow speed.

Easily learn the principles of RSA encryption algorithms

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.