-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.py
73 lines (55 loc) · 1.98 KB
/
encrypt.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
71
72
73
# this program reads a file key.txt and uses it as a "one-time pad" to encrypt msg.txt
# into an equal length file encrypted_msg.txt that can then be decrypted with a knowledge of
# a one-time pad system (modulo 26, no punctuations, just characters, case doesn't matter)
# and the key
#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('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
msgEncryptNum = [];
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;
msgEncryptNum.append(s);
msgEncrypt = denumberize(msgEncryptNum);
#write encrypted message
file = open('encrypted_msg.txt','w');
for i,li in enumerate(msgEncrypt):
file.write(li);
file.close();