calculate key length
This commit is contained in:
parent
ef1636dfef
commit
e3b7c1bd2d
54
vignere.py
54
vignere.py
@ -1,5 +1,11 @@
|
||||
#!/usr/bin/python3
|
||||
import sys, getopt, os
|
||||
import sys, getopt, os, collections, math
|
||||
|
||||
def chartonum(char):
|
||||
return ord(char)-97
|
||||
|
||||
def numtochar(int):
|
||||
return chr(int+97)
|
||||
|
||||
def encrypt(string,key):
|
||||
lowerstr=string.lower()
|
||||
@ -9,10 +15,10 @@ def encrypt(string,key):
|
||||
resultnums = []
|
||||
resultstring = ""
|
||||
for c in lowerstr:
|
||||
strnums.append(ord(c)-97)
|
||||
strnums.append(chartonum(c))
|
||||
print(strnums)
|
||||
for c in key:
|
||||
keynums.append(ord(c)-97)
|
||||
keynums.append(chartonum(c))
|
||||
print(keynums)
|
||||
keypointer=0
|
||||
for n in strnums:
|
||||
@ -20,7 +26,7 @@ def encrypt(string,key):
|
||||
keypointer = (keypointer+1) % (len(key))
|
||||
print(resultnums)
|
||||
for c in resultnums:
|
||||
resultstring = resultstring+chr(c+97)
|
||||
resultstring = resultstring+numtochar(c)
|
||||
print(resultstring)
|
||||
|
||||
def decrypt(string,key):
|
||||
@ -31,18 +37,18 @@ def decrypt(string,key):
|
||||
resultnums = []
|
||||
resultstring = ""
|
||||
for c in lowerstr:
|
||||
strnums.append(ord(c)-97)
|
||||
print(strnums)
|
||||
strnums.append(chartonum(c))
|
||||
#print(strnums)
|
||||
for c in key:
|
||||
keynums.append(ord(c)-97)
|
||||
print(keynums)
|
||||
keynums.append(chartonum(c))
|
||||
#print(keynums)
|
||||
keypointer=0
|
||||
for n in strnums:
|
||||
resultnums.append((n-keynums[keypointer])%26)
|
||||
keypointer = (keypointer+1) % (len(key))
|
||||
print(resultnums)
|
||||
#print(resultnums)
|
||||
for c in resultnums:
|
||||
resultstring = resultstring+chr(c+97)
|
||||
resultstring = resultstring+numtochar(c)
|
||||
print(resultstring)
|
||||
|
||||
def trigraphcount(string):
|
||||
@ -55,22 +61,26 @@ def trigraphcount(string):
|
||||
if index == -1:
|
||||
break
|
||||
print(trigraph+' found at', index)
|
||||
if not trigraph in distances and index!=i:
|
||||
distances[trigraph] = index - i
|
||||
offset = index - i
|
||||
if not trigraph in distances and offset>0:
|
||||
distances[trigraph] = [offset]
|
||||
elif offset>0:
|
||||
distances[trigraph].append(offset)
|
||||
index += 3
|
||||
#print(distances)
|
||||
print(sorted(distances.items(), key=lambda x: x[1]))
|
||||
#print(list(distances.values()))
|
||||
dists = [item for sublist in list(distances.values()) for item in sublist]
|
||||
#print(dists)
|
||||
count = collections.Counter(dists)
|
||||
most_commons = count.most_common(n=2)
|
||||
print(most_commons)
|
||||
key_len = math.gcd(most_commons[0][0], most_commons[1][0])
|
||||
print(key_len)
|
||||
lettercount(string,key_len)
|
||||
#print(sorted(distances.items(), key=lambda x: x[1]))
|
||||
|
||||
def lettercount(string,dist):
|
||||
letters = {}
|
||||
for i in range(0,len(string)-dist,dist):
|
||||
print(string[i:i+dist])
|
||||
for c in string:
|
||||
if c in letters:
|
||||
letters[c] = letters[c]+1
|
||||
else:
|
||||
letters[c] = 1
|
||||
print(letters)
|
||||
pass
|
||||
|
||||
def main(argv):
|
||||
key = ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user