def main(): parser = argparse.ArgumentParser( description='Encrypt or decrpyt a message using Playfair Cipher') parser.add_argument('-k', default="None", help="The keyword to be used in the cipher") args = parser.parse_args() if args.k == 'None': keyword = input("What is your keyword?: ") else: keyword = args.k cipher = Playfair.Playfair(keyword) option = input("Would you like to encrypt or decrypt a message?: ") message = input("What is the message you would like to " + option + "?: ") if option == 'encrypt' or option == 'Encrypt' or option[0] == 'e': splitMessage = cipher.splitMessage(message) newMessage = cipher.encrypt(splitMessage) if option == 'decrypt' or option == 'Decrypt' or option[0] == 'd': newMessage = cipher.decrypt(message) print("Here is your " + option + "ed message:") print(newMessage)
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")
def testWikipediaExample(self): wikiCipher = Playfair.Playfair("playfair example") message = "Hide the gold in the tree stump" expectedSplit = "hi de th eg ol di nt he tr ex es tu mp" actualSplit = wikiCipher.splitMessage(message) self.assertEqual(actualSplit, expectedSplit) expectedEncryption = "BM OD ZB XD NA BE KU DM UI XM MO UV IF" actualEncryption = wikiCipher.encrypt(actualSplit) self.assertEqual(actualEncryption, expectedEncryption)
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...")
def setUp(self): self.cipher = Playfair.Playfair(self.keyword)
# Make sure we have enough arguments, if not print the help message if len(sys.argv) < 5: parser.print_help() sys.exit(1) # Actually parse the arguments arguments = parser.parse_args() 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"]:
#Main program import sys import Caesar import RailFence import Row_Transposition import Playfair import Vigenere #Checking user input if len(sys.argv) < 6: print("You are missing arguments, check your input.") 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.")