Digital Media Concepts/RSA (cryptosystem)

What is RSA
It is one of the first asymmetric cryptography algorithms which uses a pair of keys to encrypt and decrypt data. This algorithm is now widely used to transmit sensitive data over insecure network like the Internet.

History
Before mid-1970s, people used symmetric cryptography algorithms to encrypt data. With these algorithms, data encrypted with a particular cryptographic key could only be decrypted with the same key. Anyone without the key cannot decrypt the data. Therefore people could safely send sensitive information through insecure communication channel. However, people could not find a way to safely exchange their keys between them. Asymmetric cryptography algorithms were invented to solve this problem, and RSA is one of them. In April 1977, RSA was invented by three people from Massachusetts Institute of Technology, including two computer scientists, Ron Rivest, Adi Shamir, and a mathematician Leonard Adleman, and was later publicized in August of the same year.

RSA is now in public domain, and can be freely implemented and used by anyone.

== Steps ==

==== Generate the keys ==== As an asymmetric cryptography algorithm, RSA cryptosystem involves two keys, the public key and the private key. Data encrypted by one key can only be decrypted with the other.


 * 1) Randomly choose two prime numbers, $$p$$ and $$q$$.
 * 2) Multiply them together: $$n=p\cdot q$$
 * 3) Get the least common multiple of $$p-1$$ and $$q-1$$: $$\phi=lcm(p-1, q-1)$$
 * 4) Randomly choose a positive integer $$e$$ which is less than $$\phi$$ and coprime to $$\phi$$.
 * 5) Calculate modular multiplicative inverse $$d$$ of $$e$$ modulo $$\phi$$. Which means, find a positive integer $$d$$ which is less than $$\phi$$ that satisfies: $$e \cdot d \equiv 1 \pmod{\phi}$$
 * 6) Now the key pair is generated. $$(d, n)$$ is the private key, and $$(e, n)$$ is the public key

Sample Code
Here's a sample implementation of generating RSA keys written in Python 3.6



Send the public key to others
Let's say Alice and Bob want to have a secure communication. They may exchange their public keys without encryption. After that, a sender should always encrypt the data with the receiver's public key before sending it.

For example, Bob's data is encrypted with Bob's public key, and only people who know Alice's private key can decrypt the data. So Alice is the only person that meets this requirement. The same works with Bob.

Encryption and Decryption

 * 1) Let's say the original data is an positive integer $$m$$ which should be less than $$n$$. Encrypted data, positive integer $$c$$, can be generated with a public key: $$c=m^{e}\bmod{n}$$
 * 2) The encrypted data $$c$$ can be decrypted with the corresponding private key: $$m=c^{d}\bmod{n}$$

Proof of correctness
Because the encrypted data $$c$$ is calculated as:

$$c=m^{e}\bmod{n}$$

When decrypting the encrypted data $$c$$, the result is:

$$r=c^{d}\bmod{n}=(m^{e}\bmod{n})^{d}\bmod{n}=m^{e\cdot d}\bmod{n}$$

If $$m^{e\cdot d}\bmod{n}$$ equals to the original unencrypted data $$m$$, then this algorithm is correct.

To prove $$m^{e\cdot d}\bmod{n}=m$$, Chinese remainder theorem is needed.

It says when $$a$$ and $$b$$ are coprime ($$gcd(a,b)=1$$), if both of these statements are true:

$$x\equiv y\pmod{a}$$

$$x\equiv y\pmod{b}$$

Then this is also true:

$$x\equiv y\pmod{a\cdot b}$$

As $$p$$ and $$q$$ are both prime numbers, they are obviously coprime. So if both of these statements could be proved to be true:

$$m^{e\cdot d}\equiv m\pmod{p}$$

$$m^{e\cdot d}\equiv m\pmod{q}$$

Then this is also true:

$$m^{e\cdot d}\equiv m\pmod{n}$$

Then the correctness of RSA algorithm could be proved:

$$r=m^{e\cdot d}\bmod{n}=m\bmod{n}=m$$

Because $$p$$ and $$q$$ are identical, only one of $$m^{e\cdot d}\equiv m\pmod{p}$$ and $$m^{e\cdot d}\equiv m\pmod{q}$$ need to be proved. The same works with the other one.

Prove the correctness of $$m^{e\cdot d}\equiv m\pmod{p}$$:

To accomplish this, Fermat's little theorem is necessary.

It says, if $$y$$ is a prime, and $$x$$ is not a multiple of $$y$$ ($$y\nmid x$$), then this statement is true:

$$x^{y-1}\equiv 1\pmod{y}$$

As the relationship between $$m$$ and $$p$$ is unknown, the problem need to be divided into two cases.

Case 1: $$m$$ and $$p$$ are not coprime, which means $$m$$ is a multiple of $$p$$ ($$p\mid m$$). Obviously,

$$m^{e\cdot d}\equiv 0\equiv m\pmod{p}$$

Case 2: $$m$$ and $$p$$ are coprime. In this case, Fermat's little theorem could be used. So $$m^{p-1}\equiv 1\pmod{p}$$

Because:

$$e\cdot d\equiv 1\pmod{\phi}$$

As $$\phi$$ is a multiple of both $$p-1$$ and $$q-1$$, both of these statements are true:

$$e\cdot d\equiv 1\pmod{p-1}$$

$$e\cdot d\equiv 1\pmod{q-1}$$

So $$e\cdot d$$ can be expressed as:

$$e\cdot d=1+k\cdot(p-1)$$

Therefore:

$$m^{e\cdot d}\equiv m^{1+k\cdot(p-1)}\equiv m\cdot(m^{p-1})^{k}\equiv m\cdot 1^{k}\equiv m\pmod{p}$$

Q.E.D.

== Safety ==

The public key, which is $$e$$ and $$n$$, can be known to everyone. As far as $$d$$ is kept private, no one except the private key owner is able to decrypt the data encrypted by the public key. However, number $$n$$ has two prime factors $$p$$ and $$q$$. If one can find $$p$$ and $$q$$ by factoring $$n$$, then this person can also find out $$\phi=lcm(p-1, q-1)$$, and eventually $$d$$. So the problem of how safe RSA is, is equivalent to how hard it is to factor $$n$$.

It is proved that, currently with traditional (non-quantum) computer, factoring a big number is an NP problem. It may or may not be an NP-complete problem. RSA remains safe when integer factorization can't be solved in polynomial time.