Skip to content

Commit a2942d2

Browse files
committed
Adding the common modulus attack script
1 parent a8c7ed5 commit a2942d2

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Diff for: RSA Attacks/RSA: Common modulus attack.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/python3.4
2+
# Written by Anirudh Anand (lucif3r) : email - [email protected]
3+
# This program will help to decrypt cipher text to plain text if you have
4+
# more than 1 cipher text encrypted with same Modulus (N) but different
5+
# exponents. We use extended Euclideangm Algorithm to achieve this.
6+
7+
__author__ = 'lucif3r'
8+
9+
import gmpy2
10+
11+
12+
class RSAModuli:
13+
def __init__(self):
14+
self.a = 0
15+
self.b = 0
16+
self.m = 0
17+
self.i = 0
18+
def gcd(self, num1, num2):
19+
"""
20+
This function os used to find the GCD of 2 numbers.
21+
:param num1:
22+
:param num2:
23+
:return:
24+
"""
25+
if num1 < num2:
26+
num1, num2 = num2, num1
27+
while num2 != 0:
28+
num1, num2 = num2, num1 % num2
29+
return num1
30+
def extended_euclidean(self, e1, e2):
31+
"""
32+
The value a is the modular multiplicative inverse of e1 and e2.
33+
b is calculated from the eqn: (e1*a) + (e2*b) = gcd(e1, e2)
34+
:param e1: exponent 1
35+
:param e2: exponent 2
36+
"""
37+
self.a = gmpy2.invert(e1, e2)
38+
self.b = (float(self.gcd(e1, e2)-(self.a*e1)))/float(e2)
39+
def modular_inverse(self, c1, c2, N):
40+
"""
41+
i is the modular multiplicative inverse of c2 and N.
42+
i^-b is equal to c2^b. So if the value of b is -ve, we
43+
have to find out i and then do i^-b.
44+
Final plain text is given by m = (c1^a) * (i^-b) %N
45+
:param c1: cipher text 1
46+
:param c2: cipher text 2
47+
:param N: Modulus
48+
"""
49+
i = gmpy2.invert(c2, N)
50+
mx = pow(c1, self.a, N)
51+
my = pow(i, int(-self.b), N)
52+
self.m= mx * my % N
53+
def print_value(self):
54+
print("Plain Text: ", self.m)
55+
56+
57+
def main():
58+
c = RSAModuli()
59+
N =
60+
c1 =
61+
c2 =
62+
e1 =
63+
e2 =
64+
c.extended_euclidean(e1, e2)
65+
c.modular_inverse(c1, c2, N)
66+
c.print_value()
67+
68+
if __name__ == '__main__':
69+
main()

0 commit comments

Comments
 (0)