def sbytexor(s): bestKey = {"xorbyte": bytes(0), "score": 0, "msgResult": ""} for byte in range(0, 255): currentKey = {"xorbyte": bytes(0), "score": 0, "msgResult": ""} xored = xor(s, len(s) * [byte]) currentKey['xorbyte'] = byte currentKey['msgResult'] = xor(s, len(s) * [currentKey['xorbyte']]) currentKey['score'] = int(enscore(xored)) if bestKey['score'] < currentKey['score']: bestKey = currentKey return bestKey
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'01', b'00', b'01'), (b'', b'', b'') ] for input1, input2, XORresult in testCases: self.assertEqual(len(input1), len(XORresult)) self.assertEqual(len(input2), len(XORresult)) self.assertEqual(xor(input1,input2), XORresult)
def aes_decrypt_cbc(txt, key, iv): plaintext = bytearray() blocks = breaktoblocks(txt, 16) prev_block = iv for block in blocks: currentplaintext = xor(aes_decrypt_ecb(block, key), prev_block) prev_block = block plaintext += currentplaintext return bytes(plaintext)
def aes_encrypt_cbc(txt, key, iv): ciphers = bytearray() blocks = breaktoblocks(pkcs7pad(txt, 16), 16) prev_block = iv for block in blocks: currentcipher = aes_encrypt_ecb(xor(block, prev_block), key) prev_block = currentcipher ciphers += currentcipher return bytes(ciphers)
def caesarDecrypt(cipher, key): elongatedKey = createElongatedKey(key, len(cipher)) return s1c02.xor(cipher, elongatedKey)
def caesarEncrypt(message, key): elongatedKey = createElongatedKey(key, len(message)) return s1c02.xor(message, elongatedKey)
def repkeyxor(s, k): return xor(s, k * len(s))
def caesarDecrypt(key, ciphertext): resultarray = [] for i in range(len(ciphertext)): resultarray.append(key) result = b"".join(resultarray) return xor(ciphertext, result)
def caesarEncrypt(key, plaintext): resultarray = [] for i in range(len(plaintext)): resultarray.append(key) result = b"".join(resultarray) return xor(plaintext, result)
def caesarDecrypt(message, key): return xor(message, key)
def hd(s1, s2): return "".join([bin(l) for l in xor(s1, s2)]).count("1")