Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ASCI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class ASCI:
num_to_char = dict()
char_to_num = dict()

for num_char in range(0,128):
char = chr(num_char)
pos = len(char_to_num)
num_to_char[pos] = char
char_to_num[char] = pos

@staticmethod
def contains(char: str) -> bool:
return (char in ASCI.char_to_num)
pass

@staticmethod
def isalpha(char: str) -> bool:
return (contains(char) and char.isalpha())
pass

@staticmethod
def lower(char: str) -> str:

return char.lower()

@staticmethod
def order(char: str) -> int:

return ASCI.char_to_num[char]
pass

@staticmethod
def getchar(num: int) -> str:

return ASCI.num_to_char[num]
pass

@staticmethod
def size() -> int:
return len(ASCI.num_to_char)
pass

pass
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
# encryptor
Encruptor Python MIPT 2021
Aplication runnig by comand :

python3 encryptor.py[command][parameters]


Commands:
encode - encoding text
--cipher [type of cipher] || Cipher type
--key [key] || Key to encrypt
--input [file path] || Path to input file
--output [file path] || Path to output file


decode - decoding text
--cipher [type of cipher] || Cipher type
--key [key] || Key to encrypt
--input [file path] || Path to input file
--output [file path] || Path to output file


train - training model
--input [file path] || Text to analyze
--output [file path] || Output model


hack - hack caesar cipher
--input [file path] || File to hack
--output [file path] || Hacked file
--model [file path] || Model file

Example: python3 main.py encode --cipher caesar --key 231 --input ./example.txt --output ./encrypted.txt
python3 main.py encode --cipher vernam --key 231 --input ./example.txt --output ./encrypted.txt
48 changes: 48 additions & 0 deletions alphabet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Alphabet:
num_to_char = dict()
char_to_num = dict()
for num_char in range(ord('a'), ord('z') + 1):
char = chr(num_char)
pos = len(char_to_num)
num_to_char[pos] = char
char_to_num[char] = pos

for num_char in range(ord('A'), ord('Z') + 1):
char = chr(num_char)
pos = len(char_to_num)
num_to_char[pos] = char
char_to_num[char] = pos

@staticmethod
def contains(char: str) -> bool:
return (char in Alphabet.char_to_num)
pass

@staticmethod
def isalpha(char: str) -> bool:
return (contains(char) and char.isalpha())
pass

@staticmethod
def lower(char: str) -> str:

return char.lower()

@staticmethod
def order(char: str) -> int:

return Alphabet.char_to_num[char]
pass

@staticmethod
def getchar(num: int) -> str:

return Alphabet.num_to_char[num]
pass

@staticmethod
def size() -> int:
return len(Alphabet.num_to_char)
pass

pass
37 changes: 37 additions & 0 deletions caesar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import abc
from alphabet import Alphabet
from encoder_abstract import Encoder


class CaesarEncoder(Encoder):

def __init__(self, key: int):
self.key = int(key) % Alphabet.size()


def encode_char(self, char: str, pos: int) -> str:
if Alphabet.contains(char):
sum_code = Alphabet.order(char) + self.key
return Alphabet.getchar(sum_code % Alphabet.size())
else:
return char





class CaesarDecoder(Encoder):

def __init__(self, key: int):
self.key = int(key) % Alphabet.size()


def encode_char(self, char: str, pos: int) -> str:
if Alphabet.contains(char):
sum_code = Alphabet.order(char) - self.key
return Alphabet.getchar(sum_code % Alphabet.size())
else:
return char



40 changes: 40 additions & 0 deletions encode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from caesar import CaesarEncoder, CaesarDecoder
from vigenere import VigenereEncoder, VigenereDecoder
from vernam import VernamDecoder, VernamEncoder
from hacker import CaesarHacker
from train import FrequencyTrainer

def encode(cipher: str, key, text: str) -> str:

if cipher == 'vigenere':
encoder = VigenereEncoder(key)
elif cipher == 'caesar':
encoder = CaesarEncoder(key)
elif cipher == 'vernam':
encoder = VernamEncoder(key)
return encoder.encode(text)



def decode(cipher: str, key, text: str) -> str:
if cipher == 'vigenere':
decoder = VigenereDecoder(key)
elif cipher == 'caesar':
decoder = CaesarDecoder(key)
elif cipher == 'vernam':
decoder = VernamDecoder(key)
return decoder.encode(text)


def train(text: str) -> str:
trainer = FrequencyTrainer()
trainer.train(text)
return trainer.get_json_model()
pass


def hack(model: dict, text: str) -> str:
hacker = CaesarHacker(model)
hacker_decoder = CaesarDecoder(hacker.hack_key(text))
return hacker_decoder.encode(text)
pass
25 changes: 25 additions & 0 deletions encoder_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import abc


class Encoder():

__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def __init__(self, key):
pass

@abc.abstractmethod
def encode_char(self, char: str, pos: int) -> str:
pass

def encode(self, text: str) -> str:
result = ""
position = 0
for char in text:
result += self.encode_char(char, position)
position += 1
return result
pass

pass
3 changes: 3 additions & 0 deletions example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

William Shakespeare
William Shakespeare (baptised 26 April 1564 – died 23 April 1616) was an English poet and playwright, widely regarded as the greatest writer in the English language and the world's pre-eminent dramatist. He is often called England's national poet and the "Bard of Avon" (or simply "The Bard"). His surviving works consist of 38 plays, 154 sonnets, two long narrative poems, and several other poems. His plays have been translated into every major living language, and are performed more often than those of any other playwright. Shakespeare was born and raised in Stratford-upon-Avon. At the age of 18 he married Anne Hathaway, who bore him three children: Susanna, and twins Hamnet and Judith. Between 1585 and 1592 he began a successful career in London as an actor, writer, and part owner of the playing company the Lord Chamberlain's Men, later known as the King's Men. He appears to have retired to Stratford around 1613, where he died three years later. Few records of Shakespeare's private life survive, and there has been considerable speculation about such matters as his sexuality, religious beliefs, and whether the works attributed to him were written by others. Shakespeare produced most of his known work between 1590 and 1613. His early plays were mainly comedies and histories, genres he raised to the peak of sophistication and artistry by the end of the sixteenth century. Next he wrote mainly tragedies until about 1608, including Hamlet, King Lear, and Macbeth, considered some of the finest examples in the English language. In his last phase, he wrote tragicomedies, also known as romances, and collaborated with other playwrights. Many of his plays were published in editions of varying quality and accuracy during his lifetime, and in 1623 two of his former theatrical colleagues published the First Folio, a collected edition of his dramatic works that included all but two of the plays now recognised as Shakespeare's. Shakespeare was a respected poet and playwright in his own day, but his reputation did not rise to its present heights until the nineteenth century. The Romantics, in particular, acclaimed Shakespeare's genius, and the Victorians hero-worshipped Shakespeare with a reverence that George Bernard Shaw called "bardolatry". In the twentieth century, his work was repeatedly adopted and rediscovered by new movements in scholarship and performance. His plays remain highly popular today and are consistently performed and reinterpreted in diverse cultural and political contexts throughout the world. Source: Wikipedia
65 changes: 65 additions & 0 deletions hacker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from abc import ABCMeta, abstractmethod
from train import FrequencyTrainer
from alphabet import Alphabet


class Hacker:

__metaclass__ = ABCMeta

@abstractmethod
def __init__(self, model):
self.model = dict(model)
pass

@abstractmethod
def hack_text(self, text: str) -> str:
pass

pass


class CaesarHacker(Hacker):

def __init__(self, model):
super().__init__(model)
pass

def find_frequency(self, text: str):

self.frequency = {}

for char in text:
if not Alphabet.contains(char):
continue
if char not in self.frequency:
self.frequency[char] = 0
self.frequency[char] += 1
pass

pass

def hack_key(self, text: str):

self.find_frequency(text)

min_shift = 0
min_delta = -1

for shift in range(0, Alphabet.size()):
delta = 0
for (char, freq) in self.frequency.items():
num_new_char = (Alphabet.order(char) + shift) % Alphabet.size()
new_char = Alphabet.getchar(num_new_char)

delta += (self.frequency[char] if char in self.frequency else 0) ** 2 - \
(self.model[new_char] if new_char in self.model else 0) ** 2

if min_delta == -1 or delta < min_delta:
min_delta = delta
min_shift = shift

return Alphabet.size() - min_shift
pass

pass
Loading