109 lines
3.3 KiB
Python
Executable File
109 lines
3.3 KiB
Python
Executable File
#!/usr/bin/python3
|
|
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()
|
|
print("Encrypting "+lowerstr+" with key "+key)
|
|
strnums = []
|
|
keynums = []
|
|
resultnums = []
|
|
resultstring = ""
|
|
for c in lowerstr:
|
|
strnums.append(chartonum(c))
|
|
print(strnums)
|
|
for c in key:
|
|
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)
|
|
for c in resultnums:
|
|
resultstring = resultstring+numtochar(c)
|
|
print(resultstring)
|
|
|
|
def decrypt(string,key):
|
|
lowerstr=string.lower()
|
|
print("Decrypting "+lowerstr+" with key "+key)
|
|
strnums = []
|
|
keynums = []
|
|
resultnums = []
|
|
resultstring = ""
|
|
for c in lowerstr:
|
|
strnums.append(chartonum(c))
|
|
#print(strnums)
|
|
for c in key:
|
|
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)
|
|
for c in resultnums:
|
|
resultstring = resultstring+numtochar(c)
|
|
print(resultstring)
|
|
|
|
def trigraphcount(string):
|
|
distances = {}
|
|
for i in range(0,len(string)-2):
|
|
trigraph = string[i:i+3]
|
|
index = 0
|
|
while index < len(string):
|
|
index = string.find(trigraph, index)
|
|
if index == -1:
|
|
break
|
|
print(trigraph+' found at', index)
|
|
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(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):
|
|
pass
|
|
|
|
def main(argv):
|
|
key = ""
|
|
try:
|
|
opts, args = getopt.getopt(argv,"hk:e:d:t:l:",["key=","encrypt=","decrypt=","trigraphs=","lettercount="])
|
|
except getopt.GetoptError:
|
|
print(os.path.basename(__file__)+' -k <key> -e <plain text>')
|
|
print(os.path.basename(__file__)+' -k <key> -d <encrypted text>')
|
|
sys.exit(2)
|
|
for opt, arg in opts:
|
|
if opt == '-h':
|
|
print(os.path.basename(__file__)+' -k <key> -e <plain text>')
|
|
print(os.path.basename(__file__)+' -k <key> -d <encrypted text>')
|
|
sys.exit()
|
|
elif opt in ("-k", "--key"):
|
|
key = arg
|
|
elif opt in ("-e", "--encrypt"):
|
|
encrypt(arg,key)
|
|
elif opt in ("-d", "--decrypt"):
|
|
decrypt(arg,key)
|
|
elif opt in ("-t", "--trigraphs"):
|
|
trigraphcount(arg)
|
|
elif opt in ("-l", "--lettercount"):
|
|
lettercount(arg,int(key))
|
|
if __name__ == '__main__':
|
|
main(sys.argv[1:]) |