First commit
This commit is contained in:
parent
7bd9325a73
commit
c9b432c900
75
main.py
Normal file
75
main.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from lorem import text as loremtext
|
||||||
|
|
||||||
|
|
||||||
|
def checkArguments():
|
||||||
|
args = ArgumentParser(description='Cryptanalyst')
|
||||||
|
args.add_argument('-f', '--filename', help='Text to analyze')
|
||||||
|
args.add_argument('-s', '--stats', help='Statistics', action='store_true')
|
||||||
|
args.add_argument('-d', '--decrypt', help='Decrypt Caesar text', action='store_true')
|
||||||
|
return args.parse_args()
|
||||||
|
|
||||||
|
def readCipherText(filename):
|
||||||
|
file = []
|
||||||
|
with open(filename, 'r') as f:
|
||||||
|
data = f.readlines()
|
||||||
|
|
||||||
|
for entry in data:
|
||||||
|
file.append(entry.strip())
|
||||||
|
|
||||||
|
return file
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = checkArguments()
|
||||||
|
cipherFile = None
|
||||||
|
|
||||||
|
if args.filename:
|
||||||
|
cipherFile = readCipherText(args.filename)
|
||||||
|
|
||||||
|
if args.stats:
|
||||||
|
stats(cipherFile)
|
||||||
|
if args.decrypt:
|
||||||
|
decryptCaesarText(cipherFile)
|
||||||
|
|
||||||
|
|
||||||
|
def stats(file=None):
|
||||||
|
if file is None:
|
||||||
|
text = loremtext()
|
||||||
|
else:
|
||||||
|
text = file
|
||||||
|
|
||||||
|
lettersOccurence = {}
|
||||||
|
letterUpperToAscii = ord('A') # Get the ASCII code
|
||||||
|
for i in range (0, 26):
|
||||||
|
lettersOccurence[chr(i + letterUpperToAscii)] = 0
|
||||||
|
|
||||||
|
for line in text:
|
||||||
|
for letter in line:
|
||||||
|
letter = letter.upper()
|
||||||
|
if letter in lettersOccurence:
|
||||||
|
lettersOccurence[letter] = lettersOccurence[letter] + 1
|
||||||
|
|
||||||
|
x = [chr(letter + 65) for letter in range(0, 26)]
|
||||||
|
y = [lettersOccurence[tmp] for tmp in lettersOccurence]
|
||||||
|
|
||||||
|
largeur = 0.5
|
||||||
|
plt.bar(x, y, largeur)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
def decryptCaesarText(cipherFile):
|
||||||
|
if cipherFile is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
#for lines in cipherFile:
|
||||||
|
# splitLines = lines.split(' ')
|
||||||
|
|
||||||
|
# for word in splitLines:
|
||||||
|
# if len(word) <= 3:
|
||||||
|
# print(word)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
2
test.txt.crypt
Normal file
2
test.txt.crypt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
AJGNNQ YQTNF!
|
||||||
|
LG UWKU WP OGUUCIG GP ENCKT !!
|
Loading…
Reference in New Issue
Block a user