Exemplo n.º 1
0
def test_caesar_basic():
    mess = "UNMENSAJECONÑ"
    mess1, mess7 = "VÑNFÑTBKFDPÑO", "BTSLTZHPLJVTU"
    assert c1.cipher(mess) == mess1
    assert c2.cipher(mess) == mess7
    assert c3.cipher(mess) == mess
    assert c1.decipher(mess1) == mess
    assert c2.decipher(mess7) == mess
    assert c3.decipher(mess) == mess
    caesar = Caesar(alphabet)
    crip = caesar.cipher(mess)
    assert mess == caesar.decipher(crip)
Exemplo n.º 2
0
 def __init__(self, alphabet, password=None):
     #Recomendación, ingeniárselas para no cargar siempre O(n^2) en memoria aunque esto no
     #será evaluado, con n el tamaño del alfabeto.
     """
     Constructor de clase, recibe un parámetro obligatorio correspondiente al alfabeto
     y un parámetro opcional que es la palabra clave, en caso de ser None, entonces
     generar una palabra pseudoaleatoria de al menos tamaño 4.
     :param alphabet: Alfabeto a trabajar con el cifrado.
     :param password: El password que puede ser o no dada por el usuario.
     """
     self.alphabet = alphabet
     if password == None:
         self.password = "******"
     else:
         self.password = password
     self.cesar = Caesar(self.alphabet)
Exemplo n.º 3
0
        receiver.set_key(key[1])
    else:
        sender.set_key(key)
        receiver.set_key(key)
    message = "shady crypto"

    print("Key is: ", key)
    print("Plaintext: ", message)
    crypted = message = sender.operate_cipher(message)
    print("Cryptedtext : ", message)
    message = receiver.operate_cipher(message)
    print("Cryptedtext converted back to Plaintext: ", message)

    if cipher.verify("text hacking example", key):
        print("Cipher is verified")
    else:
        print("Cipher is invalid")

    if not rsa:
        hacker = Hacker()
        print("Hacker will hack this cryptedtext: ", crypted)
        hacked = hacker.operate_cipher(crypted, cipher)
        print("Message:", '"' + str(hacked[0]) + '"', "is hacked with Key:", hacked[1])
    print("\n"*2)

main(Caesar())
main(MultiplicationCipher())
main(AffineCipher())
main(UnbreakableCipher())
main(RSACipher(), 1)
Exemplo n.º 4
0
from caesar_cipher import Caesar

alphabet = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ"
c1, c2, c3 = Caesar(alphabet, 1), Caesar(alphabet, 7), Caesar(alphabet,27)

def aux_tests(caesar, message, ciphered):
    one = caesar.cipher(message) == ciphered
    two = caesar.decipher(ciphered) == message
    return (one, two)

def test_caesar_basic():
    mess = "UNMENSAJECONÑ"
    mess1, mess7 = "VÑNFÑTBKFDPÑO", "BTSLTZHPLJVTU"
    assert c1.cipher(mess) == mess1
    assert c2.cipher(mess) == mess7
    assert c3.cipher(mess) == mess
    assert c1.decipher(mess1) == mess
    assert c2.decipher(mess7) == mess
    assert c3.decipher(mess) == mess
    caesar = Caesar(alphabet)
    crip = caesar.cipher(mess)
    assert mess == caesar.decipher(crip)

def test_caesar_spaces():
    mess = "UN MENSAJE CON Ñ Y ESPACIOS"
    mess1, mess7 = "VÑ NFÑTBKF DPÑ O Z FTQBDJPT", "BT SLTZHPL JVT U F LZWHJOVZ"
    assert c1.cipher(mess, True) == mess1
    assert c2.cipher(mess, True) == mess7
    assert c1.decipher(mess1, True) == mess
    assert c2.decipher(mess7, True) == mess
Exemplo n.º 5
0
def welcome():
    """Enables the user to select a cipher and either encrypt or
    decrypt a message. Once complete the encrypted/decrypted message
    will be returned, and the user will be prompted to restart or quit.
    """

    clear()
    cipher_choice = None
    encrypt_or_decrypt = None
    cipher_dict = {
        '1': 'Caesar',
        '2': 'Keyword',
        '3': 'Affine',
        '4': 'Transposition'
    }

    print("""Welcome to the Secret Messages project for treehouse techdegree.
    Select a cipher from the list below or press 'q' to quit:\n
    1 - Caesar
    2 - Keyword
    3 - Affine
    4 - Transposition""")

    # Select cipher type.
    while cipher_choice is None:
        cipher_choice = input("Enter 1, 2, 3, or 4 to begin.\n>> ").lower()
        if cipher_choice not in ['1', '2', '3', '4']:
            if cipher_choice == 'q':
                print("Good Bye.")
                sys.exit()
            else:
                print("Invalid selection, Try again.")
                cipher_choice = None

    print("You have selected {}.".format(cipher_dict[cipher_choice]))

    # Select Encryption or decryption.
    while encrypt_or_decrypt is None:
        encrypt_or_decrypt = input("Would you like to encrypt or decrypt?\n>>\
").lower()
        if encrypt_or_decrypt not in ['encrypt', 'decrypt']:
            if encrypt_or_decrypt == 'q':
                print("Good Bye.")
                sys.exit()
            else:
                print("Invalid selection, Try again.")
                encrypt_or_decrypt = None

    # Prints Caesar encryption/decrytion.
    clear()
    if cipher_choice == '1':
        if encrypt_or_decrypt == 'encrypt':
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Caesar().encrypt()))
        else:
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Caesar().decrypt()))

    # Prints Keyword encryption/decrytion.
    if cipher_choice == '2':
        if encrypt_or_decrypt == 'encrypt':
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Keyword().encrypt()))
        else:
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Keyword().decrypt()))

    # Prints Affine encryption/decrytion.
    if cipher_choice == '3':
        if encrypt_or_decrypt == 'encrypt':
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Affine().encrypt()))
        else:
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Affine().decrypt()))

    # Prints Transposition encryption/decrytion.
    if cipher_choice == '4':
        if encrypt_or_decrypt == 'encrypt':
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Transposition().encrypt()))
        else:
            print("The {}ed message is: {}".format(encrypt_or_decrypt,
                                                   Transposition().decrypt()))

    # Ask user to restart or quit.
    restart = input("Would you like to encrypt/decrypt another message? Y/n\n\
>> ").upper()
    if restart == 'Y':
        welcome()
    else:
        print("Good Bye.")
        sys.exit()
Exemplo n.º 6
0
 def __init__(self):
     super().__init__()
     self.multiplication = MultiplicationCipher()
     self.caesar = Caesar()