def test_vigenere_larger(self): key = generate_alpha(random.randint(100, 1000)).lower() vigenere = VigenereCipher(key) plaintext = generate_alpha(random.randint(10000, 100000)).lower() ciphertext = vigenere.encrypt(plaintext) message = vigenere.decrypt(ciphertext) self.assertSequenceEqual(plaintext, message)
def test_random_key(self): block_size = 5 key = HillCipher.generate_key(block_size) message = generate_alpha(50).lower() hill = HillCipher(key) cipher = hill.encrypt(message) self.assertEqual(message, hill.decrypt(cipher))
def test_vigenere_large(self): key = 'hereissomerandomkeythaticouldntthinkof' vigenere = VigenereCipher(key) plaintext = generate_alpha(random.randint(1000, 10000)).lower() ciphertext = vigenere.encrypt(plaintext) message = vigenere.decrypt(ciphertext) self.assertSequenceEqual(plaintext, message)
def test_message_padding(self): text = generate_alpha(13) key = 0b101010010 cipher = ToyDesCipher(key) ciphertext = cipher.encrypt(text) plaintext = cipher.decrypt(ciphertext) # Compare against the text string with the trailing punctuation removed. self.assertEqual(text, plaintext[:-2])
def test_message_padding(self): text = generate_alpha(17) key = 76173234526173235131 cipher = DesCipher(key) ciphertext = cipher.encrypt(text) plaintext = cipher.decrypt(ciphertext) # Compare against the text string with the trailing padding self.assertEqual(text, plaintext[:-7])
def test_random_key_larger(self): # Decrypt fails on larger block_sizes block_size = 13 key = HillCipher.generate_key(block_size) message = generate_alpha(5 * block_size).lower() hill = HillCipher(key) cipher = hill.encrypt(message) self.assertEqual(message, hill.decrypt(cipher))
def test_random_key_large(self): block_size = 9 key = HillCipher.generate_key(block_size) # Generate a *large* random message message = generate_alpha(500 * block_size).lower() hill = HillCipher(key) cipher = hill.encrypt(message) self.assertEqual(message, hill.decrypt(cipher))
def test_encrypt_2(self): # Encrypt and decrypt a random text 10 times... That's gotta verify it works right? for i in range(10): text = generate_alpha(360) key = 0b010001111 cipher = ToyDesCipher(key) ciphertext = cipher.encrypt(text) plaintext = cipher.decrypt(ciphertext) self.assertEqual(text, plaintext)
def test_encrypt_2(self): # Encrypt and decrypt a random text 10 times... That's gotta verify it works right? for i in range(10): text = generate_alpha(128) key = 776481980476271347 cipher = DesCipher(key) ciphertext = cipher.encrypt(text) plaintext = cipher.decrypt(ciphertext) self.assertEqual(text, plaintext)
def test_lazy_pad_2(self): seq = 'abcdefg' seq = ''.join(lazy_pad(seq, 10, 'X')) self.assertSequenceEqual(seq, 'abcdefgXXX') # A multiple of 5 seq = generate_alpha(45) # pad the lowercase alphabetic string with numbers chosen at random padded_seq = lazy_pad(seq, 7, string.digits) for char, padded_char in zip(seq, padded_seq): self.assertEqual(char, padded_char) for remaining in padded_seq: self.assertIn(remaining, string.digits)
def test_encrypt_decrypt_2(self): size = 100 initial_values = numpy.ones(size, dtype=int) initial_values[:size // 2] = 0 numpy.random.shuffle(initial_values) coeffs = numpy.ones(size, dtype=int) coeffs[:size // 2] = 0 numpy.random.shuffle(coeffs) cipher = LfsrCipher(initial_values, coeffs) plaintext = generate_alpha(1000) ciphertext = cipher.encrypt(plaintext) actual = cipher.decrypt(ciphertext) self.assertSequenceEqual(actual, plaintext)
def test_bitstream(self): string = generate_alpha(1000) bitstream = Bitstream(bytes(string, 'ascii')) actual = ''.join(str(bit) for bit in bitstream) expected = binary_string(string) self.assertSequenceEqual(actual, expected)