Exemple #1
0
 def test_caesar_custom_charsets(self, msg):
     # Test custom charsets
     charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
     plaintext = msg["plaintext"]
     ciphertext = "M EGBqD EqoDqF yqEEmsq"
     # Test encrypt
     c = caesar(plaintext, msg["key"], "encrypt", charset=charset)
     assert c == ciphertext
     # Test decrypt
     c = caesar(ciphertext, msg["key"], "decrypt", charset=charset)
     assert c == plaintext
 def run(self, text):
     """ Run the automatic key finding """
     # strip all non letter characters
     letters_only = letters_only_uppercase(text)
     # all the possible lengths to test
     lengths = list(range(2, min(50, len(letters_only))))
     # set the number of possibilities
     self.set_total_possibilities(len(lengths))
     # pre-calculate the entire text decrypted with all the caesar shifts
     reverse_caesars = {}
     for shift, letter in enumerate(ascii_uppercase):
         reverse_caesars[letter] = caesar(letters_only, -shift)
     # store the found keys
     found_keys = set()
     # iterate over each length to check
     for length in lengths:
         # try and find the best key that is `length` long
         key = best_n_long_key(letters_only, reverse_caesars, length)
         shifts = string_to_shifts(key)
         # check if key is double an existing key
         if len(key) % 2 == 0:
             first_half = key[:len(key) // 2]
             second_half = key[len(key) // 2:]
             if first_half == second_half and first_half in found_keys:
                 # this new key is double an existing key, the solver is done
                 self.done()
                 return
         found_keys.add(key)
         self.possibility(key, reverse_vigenere(text, shifts))
     self.done()
Exemple #3
0
 def test_caesar_exceptions_non_integer_key(self, msg):
     # Test non-integer key raises exception
     with pytest.raises(ValueError):
         caesar(msg["plaintext"], "foo", "encrypt")
Exemple #4
0
 def test_caesar_exceptions_bad_action(self, msg):
     # Test bad action
     with pytest.raises(ValueError):
         # Action must be either encrypt or decrypt
         caesar(msg["plaintext"], msg["key"], "foobar")
Exemple #5
0
 def test_caesar_exceptions_duplicates_in_charset(self, msg):
     # Test bad charset
     with pytest.raises(ValueError):
         # this contains duplicates so should raise a ValueError
         charset = "asdfghjklkkjhedbb"
         caesar(msg["plaintext"], msg["key"], "encrypt", charset=charset)
Exemple #6
0
 def test_caesar_decryption(self, msg):
     # Test decryption
     c = caesar(msg["caesar_ciphertext"], msg["key"], "decrypt")
     assert c == msg["plaintext"]
 def test_caesar_reverse(self):
     self.assertEqual(caesar("Spwwz Hzcwo", -11), "Hello World")
     self.assertEqual(
         caesar("Trvjri Tzgyvi zj r dfefrcgyrsvkzt jlsjkzklkzfe tzgyvi",
                -17),
         "Caesar Cipher is a monoalphabetic substitution cipher")