Exemple #1
0
 def test_s1c03_solveS1C3(self):
     '''
     You might find that it's less clear what the testable contract of the methods
     that just solve the challenges is. If you choose, you can decide not to use
     this test method, and instead just test the helper methods you write to make
     solveSXCY() methods work.
     '''
     testCases = [
         (b'hello world!', b'c4'),
         (b'It was a bright cold day in April, and the clocks were striking thirteen.',
          b'55'),
         (b'It"s no use going back to yesterday, because I was a different person then.',
          b'a2'),
         (b'Who in the world am I?!?!?! Ah, that"s the great puzzle! :D',
          b'07'),
         (b'"She said that the homework #123#345#456S --- IDK WHAT --- is due on monday!!!":::::',
          b'73')
     ]
     for message, key in testCases:
         encrypted = binToHex(caesarEncrypt(message, hexToBin(key)))
         self.assertEqual(type(encrypted), bytes)
         decrypted = solveS1C03(hexToBin(encrypted))[0]
         self.assertEqual(decrypted, message)
     # from cryptopals
     # result = b"Cooking MC's like a pound of bacon"
     testString = b'1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736'
     print(solveS1C03(hexToBin(testString))[0])
Exemple #2
0
 def test_s1c05_vigenereEncrypt(self):
     testCases = [(
         b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal",
         b'ICE',
         b"0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f"
     )]
     for message, key, cipher in testCases:
         message = bytes("".join("{:02x}".format(c) for c in message),
                         'utf-8')
         key = bytes("".join("{:02x}".format(c) for c in key), 'utf-8')
         encrypted = binToHex(
             vigenereEncrypt(hexToBin(message), hexToBin(key)))
         self.assertEqual(cipher, encrypted)
Exemple #3
0
 def test_s1c03_caesarDecrypt(self):
     testCases = [(b'c3e187a54b690f2d', b'0123456789abcdef', b'C2'),
                  (b'5c7e183ad4f690b2', b'0123456789abcdef', b'5D'),
                  (b'', b'', b'AF')]
     for cipher, message, key in testCases:
         decrypted = binToHex(caesarDecrypt(hexToBin(cipher),
                                            hexToBin(key)))
         self.assertEqual(decrypted, message)
         self.assertEqual(type(decrypted), bytes)
         self.assertEqual(len(message), len(decrypted))
         #test that can decrypt with all possible hex bytes
         for i in range(256):
             k = bytes(hex(i)[2:].zfill(2), 'utf-8')
             decrypted = caesarDecrypt(hexToBin(cipher), hexToBin(k))
Exemple #4
0
 def test_s1c03_caesarEncrypt(self):
     testCases = [(b'0123456789ABCDEF', b'c3e187a54b690f2d', b'C2'),
                  (b'0123456789ABCDEF', b'5c7e183ad4f690b2', b'5D'),
                  (b'', b'', b'AF')]
     for hexString, cipher, key in testCases:
         encrypted = binToHex(
             caesarEncrypt(hexToBin(hexString), hexToBin(key)))
         self.assertEqual(encrypted, cipher)
         self.assertEqual(type(encrypted), bytes)
         self.assertEqual(len(hexString), len(cipher))
         #test that can encrypt with all possible hex bytes
         for i in range(256):
             k = bytes(hex(i)[2:].zfill(2), 'utf-8')
             encrypted = caesarEncrypt(hexToBin(hexString), hexToBin(k))
Exemple #5
0
    def test_s1c02_xor(self):
        '''
        Starting here, write your own tests. You can generate tests in a variety
        of ways, ranging from trivial tests (does the empty string produce the empty
        string?) to tests of properties (does xor preserve length?) to tests of types
        (is the return type of this method correct?) to specific cases (is the xor
        of these two specific byte strings equal to the correct byte string?).

        To generate specific cases, I'd suggest either using very small examples 
        you can work by hand, or generate the test cases using other code you write
        or online calculators.
        '''
        testCases = [(b'1c0111001f010100061a024b53535009181c',
                      b'686974207468652062756c6c277320657965',
                      b'746865206b696420646f6e277420706c6179'),
                     (b'', b'', b''), (b'1234', b'5678', b'444c')]
        for hexString1, hexString2, result in testCases:
            xorred = binToHex(xor(hexToBin(hexString1), hexToBin(hexString2)))
            self.assertEqual(xorred, result)
            self.assertEqual(type(xorred), bytes)