Skip to content

Commit 4cff8de

Browse files
Updated RSA Algorithm with GeeksforGeeks explanation
1 parent 9164df9 commit 4cff8de

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

Scripts/RSA Algorithm ( Encryption - Decryption ).py

+72-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def rsa_algo(p: int,q: int, msg: str):
1212
e = find_e(z)
1313
d = find_d(e, z)
1414

15-
# Convert Plain Text -> Cyper Text
15+
# Convert Plain Text -> Cypher Text
1616
cypher_text = ''
1717
# C = (P ^ e) % n
1818
for ch in msg:
@@ -22,7 +22,7 @@ def rsa_algo(p: int,q: int, msg: str):
2222
# convert the calculated value to Characters(chr)
2323
cypher_text += chr((ch ** e) % n)
2424

25-
# Convert Plain Text -> Cyper Text
25+
# Convert Plain Text -> Cypher Text
2626
plain_text = ''
2727
# P = (C ^ d) % n
2828
for ch in cypher_text:
@@ -107,4 +107,74 @@ def gcd(x: int, y: int):
107107
4. Get the Encrypted/Cypher text(C) and Decrypted/Plain text(P) :
108108
# C = (P ^ e) % n
109109
# P = (C ^ d) % n
110+
'''
111+
112+
'''
113+
GeeksforGeeks :
114+
115+
RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e.
116+
Public Key and Private Key. As the name describes that the Public Key is given to everyone and Private key is kept
117+
private.
118+
119+
An example of asymmetric cryptography :
120+
121+
A client (for example browser) sends its public key to the server and requests for some data.
122+
The server encrypts the data using client’s public key and sends the encrypted data.
123+
Client receives this data and decrypts it.
124+
Since this is asymmetric, nobody else except browser can decrypt the data even if a third party has public key of
125+
browser.
126+
127+
The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer. The public key
128+
consists of two numbers where one number is multiplication of two large prime numbers. And private key is also derived
129+
from the same two prime numbers. So if somebody can factorize the large number, the private key is compromised.
130+
Therefore encryption strength totally lies on the key size and if we double or triple the key size, the strength of
131+
encryption increases exponentially. RSA keys can be typically 1024 or 2048 bits long, but experts believe that 1024 bit
132+
keys could be broken in the near future. But till now it seems to be an infeasible task.
133+
134+
Let us learn the mechanism behind RSA algorithm :
135+
136+
>> Generating Public Key :
137+
Select two prime no's. Suppose P = 53 and Q = 59.
138+
Now First part of the Public key : n = P*Q = 3127.
139+
140+
We also need a small exponent say e :
141+
But e Must be
142+
143+
An integer.
144+
145+
Not be a factor of n.
146+
147+
1 < e < Φ(n) [Φ(n) is discussed below],
148+
Let us now consider it to be equal to 3.
149+
150+
151+
Our Public Key is made of n and e
152+
>> Generating Private Key :
153+
154+
We need to calculate Φ(n) :
155+
Such that Φ(n) = (P-1)(Q-1)
156+
so, Φ(n) = 3016
157+
158+
159+
Now calculate Private Key, d :
160+
d = (k*Φ(n) + 1) / e for some integer k
161+
For k = 2, value of d is 2011.
162+
Now we are ready with our – Public Key ( n = 3127 and e = 3) and Private Key(d = 2011)
163+
164+
Now we will encrypt “HI” :
165+
166+
Convert letters to numbers : H = 8 and I = 9
167+
168+
169+
Thus Encrypted Data c = 89e mod n.
170+
Thus our Encrypted Data comes out to be 1394
171+
172+
173+
Now we will decrypt 1349 :
174+
175+
Decrypted Data = cd mod n.
176+
Thus our Encrypted Data comes out to be 89
177+
178+
8 = H and I = 9 i.e. "HI".
179+
110180
'''

0 commit comments

Comments
 (0)