示例#1
0
def get_lang(message):
    if not (message.text == 'ru' or message.text == 'en'):
        bot.send_message(message.chat.id, "Language can be only en or ru!")
    else:
        dbworker.set_data(message.chat.id, 'lang', message.text)

        if dbworker.get_current_state(
                message.chat.id) == dbconfiguration.CAESAR_LANG_ET:
            bot.send_message(message.chat.id,
                             '''So, there is your output file!''')
            bot.send_document(
                message.chat.id,
                Caesar.encrypt_text(
                    dbworker.get_data(message.chat.id, 'file'),
                    dbworker.get_data(message.chat.id, 'shift'),
                    dbworker.get_data(message.chat.id,
                                      'lang')).encode('UTF-8'))
            dbworker.clear_data(message.chat.id)

        if dbworker.get_current_state(
                message.chat.id) == dbconfiguration.CAESAR_LANG_DT:
            bot.send_message(message.chat.id,
                             '''So, there is your output file!''')
            bot.send_document(
                message.chat.id,
                Caesar.decrypt_text(
                    dbworker.get_data(message.chat.id, 'file'),
                    dbworker.get_data(message.chat.id, 'shift'),
                    dbworker.get_data(message.chat.id,
                                      'lang')).encode('UTF-8'))
            dbworker.clear_data(message.chat.id)

        dbworker.set_state(message.chat.id, dbconfiguration.CAESAR, bot)
示例#2
0
def main(argv):
    # Create instance of the CipherInterface
    cipher = CipherInterface()
    if len(argv) < 6:
        print("ERROR: not enough arguments filled out")
    else:
        cipherName = argv[1]
        key = argv[2]
        encOrDec = argv[3]
        textInput = argv[4] + ".txt"
        textOutput = argv[5] + ".txt"
        # print(cipherName)
        # print(key)
        # print(encOrDec)
        # print(textInput)
        # print(textOutput)

    # Open inputfile to read contents
    # assign contents to variable to encrypt/decrypt
    inputFile = open(textInput, "rt")
    contents = inputFile.read()
    inputFile.close()
    # print("File Contents")
    # print(contents)

    # Choose the cipher
    if cipherName.lower() == "plf":
        print("Playfair is chosen")
        cipher = Playfair()

    elif cipherName.lower() == "vig":
        print("Vigenere is chosen")
        cipher = Vigenere()

    elif cipherName.lower() == "rfc":
        print("Railfence is chosen")
        cipher = Railfence()

    elif cipherName.lower() == "rts":
        print("Row Transposition is chosen")
        cipher = RowTransposition()

    elif cipherName.lower() == "ces":
        print("Caesar is chosen")
        cipher = Caesar()

    # Set the encryption key
    cipher.setKey(key)
    if (encOrDec.lower() == "enc"):
        # Perform encryption
        outputText = cipher.encrypt(contents)
    else:
        # Perform decryption
        outputText = cipher.decrypt(contents)
    # Create and write into output file the outputText
    outputFile = open(textOutput, "w+")
    outputFile.write(outputText)
    outputFile.close()
示例#3
0
def get_shift(message):
    try:
        shift = int(message.text)
        if shift <= 0:
            raise ValueError
    except ValueError:
        bot.send_message(message.chat.id, "Secret key must be natural number!")
    else:
        dbworker.set_data(message.chat.id, 'shift', shift)

        if dbworker.get_current_state(
                message.chat.id) == dbconfiguration.CAESAR_SHIFT_ENCRYPT:
            bot.send_message(message.chat.id,
                             '''So, there is your output file!''')
            bot.send_document(
                message.chat.id,
                Caesar.encrypt(dbworker.get_data(message.chat.id, 'file'),
                               dbworker.get_data(message.chat.id, 'shift')))
            dbworker.set_state(message.chat.id, dbconfiguration.CAESAR, bot)
            dbworker.clear_data(message.chat.id)

        if dbworker.get_current_state(
                message.chat.id) == dbconfiguration.CAESAR_SHIFT_DECRYPT:
            bot.send_message(message.chat.id,
                             '''So, there is your output file!''')
            bot.send_document(
                message.chat.id,
                Caesar.decrypt(dbworker.get_data(message.chat.id, 'file'),
                               dbworker.get_data(message.chat.id, 'shift')))
            dbworker.set_state(message.chat.id, dbconfiguration.CAESAR, bot)
            dbworker.clear_data(message.chat.id)

        if dbworker.get_current_state(
                message.chat.id) == dbconfiguration.CAESAR_SHIFT_ET:
            dbworker.set_state(message.chat.id, dbconfiguration.CAESAR_LANG_ET,
                               bot)
            bot.send_message(message.chat.id, '''Input language (ru or en)!''')

        if dbworker.get_current_state(
                message.chat.id) == dbconfiguration.CAESAR_SHIFT_DT:
            dbworker.set_state(message.chat.id, dbconfiguration.CAESAR_LANG_DT,
                               bot)
            bot.send_message(message.chat.id, '''Input language (ru or en)!''')
示例#4
0
class Affine(Cipher):
    """
    Affine cipher that uses a combination of Caesar and Multiplication cipher
    """

    def __init__(self):
        self.__caesar__ = Caesar()
        self.__multi__ = Multiplication()

    def encode(self, clear_text, key):
        """
        Encode message, first with multiplication then caesar.
        :param clear_text: clear text
        :param key: Tuple, (Multi_key, Caesar_key)
        :return: encoded message
        """
        encoded_multi = self.__multi__.encode(clear_text, key[0])
        encoded_caesar = self.__caesar__.encode(encoded_multi, key[1])
        return encoded_caesar

    def decode(self, encoded_text, key):
        """
        Decode message, first with caesar then multiplication
        :param encoded_text: encoded message
        :param key: Tuple, (Multi_key, Caesar_key)
        :return: decoded message
        """
        decoded_caesar = self.__caesar__.decode(encoded_text, key[1])
        decoded_multi = self.__multi__.decode(decoded_caesar, key[0])
        return decoded_multi

    def generate_keys(self):
        """
        Generates two keys for sender and receiver
        :return: keys with (Multiplication, Caesar) and inverse of that
        """

        key_1_1, key_2_1 = self.__multi__.generate_keys()
        key_1_2, key_2_2 = self.__caesar__.generate_keys()
        return (key_1_1, key_1_2), (key_2_1, key_2_2)

    def possible_keys(self):
        return self.__multi__.possible_keys() * self.__caesar__.possible_keys()
示例#5
0
from Caesar import Caesar
from Multiplication import Multiplication
from Sender import Sender
from Receiver import Receiver
from Hacker import Hacker
from Affine import Affine
from Unbreakable import Unbreakable
from RSA import RSA

cipher_list = {"Caesar": Caesar(), "Multiplication": Multiplication(),
               "Affine": Affine(), "Unbreakable": Unbreakable()}


def main():
    verify_ciphers()
    # verify_hacker()


def verify_hacker():
    for name, cipher in cipher_list.items():

        key_encrypt, key_decrypt = cipher.generate_keys()

        sender = Sender(cipher=cipher, key=key_encrypt)
        receiver = Receiver(cipher=cipher, key=key_decrypt)
        hacker = Hacker(cipher=cipher)

        clear_text = "Hello World"

        encrypted_message = sender.send_message(clear_text)
        decrypted_message = receiver.receive_message(encrypted_message)
示例#6
0
def ProcessDecrypt(text, key):
    chiper = Caesar(text, key)
    return chiper.decrypt()
示例#7
0
def ProcessEncrypt(text, key):
    chiper = Caesar(text, key)
    return chiper.encrypt()
示例#8
0
            cipherText = cipher.encrypt(text)
            fileWriter.write(cipherText)
            fileReader.close()
            fileWriter.close()

        if cipherName == 'RTS':
            cipher = RowTransposition()
            cipher.setKey(cipherKey)
            text = fileReader.read()
            cipherText = cipher.encrypt(text)
            fileWriter.write(cipherText)
            fileReader.close()
            fileWriter.close()

        if cipherName == 'CES':
            cipher = Caesar()
            cipher.setKey(cipherKey)
            text = fileReader.read()
            cipherText = cipher.encrypt(text)
            fileWriter.write(cipherText)
            fileReader.close()
            fileWriter.close()

        if cipherName == 'RFC':
            cipher = RailFence()
            cipher.setKey(cipherKey)
            text = fileReader.read()
            cipherText = cipher.encrypt(text)
            fileWriter.write(cipherText)
            fileReader.close()
            fileWriter.close()
示例#9
0
 def __init__(self):
     self.__caesar__ = Caesar()
     self.__multi__ = Multiplication()