-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.py
70 lines (52 loc) · 1.76 KB
/
decrypt.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
59
60
61
62
63
64
65
66
67
68
69
70
# this program decrypts what encrypt.py encrypted
#functions
#function to make a list of numbers representing each letter in the input, disregards everything
#not a letter
#0 is a, 1 is b, etc.
def numberize(data):
letters = [ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z' ];
Letters = uppercase(letters);
output = [];
for li in data:
for chaar in li:
for idx, le in enumerate(letters):
if (chaar == le or chaar == Letters[idx]):
output.append(idx); #from 0 to 25
return output;
#end function
#denumberize
def denumberize(data):
letters = [ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z' ];
output = [];
for i in data:
output.append(letters[i]);
return output;
#end function
#function to make every character of a string in a list uppercase
def uppercase(data):
upped = [];
for li in data:
upped.append(li.upper());
return upped;
#end function
oneTimePad = open('key.txt','r');
message = open('encrypted_msg.txt','r');
key = oneTimePad.readlines(); #reads in each line, stores in a list appended with \n
msg = message.readlines();
keyNumbers = numberize(key);
msgNumbers = numberize(msg);
#use the modulo arithmetic to make our coded message
msgDecryptNum = [];
keyLen = len(keyNumbers);
for i, msg_i in enumerate(msgNumbers):
#sum modulo, so it's from 0 to 25
s = (msg_i-keyNumbers[i%keyLen])%26;
msgDecryptNum.append(s);
msgDecrypt = denumberize(msgDecryptNum);
#write encrypted message
file = open('decrypted_msg.txt','w');
for i,li in enumerate(msgDecrypt):
file.write(li);
file.close();