forked from Bl4omArchie/Pubcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (50 loc) · 1.93 KB
/
app.py
File metadata and controls
66 lines (50 loc) · 1.93 KB
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
59
60
61
62
63
64
65
66
from pubcrypt.cryptosystem.rsa import *
import argparse
def generate_keypair(nBits, e=65537):
n, e, d = generate(nBits, e)
print (f"\nn = {n}\ne = {e}\nd = {d}")
def encrypt(m, e, n):
c = primitive_exp(m, e, n)
print (f"\nC = {c}")
def decrypt(c, d, n):
m = primitive_exp(c, d, n)
print (f"\nM = {m}")
def recover_prime_factor(n, e, d):
p, q = prime_recovery(n, e, d)
print (f"\np = {p}\nq = {q}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Call function from pubcrypt module')
parser.add_argument('-g', type=int, help='Generate an RSA keypair. Indicate the bits size as an argument')
parser.add_argument('-enc', type=int, help='encryption your data')
parser.add_argument('-dec', type=int, help='decrypt your data')
parser.add_argument('-r', type=int, help='recover your primes factor. Indicate the public modulus as an argument')
parser.add_argument('-e', type=int, help='Public exponent. By default: e=65537')
parser.add_argument('-n', type=int, help='Public modulus')
parser.add_argument('-d', type=int, help='Private exponent')
args = parser.parse_args()
if args.g:
try:
if args.e == None:
args.e = 65537
generate_keypair(args.g, args.e)
except:
print ("Error: missing arguments '-e'")
elif args.enc:
try:
if args.e == None:
args.e = 65537
encrypt(args.enc, args.e, args.n)
except:
print ("Error: missing arguments '-e' and '-n'")
elif args.dec:
try:
decrypt(args.dec, args.d, args.n)
except:
print ("Error: missing arguments '-d' and '-n'")
elif args.r:
try:
if args.e == None:
args.e = 65537
recover_prime_factor(args.r, args.e, args.d)
except:
print ("Error: missing arguments '-r', '-e' and '-d'")