def test_decrypt(self): """Tests decrypt function for Caesar cipher""" self.assertEqual( Vigenere.decrypt(ALPHABET, ALPHABET), 'A' * 26) self.assertEqual( Vigenere.decrypt(ALPHABET[:13] * 2, ALPHABET[:13]), 'A' * 26) self.assertEqual( Vigenere.decrypt(ALPHABET[::2] * 2, ALPHABET), ALPHABET) with self.assertRaises(Vigenere.VigenereError): Vigenere.decrypt(5, ALPHABET) with self.assertRaises(Vigenere.VigenereError): Vigenere.decrypt(ALPHABET, range(5))
def decrypt(text, key): """Decrypts text using the One-Time Pad cipher D(x) = Vigenere.decrypt(text, key) text : string key : string """ if type(text) is not str: raise OneTimePadError('Can only encrypt strings.') if type(key) is not str: raise OneTimePadError('key must be a string.') if len(key) < len(text): raise OneTimePadError('key must be at least the same length as text.') return Vigenere.decrypt(utils.fix_text(text), utils.fix_text(key))
def __decrypt(message, key): # Split message in half B = message[:len(message) / 2] C = message[len(message) / 2:] Rail_Fence_key = Jordan_Harman_Algorithm_1.get_rail_key(key) Caesar_key = Jordan_Harman_Algorithm_1.get_caesar_key(key) Vigenere_key = Jordan_Harman_Algorithm_1.get_vigenere_key(key) # Begin Rounds for x in range(0, 8): C = Caesar.decrypt(C, Caesar_key) C = Rail_Fence.decrypt(C, Rail_Fence_key) C = Vigenere.decrypt(C, Vigenere_key) B, C = C, B return B + C
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!")