-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher8.py
107 lines (78 loc) · 3.23 KB
/
cipher8.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
mode = input('Do you want to encrypt or decrypt (e,d)? ').lower()
words = [' the ',' be ',' to ',' of ',' and ',' in ',' that ',' have ',' I ',' it ',' for ',' not ',' on ',' with ',' he ',' as ',' you ',' do ',' at ',' this ',' but ',' his ',' by ',' from ',' they ',' we ',' say ',' her ',' she ',' or ',' an ',' will ',' my ',' one ',' all ',' would ',' there ',' their ',' what ',' so ',' up ',' out ',' if ',' about ',' who ',' get ',' which ',' go ',' me ',' when ',' make ',' can ',' like ',' time ',' no ',' just ',' him ',' know ',' take ',' people ',' into ',' year ',' your ',' good ',' some ',' could ',' them ',' see ',' other ',' than ',' then ',' now ',' look ',' only ',' come ',' its ',' over ',' think ',' also ',' back ',' after ',' use ',' two ',' how ',' our ',' work ',' first ',' well ',' way ',' even ',' new ',' want ',' because ',' any ',' these ',' give ',' day ',' most ',' us ']
def encrypt():
box = []
row = []
count = 1
for letter in message:
if len(row) < key:
row.append(letter)
elif len(row) == key:
box.append(row)
row = []
row.append(letter)
if count == len(message):
spaces = key - len(row)
for space in range(spaces):
row.append(' ')
box.append(row)
count += 1
fencryption = ''
for letter in range(key):
for row in box:
fencryption += row[letter]
return fencryption
def decrypt():
deciphers = []
for answer in range(2,len(code)):
decryption = ''
cipher = []
row = []
count = 1
for letter in code:
if len(row) < answer:
row.append(letter)
elif len(row) == answer:
cipher.append(row)
row = []
row.append(letter)
if count == len(code):
spaces = answer - len(row)
for space in range(spaces):
row.append(' ')
cipher.append(row)
count += 1
for letter in range(answer):
for row in cipher:
decryption += row[letter]
cipherkey = len(code)/answer
matches = 0
for word in words:
if word in decryption:
matches += 1
if matches != 0:
deciphers.append((decryption,matches))
deciphers = sorted(deciphers, key=lambda matches: matches[1])[::-1]
return deciphers
while True:
if mode.startswith('e'):
message = input('What\'s your message? ')
key = input('Choose your key man ')
while True:
try:
key = int(key)
break
except ValueError:
key = input('Try to enter a number')
fencryption = encrypt()
print('\n'+ fencryption)
break
if mode.startswith('d'):
code = input('Enter your code: ')
decryptions = decrypt()
for i in decryptions:
print(i[0])
break
else:
mode = input('I\'m sorry i didn\'t get it ("encrypt" or "decrypt")').lower()
# create a dictionary with the most common words webscrap https://en.wikipedia.org/wiki/Most_common_words_in_English