def test():
    message = 'The quick brown fox jumped over the lazy dog!. 1234#'
    key = 100
    cipher = CaesarCipher()
    cipher.encryptText(message, key)
    encrypted = cipher.encrypted
    unencrypted = cipher.decryptText(key)
    assert unencrypted == message
def test_brute_force():
    message = 'The quick brown fox jumped over the lazy dog!. 1234#'
    key = 95
    cipher = CaesarCipher()
    cipher.encryptText(message, key)
    encrypted = cipher.encrypted

    for k in range(1, cipher.RNG_CHAR + 1):
        unencrypted = cipher.decryptText(k)
        print(f'Testing key = {k:02d} :', unencrypted)
        if unencrypted == message:
            break
    print(f'Key found after {k} attempts!')
    assert unencrypted == message
Example #3
0
def main(process, key, input_file, output_file):
    with open(input_file, "r") as f:
        text = f.read()

    cipher_obj = CaesarCipher()
    if process == "encrypt":
        result = cipher_obj.encrypt(text, key)
    elif process == "decrypt":
        result = cipher_obj.decrypt(text, key)
    else:
        raise Exception("No such process")

    with open(output_file, "w") as f:
        f.write(result)
def main() -> None:
    print('Тестовый случай: русский язык, шифр Цезаря, ключ 10')
    test_cipher('russian_text.txt', CaesarCipher(), 10)

    print('Тестовый случай: английский язык, шифр Цезаря, ключ 5')
    test_cipher('english_text.txt', CaesarCipher(), 5)

    print(
        'Тестовый случай: русский язык, шифр Виженера, ключ \'суперсекретныйключ\''
    )
    test_cipher('russian_text.txt', ViginereCipher(),
                'суперсекретныйключ'.encode('utf-8'))

    print(
        'Тестовый случай: английский язык, шифр Цезаря, ключ \'supersecretkey\''
    )
    test_cipher('english_text.txt', ViginereCipher(), b'supersecretkey')
Example #5
0
class TestEncryptString(unittest.TestCase):
    def setUp(self):
        self.message = CaesarCipher(PLAIN, SHIFT_BY)

    def test_isinstance(self):
        self.assertIsInstance(self.message, CaesarCipher)

    def test_encrypt(self):
        check = self.message.encrypt()
        expect = CIPHER
        self.assertEquals(check, expect)
Example #6
0
class TestDecryptString(unittest.TestCase):
    def setUp(self):
        self.message = CaesarCipher(CIPHER, SHIFT_BY)

    def test_isinstance(self):
        self.assertIsInstance(self.message, CaesarCipher)

    def test_decrypt(self):
        check = self.message.decrypt()
        expect = PLAIN.upper()
        self.assertEquals(check, expect)
Example #7
0
 def setUp(self):
     self.message = CaesarCipher(PLAIN, SHIFT_BY)
Example #8
0
 def setUp(self):
     self.cipher = CaesarCipher()
class CaesarCipherTestCase(unittest.TestCase):
    def setUp(self):
        self.cipher = CaesarCipher()

    def test_should_get_fulswrjudild_to_criptografia_word(self):
        assert_equals(self.cipher.encrypt("criptografia"), "fulswrjudild")

    def test_should_get_criptografia_when_decoded_criptografia_word(self):
        encrypted = self.cipher.encrypt("criptografia")
        assert_equals(self.cipher.decrypt(encrypted), "criptografia")

    def test_should_get_zope_when_decoded_zope_word(self):
        encrypted = self.cipher.encrypt("zope")
        assert_equals(self.cipher.decrypt(encrypted), "zope")

    def test_should_get_warm_when_decoded_warm_word(self):
        encrypted = self.cipher.encrypt("warm")
        assert_equals(self.cipher.decrypt(encrypted), "warm")

    def test_should_get_xmen_when_decoded_xmen_word(self):
        encrypted = self.cipher.encrypt("xmen")
        assert_equals(self.cipher.decrypt(encrypted), "xmen")

    def test_should_get_livelongandprosper_when_decoded_livelongandprosper_word(self):
        encrypted = self.cipher.encrypt("livelongandprosper")
        assert_equals(self.cipher.decrypt(encrypted), "livelongandprosper")

    def test_should_get_zope_when_decoded_zope_word_with_a_7_key_value(self):
        cipher = CaesarCipher(7)
        encrypted = cipher.encrypt("zope")
        assert_equals(cipher.decrypt(encrypted), "zope")
Example #10
0
#Created by Zoltan Szeman
#This is how Caeser used to cipher his private letters

from random import randint
from caesar_cipher import CaesarCipher
from decipher import Decipher

'''Ciphering'''
file = open("text-file\cryptonomicon_plaintext.txt")
msg = file.read()
alphabet = "abcdefghijklmnopqrstuvwxyz"
key = randint(1,25) #the key remains unknown
cipher = CaesarCipher(plaintext = msg, char_set = alphabet)
coded_msg = cipher.encrypt(key)

'''Deciphering'''
file_coded = open("text-file\cryptonomicon_ciphertext.txt","w+")
file_coded.write(coded_msg)
file_coded.seek(0)
msg_to_break = file_coded.read()
codebreak = Decipher(plaintext = "", ciphertext = msg_to_break, char_set = alphabet)

method = "f" #"f" - frequancy analysis or "b" - brute force codebreaking
if method == "f":    
    codebreak_key = codebreak.freq_analysis()
elif method == "b":
    codebreak_key = codebreak.brute_force()
    
decoded_msg = codebreak.decrypt(codebreak_key)
file_decoded = open("text-file\cryptonomicon_deciphered.txt","w+")
file_decoded.write(decoded_msg)
Example #11
0
 def test_invalid_key(self, key):
     with pytest.raises(ValueError) as err_msg:
         CaesarCipher(key)
     assert str(err_msg.value) == "Key must be an integer between 1 and 25."
Example #12
0
 def test_decipher(self):
     cipher = CaesarCipher(10)
     deciphered_msg = cipher.decipher("Klmn.opqRs#tuvw xyZAbcdef?Ghij")
     assert deciphered_msg == "Abcd.efgHi#jklm noPQrstuv?Wxyz"
Example #13
0
 def __init__(self, plaintext="", ciphertext="", char_set=""):
     '''CaesarCipher is the parent'''
     CaesarCipher.__init__(self, plaintext, ciphertext, char_set)
 def setUp(self):
     self.cipher = CaesarCipher()
Example #15
0
 def test_should_get_zope_when_decoded_zope_word_with_a_7_key_value(self):
     cipher = CaesarCipher(7)
     encrypted = cipher.encrypt("zope")
     assert_equals(cipher.decrypt(encrypted), "zope")
Example #16
0
 def setUp(self):
     self.message = CaesarCipher(CIPHER, SHIFT_BY)
Example #17
0
class CaesarCipherTestCase(unittest.TestCase):
    def setUp(self):
        self.cipher = CaesarCipher()

    def test_should_get_fulswrjudild_to_criptografia_word(self):
        assert_equals(self.cipher.encrypt("criptografia"), "fulswrjudild")

    def test_should_get_criptografia_when_decoded_criptografia_word(self):
        encrypted = self.cipher.encrypt("criptografia")
        assert_equals(self.cipher.decrypt(encrypted), "criptografia")

    def test_should_get_zope_when_decoded_zope_word(self):
        encrypted = self.cipher.encrypt("zope")
        assert_equals(self.cipher.decrypt(encrypted), "zope")

    def test_should_get_warm_when_decoded_warm_word(self):
        encrypted = self.cipher.encrypt("warm")
        assert_equals(self.cipher.decrypt(encrypted), "warm")

    def test_should_get_xmen_when_decoded_xmen_word(self):
        encrypted = self.cipher.encrypt("xmen")
        assert_equals(self.cipher.decrypt(encrypted), "xmen")

    def test_should_get_livelongandprosper_when_decoded_livelongandprosper_word(
            self):
        encrypted = self.cipher.encrypt("livelongandprosper")
        assert_equals(self.cipher.decrypt(encrypted), "livelongandprosper")

    def test_should_get_zope_when_decoded_zope_word_with_a_7_key_value(self):
        cipher = CaesarCipher(7)
        encrypted = cipher.encrypt("zope")
        assert_equals(cipher.decrypt(encrypted), "zope")
Example #18
0
N_TRAIN_SAMPLES = 100
N_TEST_SAMPLES = 50
MESS_LEN = 50
EMBEDDING_DIM = 5
HIDDEN_SIZE = 10

# Training
LR = 1e-3
N_EPOCHS = 15


#------------------------------------------------------------------------------
#	Main execution
#------------------------------------------------------------------------------
# Create dataset
cipher = CaesarCipher()
train_data = cipher.gen_data(n_samples=N_TRAIN_SAMPLES, mess_len=MESS_LEN)
test_data = cipher.gen_data(n_samples=N_TEST_SAMPLES, mess_len=MESS_LEN)

# Build model
network = Network(
	input_sz=cipher.n_vocab,
	embedding_dim=EMBEDDING_DIM,
	hidden_sz=HIDDEN_SIZE,
	output_sz=cipher.n_vocab,
)

# Loss and optimizer
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(network.parameters(), lr=LR)
 def test_should_get_zope_when_decoded_zope_word_with_a_7_key_value(self):
     cipher = CaesarCipher(7)
     encrypted = cipher.encrypt("zope")
     assert_equals(cipher.decrypt(encrypted), "zope")