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
|
#!/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):
|
def encrypt(string,key):
|
||||||
lowerstr=string.lower()
|
lowerstr=string.lower()
|
||||||
@ -9,10 +15,10 @@ def encrypt(string,key):
|
|||||||
resultnums = []
|
resultnums = []
|
||||||
resultstring = ""
|
resultstring = ""
|
||||||
for c in lowerstr:
|
for c in lowerstr:
|
||||||
strnums.append(ord(c)-97)
|
strnums.append(chartonum(c))
|
||||||
print(strnums)
|
print(strnums)
|
||||||
for c in key:
|
for c in key:
|
||||||
keynums.append(ord(c)-97)
|
keynums.append(chartonum(c))
|
||||||
print(keynums)
|
print(keynums)
|
||||||
keypointer=0
|
keypointer=0
|
||||||
for n in strnums:
|
for n in strnums:
|
||||||
@ -20,7 +26,7 @@ def encrypt(string,key):
|
|||||||
keypointer = (keypointer+1) % (len(key))
|
keypointer = (keypointer+1) % (len(key))
|
||||||
print(resultnums)
|
print(resultnums)
|
||||||
for c in resultnums:
|
for c in resultnums:
|
||||||
resultstring = resultstring+chr(c+97)
|
resultstring = resultstring+numtochar(c)
|
||||||
print(resultstring)
|
print(resultstring)
|
||||||
|
|
||||||
def decrypt(string,key):
|
def decrypt(string,key):
|
||||||
@ -31,18 +37,18 @@ def decrypt(string,key):
|
|||||||
resultnums = []
|
resultnums = []
|
||||||
resultstring = ""
|
resultstring = ""
|
||||||
for c in lowerstr:
|
for c in lowerstr:
|
||||||
strnums.append(ord(c)-97)
|
strnums.append(chartonum(c))
|
||||||
print(strnums)
|
#print(strnums)
|
||||||
for c in key:
|
for c in key:
|
||||||
keynums.append(ord(c)-97)
|
keynums.append(chartonum(c))
|
||||||
print(keynums)
|
#print(keynums)
|
||||||
keypointer=0
|
keypointer=0
|
||||||
for n in strnums:
|
for n in strnums:
|
||||||
resultnums.append((n-keynums[keypointer])%26)
|
resultnums.append((n-keynums[keypointer])%26)
|
||||||
keypointer = (keypointer+1) % (len(key))
|
keypointer = (keypointer+1) % (len(key))
|
||||||
print(resultnums)
|
#print(resultnums)
|
||||||
for c in resultnums:
|
for c in resultnums:
|
||||||
resultstring = resultstring+chr(c+97)
|
resultstring = resultstring+numtochar(c)
|
||||||
print(resultstring)
|
print(resultstring)
|
||||||
|
|
||||||
def trigraphcount(string):
|
def trigraphcount(string):
|
||||||
@ -55,22 +61,26 @@ def trigraphcount(string):
|
|||||||
if index == -1:
|
if index == -1:
|
||||||
break
|
break
|
||||||
print(trigraph+' found at', index)
|
print(trigraph+' found at', index)
|
||||||
if not trigraph in distances and index!=i:
|
offset = index - i
|
||||||
distances[trigraph] = index - i
|
if not trigraph in distances and offset>0:
|
||||||
|
distances[trigraph] = [offset]
|
||||||
|
elif offset>0:
|
||||||
|
distances[trigraph].append(offset)
|
||||||
index += 3
|
index += 3
|
||||||
#print(distances)
|
#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):
|
def lettercount(string,dist):
|
||||||
letters = {}
|
pass
|
||||||
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)
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
key = ""
|
key = ""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user