예제 #1
0
def main():
    print()
    cipher = None

    # Determine which type of cipher
    if cipher_name == 'PLF':
        cipher = Playfair()
    elif cipher_name == 'RTS':
        cipher = RowTransposition()
    elif cipher_name == 'RFC' or cipher_name == 'CES':
        try:
            shift_amt = int(key)
        except ValueError:
            print("ERR: Please enter a number as the key for a Caesar/Railfence cipher!\n")
            return
        cipher = Railfence() if cipher_name == 'RFC' else Caesar()
    elif cipher_name == 'VIG':
        cipher = Vigenere()
    else:
        print("ERR: Not a valid cipher type! Please use:")
        print("\tPLF, RTS, RFC, VIG, or CES\n")
        return
    
    # Set the key
    if not cipher.setKey(key):
        print("ERR: Invalid key, try again.\n")
        return

    inFile = None
    outFile = None
    
    # Attempt to read in the text the user wants to encrypt/decrypt
    try:
        inFile = open(input_file, "r")
    except FileNotFoundError:
        print("ERR: '", input_file, "' cannot be opened! Try a valid file\n")
        return

    outFile = open(output_file, 'w')
    
    # Perfrom the encryption/decryption
    if method == "ENC":
        plaintext = inFile.read()
        ciphertext = cipher.encrypt(plaintext)
        outFile.write(ciphertext)
        print("Encryption was successfull!")

    elif method == "DEC":
        ciphertext = inFile.read()
        plaintext = cipher.decrypt(ciphertext)
        outFile.write(plaintext)
        print("Decryption was successfull!")

    else:
        print("ERR: Incorrect method. Please enter 'ENC' or 'DEC'\n")
        return

    print("Thank you for using this program.\n")
예제 #2
0
                  enable_events=True,
              )
          ],
          [
              sg.Text("", size=(8, 1)),
              sg.Button("encrypt", size=(10, 1), border_width=2, pad=(15, 15)),
              sg.Button("decrypt", size=(10, 1), border_width=2, pad=(15, 15)),
              sg.Button("attack", size=(10, 1), border_width=2, pad=(15, 15))
          ],
          [
              sg.Text("Ciphertext: ", size=(8, 1)),
              sg.Multiline(key="-CIPHERTEXT-")
          ]]

window = sg.Window(title="Caesar Cipher Tool", layout=layout, margins=(30, 50))
crypto = Caesar.Caesar()

while True:
    event, values = window.read()

    if event == None or event == 'Exit':
        logger.warning("EXIT...")
        break

    elif event == "encrypt":
        plain = values["-PLAINTEXT-"]
        cipher = crypto.encrypt(plain)
        window["-CIPHERTEXT-"].update(cipher)
        logger.info(f"ENCRYPT: {plain.strip()} -> {cipher.strip()}")

    elif event == "decrypt":
예제 #3
0
def handle_input(cipher, key, in_file, out_file, mode):
    # Standardize the cipher
    the_cipher = cipher.lower()

    # Try to open files specified
    try:
        input_file = open(in_file, "r")
        output_file = open(out_file, "w")

        text = input_file.read()
        input_file.close()
    except IOError:
        # If there was an error opening the file, abort
        print("Error opening file")
        return

    # Execute Caesar cipher
    if the_cipher == "caesar":
        CIPHER = Caesar.Caesar()

        if CIPHER.set_key(key):
            if mode == "e":
                print("Using Caesar cipher to encrypt: {}".format(in_file))
                encrypt = CIPHER.encrypt(text)
                output_file.write(encrypt)

            elif mode == "d":
                print("Using Caesar cipher to decrypt: {}".format(out_file))
                decrypt = CIPHER.decrpyt(text)
                output_file.write(decrypt)

            output_file.close()
        else:
            print("Key: \"{}\" is not a valid key.\n "
                  "No encryption will occur.".format(key))

    # Execute Playfair cipher
    elif the_cipher == "playfair":
        CIPHER = Playfair.Playfair()

        if CIPHER.set_key(key):
            CIPHER.construct_key_matrix(key)
            if mode == "e":
                print("Using Playfair cipher to encrypt: {}".format(in_file))
                encrypt = CIPHER.encrypt(text)
                output_file.write(encrypt)

            elif mode == "d":
                print("Using Playfair cipher to decrypt: {}".format(out_file))
                decrypt = CIPHER.decrpyt(text)
                output_file.write(decrypt)

            output_file.close()
        else:
            print("Key: \"{}\" is not a valid key.\n "
                  "No encryption will occur.".format(key))

    # Execute Railfence cipher
    elif the_cipher == "railfence":
        CIPHER = Railfence.Railfence()

        if CIPHER.set_key(key):
            if mode == "e":
                print("Using Railfence cipher to encrypt: {}".format(in_file))
                encrypt = CIPHER.encrypt(text)
                output_file.write(encrypt)

            elif mode == "d":
                print("Using Railfence cipher to decrypt: {}".format(out_file))
                decrypt = CIPHER.decrpyt(text)
                output_file.write(decrypt)

            output_file.close()
        else:
            print("Key: \"{}\" is not a valid key.\n "
                  "No encryption will occur.".format(key))

    # Execute Row Transposition cipher
    elif the_cipher == "rowtransposition":
        CIPHER = RowTransposition.RowTransposition()

        if CIPHER.set_key(key):
            if mode == "e":
                print("Using Row Transposition cipher to encrypt: {}".format(
                    in_file))
                encrypt = CIPHER.encrypt(text)
                output_file.write(encrypt)

            elif mode == "d":
                print("Using Row Transposition cipher to decrypt: {}".format(
                    out_file))
                decrypt = CIPHER.decrpyt(text)
                output_file.write(decrypt)

            output_file.close()
        else:
            print("Key: \"{}\" is not a valid key.\n "
                  "No encryption will occur.".format(key))

    # Execute Vigenre cipher
    elif the_cipher == "vigenre":
        CIPHER = Vigenre.Vigenre()

        if CIPHER.set_key(key):
            CIPHER.build_rows()
            if mode == "e":
                print("Using Vigenre cipher to encrypt: {}".format(in_file))
                encrypt = CIPHER.encrypt(text)
                output_file.write(encrypt)

            elif mode == "d":
                print("Using Vigenre cipher to decrypt: {}".format(out_file))
                decrypt = CIPHER.decrpyt(text)
                output_file.write(decrypt)

            output_file.close()
        else:
            print("Key: \"{}\" is not a valid key.\n "
                  "No encryption will occur.".format(key))

    # User didn't enter a correct cipher
    else:
        print("Unknown cipher entered...")
예제 #4
0
 def __init__(self):
     self.book = Book()
     self.caesar = Caesar()
     self.rsa = RSA()
예제 #5
0
cipher = CipherInterface()

assert arguments.cipher in valid_ciphers, "Cipher not recognized. Use --help for more info."

# set cipher to specified cipher
if(arguments.cipher == "PLF"):
	cipher = Playfair()
elif(arguments.cipher == "RTS"):
	cipher = RowTransposition()
elif(arguments.cipher == "RFC"):
	cipher = Railfence()
elif(arguments.cipher == "VIG"):
	cipher = Vigenre()
elif(arguments.cipher == "CES"):
	cipher = Caesar()
elif(arguments.cipher == "HIL"):
	cipher = Hill()
elif(arguments.cipher == "EGM"):
	cipher = Enigma()

# Normalize and set the cipher key
if arguments.cipher in ["VIG", "PLF", "EGM", "HIL"]:
	normalizedKey = ""
	for char in str(arguments.key).lower():
		if 96 < ord(char) < 123:
			normalizedKey += char
	assert len(normalizedKey) > 0, "Zero length input key"
elif arguments.cipher in ["RFC", "CES"]:
	normalizedKey = int(arguments.key)
else:
예제 #6
0
파일: cipher.py 프로젝트: lekevin42/ciphers
def choose_cipher(cipher_name, key, option, input_file, output_file):
    #Playfair
    if cipher_name == "PLF":
        if option == "ENC":
            pass

        elif option == "DEC":
            pass

        else:
            print("Invalid option! Options are ENC/DEC")

    #Row Transposition
    elif cipher_name == "RTS":
        rowtransposition = RowTransposition()
        rowtransposition.setKey(key)

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = rowtransposition.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = rowtransposition.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    #Railfence
    elif cipher_name == "RFC":
        railfence = Railfence()
        railfence.setKey(key)

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = railfence.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = railfence.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    #Vigenere
    elif cipher_name == "VIG":
        vigenere = Vigenere()
        vigenere.setKey(key)

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = vigenere.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = vigenere.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    #Caesar
    elif cipher_name == "CES":
        caesar = Caesar()
        caesar.setKey(int(key))

        if option == "ENC":
            plain_text = read_file(input_file)
            cipher_text = caesar.encrypt(plain_text)
            write_file(output_file, cipher_text)

        elif option == "DEC":
            cipher_text = read_file(input_file)
            plain_text = caesar.decrypt(cipher_text)
            write_file(output_file, plain_text)

        else:
            print("Invalid option! Options are ENC/DEC")

    else:
        print("Invalid cipher name chosen!")
예제 #7
0
else:
    #Playfair Cipher
    if str(sys.argv[1]) == 'PLF':
        cipher_type = Playfair.Playfair()
    #Row_Transposition Cipher
    elif str(sys.argv[1]) == 'RTS':
        cipher_type = Row_Transposition.Row_Transposition()
    #RailFence Cipher
    elif str(sys.argv[1]) == 'RFC':
        cipher_type = RailFence.Railfence()
    #Vigenere Cipher
    elif str(sys.argv[1]) == 'VIG':
        cipher_type = Vigenere.Vigenere()
    #Caesar Cipher
    elif str(sys.argv[1]) == 'CES':
        cipher_type = Caesar.Caesar()
    #Invalid Input
    else:
        print("Unrecognized cipher type, check your spelling and try again.")
        exit()
    #Setting key from inputfile
    if cipher_type.setKey(sys.argv[2]):
        inFile = open(str(sys.argv[4]), "r")
        inputText = inFile.read()
        if str(sys.argv[3]) == 'ENC':
            outputText = cipher_type.encrypt(inputText)
        elif str(sys.argv[3] == 'DEC'):
            outputText = cipher_type.decrypt(inputText)
        #Invalid user input
        else:
            print("Unrecognized coding type, did you want Encode or Decode?")
예제 #8
0
#import all ciphers and converters
#they were all made as classes to not confuse the encrypt methods with each other
#import * for classes
from A1Z26 import *
from Caesar import *
from Combined import *
from MorseCode import *
from OneTimePad import *
from Vigenere import *
from Binary import *
from Hex import *
from Octal import *

#create instances of each class
a1 = A1()
caesar = Caesar()
comb = Combined()
morse = Morse()
otp = OneTimePad()
vig = Vigenere()
binary = Binary()
hexa = Hexa()
octal = Octal()

#in order to avoid long if statements, functions/methods added to lists
encFunc = [
    vig.encrypt, otp.encrypt, caesar.encrypt, a1.encrypt, morse.encrypt,
    comb.encrypt
]
decFunc = [
    vig.decrypt, otp.decrypt, caesar.decrypt, a1.decrypt, morse.decrypt,