-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrsa.py
46 lines (38 loc) · 1.1 KB
/
rsa.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
import sys
import Crypto.Util.number
from Crypto.Util.number import getPrime, GCD, inverse
def main(bits, message):
# key generation
while True:
# sample two different primes
p = getPrime(bits // 2)
q = getPrime(bits // 2)
if p == q:
continue
N = p * q
phi = (p - 1) * (q - 1)
e = 65537
# e needs to be invertible modulo phi(N)
if GCD(e, phi) > 1:
continue
d = inverse(e,phi)
print(f"Random Prime p = {p}")
print(f"Random Prime q = {q}")
print()
print(f"Modulus N = {N}")
print(f"Public exponent e = {e}")
print(f"Private exponent d = {d}")
break
# encryption
m = message % N
enc = pow(m, e, N)
dec = pow(enc, d, N)
print()
print(f"RSA ciphertext c = m ^ e mod N = {enc}")
print(f"RSA plaintext c ^ d mod N = {dec}")
assert dec == m
if __name__ == '__main__':
if (len(sys.argv) < 3):
print(f'usage: {sys.argv[0]} <bits> <message>', file=sys.stderr)
exit(1)
main(int(sys.argv[1]), int(sys.argv[2]))