Example #1
0
def main():
    print("Welcome to encryption/decrpytion program.")
    print()
    name = str(input("Please enter your name: "))
    print("Hi {},".format(name.capitalize()))
    encryption = {
        'Alberti': Alberti,
        'Atbash': Atbash,
        'Polybius': PolybiusSquare,
        'Transposition': Transposition
    }

    game = True
    while game:
        picked = str(
            input("Please pick one of the ciphers: \n-{} \n".format('\n-'.join(
                key for key in encryption.keys())))).capitalize()

        if picked not in encryption.keys():
            continue
        # Special cases
        if picked == 'Alberti':
            start_letter = str(
                input("Please Enter Albert inner allignment letter: "))
            print(
                "Warning! Alberti decrption is not implemented. If you want to decrypt choose others."
            )
            cipher = Alberti(start_letter)
        elif picked == 'Transposition':
            secret_word = str(input("Please Enter Secret word (ex. ZEBRAS): "))
            cipher = Transposition(key=secret_word)
        else:
            cipher = encryption[picked]()
        method = str(
            input("Do you want to encrpyt (en) or decrpyt (de)? ")).lower()
        if method == 'en':
            try:
                text = str(input("What is your text for encryption: \n"))
                pad = str(input("Please enter the pad number (ex. XMCKL): "))
                encrypted = cipher.encrypt(text)
                print(cipher.one_time_pad(encrypted, pad, method='encrypt'))
            except TypeError:
                raise ValueError("Input must be text")
        elif method == 'de':
            try:
                text = str(input("What is your text for decryption: \n"))
                pad = str(input("Please enter the pad number (ex.XMCKL): "))
                text_pad = cipher.one_time_pad(text, pad, method='decrypt')
                print(cipher.decrypt(text_pad))
            except TypeError:
                raise ValueError("Input must be text")
        print()
        cont = str(input("Do you want to continue (yes/no) : "))
        if not cont.lower() == 'yes':
            game = False
Example #2
0
def main():
    symbols = string.ascii_lowercase

    # first argument is the cipher
    cipher = sys.argv[1]

    # second arugment is the key
    key = sys.argv[2]

    # 3rd argument is the encrypt/Decrypt
    mode = sys.argv[3]

    # 4th argument is the input text
    input_txt = sys.argv[4]

    # 5th arugment is the output text
    output_txt = sys.argv[5]

    plainText = ''
    cipherText = ''

    text = open(input_txt)
    if mode == 'encrypt':
        plainText = text.read()
    if mode == 'decrypt':
        cipherText = text.read()

    if cipher == 'caesar' or cipher == 'Caesar':
        caesar = Caesar(key, symbols)
        if mode == 'encrypt':
            cipherText = open(output_txt, 'w')
            cipherText.write(caesar.encrypt(plainText))
            cipherText.close()
        if mode == 'decrypt':
            plainText = open(output_txt, 'w')
            plainText.write(caesar.decrypt(cipherText))
            plainText.close()

    if cipher == 'playfair' or cipher == 'Playfair':
        playfair = Playfair(key)
        if mode == 'encrypt':
            cipherText = open(output_txt, 'w')
            cipherText.write(playfair.encrypt(plainText))
            cipherText.close()
        if mode == 'decrypt':
            plainText = open(output_txt, 'w')
            plainText.write(playfair.decrypt(cipherText))
            plainText.close()

    if cipher == 'vigenere' or cipher == 'Vigenere':
        vigenere = Vigenere(key, symbols)
        if mode == 'encrypt':
            cipherText = open(output_txt, 'w')
            cipherText.write(vigenere.encrypt(plainText))
            cipherText.close()
        if mode == 'decrypt':
            plainText = open(output_txt, 'w')
            plainText.write(vigenere.decrypt(cipherText))
            plainText.close()

    if cipher == 'transposition' or cipher == 'Transposition':
        transposition = Transposition(key)
        if mode == 'encrypt':
            cipherText = open(output_txt, 'w')
            cipherText.write(transposition.encrypt(plainText))
            cipherText.close()
        if mode == 'decrypt':
            plainText = open(output_txt, 'w')
            plainText.write(transposition.decrypt(cipherText))
            plainText.close()

    if cipher == 'railfence' or cipher == 'Railfence':
        rail = RailFence(key)
        if mode == 'encrypt':
            cipherText = open(output_txt, 'w')
            cipherText.write(rail.encrypt(plainText))
            cipherText.close()
        if mode == 'decrypt':
            plainText = open(output_txt, 'w')
            plainText.write(rail.decrypt(cipherText))
            plainText.close()
Example #3
0
def run_cipher():
    #print welcome and available ciphers
    print("Welcome to myCipher. We can encrypt or decrypt using the following ciphers: \n")
    print(" -Affine Cipher\n")
    print(" -Caesar Cipher\n")
    print(" -Keyword Cipher\n")
    print(" -Transposition Cipher\n")


    #variables
    cipher_invalid = True
    valid_ciphers = ["affine", "caesar", "keyword", "transposition"]
    e_or_d_invalid = True


    #Loop until valid cipher is chosen
    while cipher_invalid:
        cipher = input("Which cipher would you like to use? ").lower()
        if cipher in valid_ciphers:
            cipher_invalid = False
        else:
            print("\nNot valid cipher")


    #loop until encrypt or decrypt is chosen
    while e_or_d_invalid:
        encrypt_or_decrypt = input("Excellent choice! Are you encrypting or decrypting? Type E/d ").lower()
        if encrypt_or_decrypt == "e" or encrypt_or_decrypt == "d":
            e_or_d_invalid = False
        else:
            print("\n Not sure whether to encrypt or decrypt! Type E/d?")


    #give message to encrypt or decrypt
    msg = input("What is your message? ")


    #encrypt or decrypt branching
    if encrypt_or_decrypt == "e":
        if cipher == "caesar":
            e_msg = Caesar().encrypt(msg)
        elif cipher == "affine":
            e_msg = Affine().encrypt(msg)
        elif cipher == "keyword":
            keyword = input("What is your keyword? ")
            e_msg = Keyword(keyword).encrypt(msg)
        elif cipher == "transposition":
            e_msg = Transposition().encrypt(msg)

        print(e_msg)

    else:
        if cipher == "caesar":
            d_msg = Caesar().decrypt(msg)
        elif cipher == "affine":
            d_msg = Affine().decrypt(msg)
        elif cipher == "keyword":
            keyword = input("What is your keyword? ")
            d_msg = Keyword(keyword).decrypt(msg)
        elif cipher == "transposition":
            d_msg = Transposition().decrypt(msg)

        print(d_msg)