@cv88/

# Autokey algorithm

## No description

Files
• main.py
main.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
```
```message=input('message?').upper()
#Scoring algorithm
import math
for n in range(len(qg)):
qg[n]=qg[n].split()
qg=dict(qg)

def score(string):
string=string.upper()
global qg
score=0
for n in range(len(string)-3):
snip=string[n:n+4]
try:
score+=math.log10(int(qg[snip]))
except:
score+=math.log10(0.5)
return score
#Scoring algorithm

m=message
message=[]
alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for n in range(len(m)):
if m[n] in alphabet:
message.append(alphabet.find(m[n]))

#Decrypting algorithm
def decrypt(message, key):
output=[]
line=key[:]
for n in range(len(message)):
output.append((message[n]-line[n])%26)
line.append((message[n]-line[n])%26)
o=output
output=""
for x in o:
output=output+alphabet[x]
return output
#Derypting algorithm

for kl in range(1,20):
key=[]
for c in range(kl):
key.append(0)
for c in range(kl):
bestn=0
bests=0
for n in range(26):
key[c]=n
output=decrypt(message, key)
if score(output)>bests:
bestn=n
bests=score(output)
#print(bestn)
key[c]=bestn
print("Keylength:")
print(kl)
print()
print('Message:')
print(decrypt(message, key))
print()
print('Key:')
output=""
for n in key:
output+=alphabet[n]
print(output)
print('\n\n')
```