-
Notifications
You must be signed in to change notification settings - Fork 3
/
cracker.py
58 lines (39 loc) · 1.44 KB
/
cracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from prime import find_factor
from powmod import power_modulo
from euklidian import inverse_modulo
from pollard import pollard
def main():
n = int(input("Enter n (as part of the public key): "))
e = int(input("Enter e (as part of the public key): "))
c = int(input("Enter c (the encrypted message to decrypt): "))
b = int(input("Enter b (the maximum prime factor in n, optional): ") or 1)
# Demo:
# n = 186444745729857899758373984272541398503249351266417000699738642133172271283265124803102459
# e = 65537
# c = 159178142916077677757648147687519523540045276157456113470673097514775229976995968698190914
# b = 200000
if n < 2 or e < 2 or c < 0 or b > n or b <= 0:
print("Invalid input")
return
p = None
if b == 1:
print("Factorizing n with the Bruteforce algorithm...")
p = find_factor(n)
else:
print("Factorizing n with the Pollard algorithm...")
if b > n - 1:
b = n - 1
p = pollard(b, n)
print("p = %d" % p)
q = n // p
print("q = %d" % q)
print("Factorizing complete.")
phi = (p - 1) * (q - 1) if p != q else p * p - p
print("phi(n) = %d" % phi)
print("Solving e * d = 1 mod phi(n) with the euklidian algorithm...")
d = inverse_modulo(e, phi)
print("d (private key) = %d" % d)
m = power_modulo(c, d, n)
print("m (decrypted message) = %d" % m)
if __name__ == "__main__":
main()