-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex3.py
More file actions
74 lines (57 loc) · 1.55 KB
/
Copy pathex3.py
File metadata and controls
74 lines (57 loc) · 1.55 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
67
68
69
70
71
72
73
74
import time
import string
import os
import os.path
import filecmp
from Crypto.PublicKey import RSA
pkfile = "exercise/rsa_key.pub"
skfile = "exercise/rsa_key"
def rsa_keygen():
key = RSA.generate(4096);
with open(skfile, 'wb') as f:
f.write(key.exportKey('PEM'))
pk = key.publickey()
with open(pkfile, 'wb') as f:
f.write(pk.exportKey('PEM'))
def rsa_encrypt(fname):
with open(pkfile, 'rb') as f:
sk = RSA.importKey(f.read(), 'PEM');
with open(fname, 'r') as f:
pt = f.read()
if len(pt) > 128:
pt = pt[:128]
pt = pt.encode('ascii')
ct = sk.encrypt(pt, 0)[0]
with open(fname + '.enc', 'wb') as f:
f.write(ct)
def rsa_decrypt(fname):
with open(skfile, 'rb') as f:
sk = RSA.importKey(f.read(), 'PEM');
with open(fname + '.enc', 'rb') as f:
ct = f.read()
pt = sk.decrypt(ct).decode('ascii')
with open(fname + '.dec', 'w') as f:
f.write(pt)
alphabet = "abcdefghijklmnopqrstuvwxyz"
ex3_path = "exercise/ex3.enc"
fname = "file.txt"
# Generating permutations
permutation = ""
found = False
for i in alphabet:
for j in alphabet:
for k in alphabet:
with open(fname,'w') as f:
f.write(i+j+k)
permutation = i+j+k
rsa_encrypt(fname)
found = filecmp.cmp(ex3_path,fname+".enc")
os.remove(fname)
os.remove(fname+".enc")
if found:
break
if found:
break
if found:
break
print(permutation)