https://en.wikipedia.org/wiki/RSA_(cryptosystem)
RSA is one of the first practical实用性的 public-key cryptosystems and is widely used for secure data transmission.
In such a cryptosystem密码系统, the encryption key is public and differs from the decryption key which is kept secret. 加密的key是公开的,解密的key是保密的
In RSA, this asymmetry不对称 is based on the practical difficulty of factoring因素 the product of two large prime numbers, the factoring problem.
RSA is made of the initial letters of the surnames of Ron Rivest, Adi Shamir, and Leonard Adleman, who first publicly described the algorithm in 1977.
Clifford Cocks, an English mathematician数学家 working for the UK intelligence agency情报机构 GCHQ, had developed an equivalent system in 1973, but it was not declassified撤销文件的机密等级 until 1997.[1]
A user of RSA creates and then publishes a public key based on two large prime numbers, along with an auxiliary辅助的 value.
The prime numbers must be kept secret.
Anyone can use the public key to encrypt a message, but with currently published methods,
if the public key is large enough, only someone with knowledge of the prime numbers can feasibly切实 decode the message.[2]
Breaking RSAencryption is known as the RSA problem; whether it is as hard as the factoring problem remains an open question.
RSA is a relatively slow algorithm, and because of this it is less commonly used to directly encrypt user data.
More often通常, RSA passes encrypted shared keys for symmetric对称的 key cryptography密码学;密码使用法 which in turn can perform bulk encryption-decryption operations at much higher speed.
The RSA algorithm involves four steps: key generation, key distribution, encryption and decryption.
RSA involves a public key and a private key. The public key can be known by everyone and is used for encrypting messages.
The intention is that messages encrypted with the public key can only be decrypted in a reasonable amount of time using the private key.
The basic principle behind RSA is the observation that it is practical to find three very large positive integers e,d and n such that with modular exponentiation for all m:
and that even knowing e and n or even m it can be extremely difficult to find d.
Additionally, for some operations it is convenient that the order of the two exponentiations can be changed and that this relation also implies:
To enable Bob to send his encrypted messages, Alice transmits her public key (n, e) to Bob via a reliable, but not necessarily secret route, and keeps the private key d secret and this is never revealed to anyone. Once distributed the keys can be reused over and over.
Bob then wishes to send message M to Alice.
He first turns M into an integer m, such that 0 ≤ m < n and gcd(m, n) = 1 by using an agreed-upon reversible protocol known as a padding scheme. He then computes the ciphertext c, using the public key e of Alice, corresponding to
This can be done efficiently, even for 500-bit numbers, using modular exponentiation. Bob then transmits c to Alice.
Alice can recover m from c by using her private key exponent d by computing
Given m, she can recover the original message M by reversing the padding scheme.
The keys for the RSA algorithm are generated the following way:
The public key consists of the modulus n and the public (or encryption) exponent e. The private key consists of the modulus n and the private (or decryption) exponent d, which must be kept secret. p, q, and φ(n) must also be kept secret because they can be used to calculate d.
Since any common factors of (p-1) and (q-1) are present in the factorisation of p*q-1,[14] it is recommended that (p-1) and (q-1) have only very small common factors, if any besides the necessary 2.[15][2][16]
Here is an example of RSA encryption and decryption. The parameters used here are artificially small, but one can also use OpenSSL to generate and examine a real keypair.
The public key is (n = 3233, e = 17). For a padded plaintext没有加密过的文件 message m, the encryption function is
The private key is (d = 2753). For an encrypted ciphertext密文 c, the decryption function is
For instance, in order to encrypt m = 65, we calculate
To decrypt c = 2790, we calculate
Both of these calculations can be computed efficiently using the square-and-multiply algorithm for modular exponentiation. In real-life situations the primes selected would be much larger; in our example it would be trivial to factorn, 3233 (obtained from the freely available public key) back to the primes p and q. Given e, also from the public key, we could then compute d and so acquire the private key.
Practical implementations use the Chinese remainder theorem to speed up the calculation using modulus of factors (mod pq using mod p and mod q).
The values dp, dq and qinv, which are part of the private key are computed as follows:
Here is how dp, dq and qinv are used for efficient decryption. (Encryption is efficient by choice of a suitable d and e pair)