# RSA Algorithm in Cryptography

 `# Write Python3 code here ` `from` `decimal ``import` `Decimal ` ` `  `def` `gcd(a,b): ` `    ``if` `b``=``=``0``: ` `        ``return` `a ` `    ``else``: ` `        ``return` `gcd(b,a``%``b) ` `p ``=` `int``(``input``(``'Enter the value of p = '``)) ` `q ``=` `int``(``input``(``'Enter the value of q = '``)) ` `no ``=` `int``(``input``(``'Enter the value of text = '``)) ` `n ``=` `p``*``q ` `t ``=` `(p``-``1``)``*``(q``-``1``) ` ` `  `for` `e ``in` `range``(``2``,t): ` `    ``if` `gcd(e,t)``=``=` `1``: ` `        ``break` ` `  ` `  `for` `i ``in` `range``(``1``,``10``): ` `    ``x ``=` `1` `+` `i``*``t ` `    ``if` `x ``%` `e ``=``=` `0``: ` `        ``d ``=` `int``(x``/``e) ` `        ``break` `ctt ``=` `Decimal(``0``) ` `ctt ``=``pow``(no,e) ` `ct ``=` `ctt ``%` `n ` ` `  `dtt ``=` `Decimal(``0``) ` `dtt ``=` `pow``(ct,d) ` `dt ``=` `dtt ``%` `n ` ` `  `print``(``'n = '``+``str``(n)``+``' e = '``+``str``(e)``+``' t = '``+``str``(t)``+``' d = '``+``str``(d)``+``' cipher text = '``+``str``(ct)``+``' decrypted text = '``+``str``(dt)) `

RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e. Public Key and Private Key. As the name describes that the Public Key is given to everyone and Private key is kept private.

An example of asymmetric cryptography :

1. A client (for example browser) sends its public key to the server and requests for some data.
2. The server encrypts the data using client’s public key and sends the encrypted data.
3. Client receives this data and decrypts it.

Since this is asymmetric, nobody else except browser can decrypt the data even if a third party has public key of browser.

The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer. The public key consists of two numbers where one number is multiplication of two large prime numbers. And private key is also derived from the same two prime numbers. So if somebody can factorize the large number, the private key is compromised. Therefore encryption strength totally lies on the key size and if we double or triple the key size, the strength of encryption increases exponentially. RSA keys can be typically 1024 or 2048 bits long, but experts believe that 1024 bit keys could be broken in the near future. But till now it seems to be an infeasible task.

Let us learn the mechanism behind RSA algorithm :

>> Generating Public Key :

```Select two prime no's. Suppose P = 53 and Q = 59.
Now First part of the Public key  : n = P*Q = 3127.
We also need a small exponent say e :
But e Must be
An integer.
Not be a factor of n.
1 < e < Φ(n) [Φ(n) is discussed below],
Let us now consider it to be equal to 3.
Our Public Key is made of n and e```

>> Generating Private Key :

```We need to calculate Φ(n) :
Such that Φ(n) = (P-1)(Q-1)
so,  Φ(n) = 3016
Now calculate Private Key, d :
d = (k*Φ(n) + 1) / e for some integer k
For k = 2, value of d is 2011.```

Now we are ready with our – Public Key ( n = 3127 and e = 3) and Private Key(d = 2011)

Now we will encrypt “HI” :

```Convert letters to numbers : H  = 8 and I = 9
Thus Encrypted Data c = 89e mod n.
Thus our Encrypted Data comes out to be 1394

Now we will decrypt 1394 :
Decrypted Data = cd mod n.
Thus our Encrypted Data comes out to be 89
8 = H and I = 9 i.e. "HI".```

Below is C implementation of RSA algorithm for small values:

 `// C program for RSA asymmetric cryptographic ` `// algorithm. For demonstration values are ` `// relatively small compared to practical ` `// application ` `#include ` `#include ` ` `  `// Returns gcd of a and b ` `int` `gcd(``int` `a, ``int` `h) ` `{ ` `    ``int` `temp; ` `    ``while` `(1) ` `    ``{ ` `        ``temp = a%h; ` `        ``if` `(temp == 0) ` `          ``return` `h; ` `        ``a = h; ` `        ``h = temp; ` `    ``} ` `} ` ` `  `// Code to demonstrate RSA algorithm ` `int` `main() ` `{ ` `    ``// Two random prime numbers ` `    ``double` `p = 3; ` `    ``double` `q = 7; ` ` `  `    ``// First part of public key: ` `    ``double` `n = p*q; ` ` `  `    ``// Finding other part of public key. ` `    ``// e stands for encrypt ` `    ``double` `e = 2; ` `    ``double` `phi = (p-1)*(q-1); ` `    ``while` `(e < phi) ` `    ``{ ` `        ``// e must be co-prime to phi and ` `        ``// smaller than phi. ` `        ``if` `(gcd(e, phi)==1) ` `            ``break``; ` `        ``else` `            ``e++; ` `    ``} ` ` `  `    ``// Private key (d stands for decrypt) ` `    ``// choosing d such that it satisfies ` `    ``// d*e = 1 + k * totient ` `    ``int` `k = 2;  ``// A constant value ` `    ``double` `d = (1 + (k*phi))/e; ` ` `  `    ``// Message to be encrypted ` `    ``double` `msg = 20; ` ` `  `    ``printf``(``"Message data = %lf"``, msg); ` ` `  `    ``// Encryption c = (msg ^ e) % n ` `    ``double` `c = ``pow``(msg, e); ` `    ``c = ``fmod``(c, n); ` `    ``printf``(``"\nEncrypted data = %lf"``, c); ` ` `  `    ``// Decryption m = (c ^ d) % n ` `    ``double` `m = ``pow``(c, d); ` `    ``m = ``fmod``(m, n); ` `    ``printf``(``"\nOriginal Message Sent = %lf"``, m); ` ` `  `    ``return` `0; ` `} ` `// This code is contributed by Akash Sharan. `

Output :

```Message data = 12.000000
Encrypted data = 3.000000
Original Message Sent = 12.000000
```

This article is contributed by Mohit Gupta_OMG 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.