def test_encrypt(self): """Tests encrypt function for Vigenere cipher""" self.assertEqual( Vigenere.encrypt('A' * 26, ALPHABET), ALPHABET) self.assertEqual( Vigenere.encrypt('A' * 26, ALPHABET[:13]), ALPHABET[:13] * 2) self.assertEqual( Vigenere.encrypt(ALPHABET, ALPHABET), ALPHABET[::2] * 2) with self.assertRaises(Vigenere.VigenereError): Vigenere.encrypt(5, ALPHABET) with self.assertRaises(Vigenere.VigenereError): Vigenere.encrypt(ALPHABET, range(5))
def encrypt(text, key): """Encrypts text using the One-Time Pad cipher E(x) = Vigenere.encrypt(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.encrypt(utils.fix_text(text), utils.fix_text(key))
def __encrypt(message, key): # Split message in half Ln = message[:len(message) / 2] Rn = message[len(message) / 2:] # Get keys 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): Rn = Vigenere.encrypt(Rn, Vigenere_key) Rn = Rail_Fence.encrypt(Rn, Rail_Fence_key) Rn = Caesar.encrypt(Rn, Caesar_key) Ln, Rn = Rn, Ln return Ln + Rn
def validate_guess(keylens, fileids, alphabet, num_candidates): keys = list(keylens.keys()) #for each fileid, we will want to check whether the correct guess is made #for all keys found = 0 not_found = 0 indecipherable = 0 for fileid in fileids: #print("PLAINTEXT: ", fileid) #setting plaintext plaintext = corpus.gutenberg.raw(fileid) plaintext = plaintext[0:300] for key in keys: ciphertext, ciphertext_file = vg.encrypt(key, plaintext) #getting most likely key lengths cipher_match_indices, cipher_matches = vg.find_matches(ciphertext) most_likely_lengths, sorted_factor_counts = vg.likely_key_lengths( ciphertext) if type(sorted_factor_counts) == int: indecipherable += 1 continue most_likely_lengths = sorted_factor_counts[:num_candidates] #print("Included: ", keylens.get(key) in most_likely_lengths, "|Key length: ", keylens.get(key), "|Choices: ", most_likely_lengths) if keylens.get(key) != 1: if keylens.get(key) in most_likely_lengths: #print("Included: ", True, "|Key length: ", keylens.get(key), "|Choices: ", most_likely_lengths) found += 1 else: print("Included: ", False, "|Key length: ", keylens.get(key), "|Choices: ",\ most_likely_lengths, "|Next: ", sorted_factor_counts[:num_candidates+1]) not_found += 1 percent_top5 = (found) / (found + not_found) percent_indecipherable = (indecipherable) / (found + not_found + indecipherable) return percent_top5, percent_indecipherable
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!")