def test_encrypted_message_try_pads(self):
        pds = (('Monoo#', [[22, 3, 32, 36, 41, 66], [31, 3, 92, 42, 49, 56], [58, 56, 75, 15, 1, 93], [5,10,2,3,0,2], [21, 92, 39, 23, 89, 2], [87, 69, 69, 11, 76, 62]], 'Hello!', [5,10,2,3,0,2]),
                ('05<Nvv`\\.8_0qvh?b?', [[42, 27, 38, 56, 8, 70, 7, 45, 17, 54, 51, 53, 69, 50, 40, 86, 67, 73], [59, 44, 50, 90, 86, 13, 13, 60, 64, 46, 70, 74, 45, 86, 5, 93, 15, 57], [43, 94, 13, 15, 33, 72, 27, 94, 28, 20, 54, 74, 67, 54, 7, 86, 70, 57], [25, 28, 94, 37, 58, 10, 52, 63, 30, 92, 72, 43, 34, 4, 3, 56, 73, 22], [72, 54, 52, 43, 47, 36, 67, 93, 16, 8, 25, 23, 22, 28, 59, 10, 2, 54], [89, 81, 94, 89, 23, 26, 53, 44, 31, 78, 50, 45, 71, 82, 79, 76, 22, 8]], 'ThiS iS MixED cASe', [59, 44, 50, 90, 86, 13, 13, 60, 64, 46, 70, 74, 45, 86, 5, 93, 15, 57]),
                ("8fxvr6/C'9 _^[G4uXCN", [[51, 32, 46, 74, 57, 81, 50, 58, 12, 78, 24, 76, 34, 66, 50, 60, 77, 5, 86, 75], [69, 37, 11, 94, 53, 18, 61, 72, 7, 45, 69, 88, 5, 58, 31, 56, 31, 44, 67, 41], [27, 35, 38, 26, 2, 84, 86, 85, 77, 40, 89, 80, 41, 78, 86, 5, 39, 50, 18, 43], [30, 12, 35, 7, 86, 43, 81, 87, 43, 57, 10, 26, 8, 31, 83, 56, 94, 83, 94, 33], [68, 85, 19, 19, 9, 52, 34, 35, 67, 48, 30, 76, 74, 33, 40, 24, 87, 38, 26, 46], [56, 34, 14, 73, 67, 35, 15, 17, 1, 16, 51, 7, 28, 31, 46, 26, 68, 84, 21, 91]], 'Special Chars:~{}2) ', [68, 85, 19, 19, 9, 52, 34, 35, 67, 48, 30, 76, 74, 33, 40, 24, 87, 38, 26, 46]),
                ('m]+h{eF&p.f\\eT]?Xb;K!{5', [[2, 48, 28, 94, 78, 42, 81, 58, 70, 78, 34, 47, 36, 47, 9, 39, 6, 45, 25, 82, 82, 49, 44], [11, 17, 9, 5, 71, 72, 35, 64, 44, 37, 59, 59, 80, 71, 59, 13, 86, 92, 44, 49, 38, 7, 15], [14, 60, 33, 28, 54, 63, 46, 46, 9, 73, 33, 25, 33, 87, 59, 69, 28, 92, 12, 22, 68, 40, 70], [25, 84, 33, 84, 91, 92, 68, 18, 80, 33, 86, 77, 93, 78, 74, 31, 68, 89, 49, 68, 12, 8, 18], [52, 52, 84, 23, 89, 81, 58, 71, 23, 31, 16, 27, 14, 11, 61, 91, 29, 32, 50, 37, 70, 59, 39], [3, 1, 91, 67, 74, 89, 76, 13, 64, 16, 90, 77, 7, 69, 12, 2, 73, 67, 15, 53, 47, 5, 94]], 'This has longer shifts#', [25, 84, 33, 84, 91, 92, 68, 18, 80, 33, 86, 77, 93, 78, 74, 31, 68, 89, 49, 68, 12, 8, 18]))
        for enc_msg,pads,exp_msg,exp_pad in pds:
            enc = student.EncryptedMessage(enc_msg)
            result_pad, result_msg = enc.decrypt_message_try_pads(pads)
            self.assertEqual(exp_pad, result_pad, "decrypt_message_try_pads returned {} as the best pad but {} was expected".format(result_pad, exp_pad))
            self.assertEqual(exp_msg, result_msg, "decrypt_message_try_pads returned the correct pad and {} as the decrypted story when {} was expected".format(result_msg, exp_msg))

        enc = student.EncryptedMessage("bad")
        result_pad, result_msg = enc.decrypt_message_try_pads([[1,2,3],[2,3,4]])
        self.assertEqual([1,2,3], result_pad, "decrypt_message_try_pads should return the first pad if none of them decrypt")
        self.assertEqual('a_a', result_msg, "decrypt_message_try_pads should return the first pad's decryption if none of them decrypt")
Exemple #2
0
    def test_valid_word_checking(self):

        error_msg = "Expected best shift to be %s and best decryption to be %s for encrypted message '%s', but got shift of %s and message '%s'. Are you sure you're checking for valid words correctly?"
        encrypted = 'qoghzsg, qoh,'
        expected_decrypt = 'castles, cat,'
        with redirect_stdout(open(os.devnull, "w")):
            c = student.EncryptedMessage(encrypted)
        shift, actual = c.decrypt_message()
        self.assertEqual(actual, expected_decrypt, error_msg % (14, expected_decrypt, encrypted, shift, actual))
 def test_encrypted_message_decrypt(self):
     pds = (('Monoo#', [5,10,2,3,0,2], 'Hello!'),
             ('05<Nvv`\\.8_0qvh?b?', [59, 44, 50, 90, 86, 13, 13, 60, 64, 46, 70, 74, 45, 86, 5, 93, 15, 57], 'ThiS iS MixED cASe'),
             ("8fxvr6/C'9 _^[G4uXCN", [68, 85, 19, 19, 9, 52, 34, 35, 67, 48, 30, 76, 74, 33, 40, 24, 87, 38, 26, 46], 'Special Chars:~{}2) '),
             ('m]+h{eF&p.f\\eT]?Xb;K!{5', [25, 84, 33, 84, 91, 92, 68, 18, 80, 33, 86, 77, 93, 78, 74, 31, 68, 89, 49, 68, 12, 8, 18], 'This has longer shifts#'))
     for enc,pad,exp in pds:
         enc_msg = student.EncryptedMessage(enc)
         result = enc_msg.decrypt_message(pad)
         self.assertNotEqual(exp, enc_msg.apply_pad(pad), "Decrypting a message should negate the pad, not just apply it!")
         self.assertEqual(exp, result, "The decrypted message returned was {} but {} was expected".format(result, exp))
    def setUp(self):
        self.text1 = 'testing message'
        self.text2 = 'A message with punctuation in it... Fun!'
        self.text3 = 'zzzzz....'

        self.encrypt1 = 'wgvvlpj pgvudih'
        self.encrypt2 = 'B nhtvbjf xluk svqdwvdulpq lo jw... Gxo!'
        self.encrypt3 = 'azaza....'

        # do this so "loading words" doesn't print out every time
        with redirect_stdout(open(os.devnull, "w")):
            self.msg1 = student.Message(self.text1)
            self.msg2 = student.Message(self.text2)
            self.msg3 = student.Message(self.text3)

            self.ptmsg1 = student.PlaintextMessage(self.text1, [3, 2])  #1
            self.ptmsg2 = student.PlaintextMessage(self.text2, [1, 3])  #2
            self.ptmsg3 = student.PlaintextMessage(self.text3, [1, 0])

            self.ctmsg1 = student.EncryptedMessage(self.encrypt1)
            self.ctmsg2 = student.EncryptedMessage(self.encrypt2)
            self.ctmsg3 = student.EncryptedMessage(self.encrypt3)
Exemple #5
0
    def setUp(self):
        self.text1 = 'testing message'
        self.text2 = 'A message with punctuation in it... Fun!'
        self.text3 = 'zzzzz....'
        self.text4 = 'he,llo'
        self.text5 = 'My PIN is 12345 so secure!'
        self.text6 = 'H3LL0 W0RLD'

        self.encrypt1 = 'whvwlqj phvvdjh'
        self.encrypt2 = 'B nfttbhf xjui qvoduvbujpo jo ju... Gvo!'
        self.encrypt3 = 'bbbbb....'
        self.encrypt4 = 'he,llo'
        self.encrypt5 = 'Xj ATY td 23456 dz dpnfcp!'
        self.encrypt6 = 'K6OO3 Z3UOG'

        # do this so "loading words" doesn't print out every time
        with redirect_stdout(open(os.devnull, "w")):
            self.msg1 = student.Message(self.text1)
            self.msg2 = student.Message(self.text2)
            self.msg3 = student.Message(self.text3)
            self.msg4 = student.Message(self.text4)
            self.msg5 = student.Message(self.text5)
            self.msg6 = student.Message(self.text6)

            self.ptmsg1 = student.PlaintextMessage(self.text1, 3) #1
            self.ptmsg2 = student.PlaintextMessage(self.text2, 1) #2
            self.ptmsg3 = student.PlaintextMessage(self.text3, 2)
            self.ptmsg4 = student.PlaintextMessage(self.text4, 0)
            self.ptmsg5 = student.PlaintextMessage(self.text5, 11)
            self.ptmsg6 = student.PlaintextMessage(self.text6, 3)

            self.ctmsg1 = student.EncryptedMessage(self.encrypt1)
            self.ctmsg2 = student.EncryptedMessage(self.encrypt2)
            self.ctmsg3 = student.EncryptedMessage(self.encrypt3)
            self.ctmsg4 = student.EncryptedMessage(self.encrypt4)
            self.ctmsg5 = student.EncryptedMessage(self.encrypt5)
            self.ctmsg6 = student.EncryptedMessage(self.encrypt6)