def test_encrypt_cbc(self): if "cbc" not in self.tests: self.skipTest("") test = b"1234567890" expected = b"8vSCtvqVqJ1nFiDO39gBfg==" key = b"YELLOW SUBMARINE" actual = base64.b64encode(encrypt_cbc(test, key)) self.assertEqual(expected, actual)
def encryption_oracle_ecb_cbc(data: bytes, is_ecb: bool) -> bytes: key = generate_bytes() before = generate_bytes(randint(5, 10)) after = generate_bytes(randint(5, 10)) data = before + data + after data = pkcs7_pad(data) if is_ecb: output = encrypt_aes_128_ecb(data, key) else: iv = generate_bytes() output = encrypt_cbc(data, key, iv) return output
def test_cbc(self): if "cbc" not in self.tests: self.skipTest("") data = b"1234567890" key = b"YELLOW SUBMARINE" iv = 16 * b"\0" cipher = AES.new(key, AES.MODE_CBC, iv) expected = cipher.encrypt(pkcs7_pad(data, AES.block_size)) actual = encrypt_cbc(data, key, iv) self.assertEqual(expected, actual) cipher = AES.new(key, AES.MODE_CBC, iv) expected = strip_padding(cipher.decrypt(expected)) actual = decrypt_cbc(actual, key, iv) self.assertEqual(expected, actual) data = b"1234567890abcdef" cipher = AES.new(key, AES.MODE_CBC, iv) expected = cipher.encrypt(data) actual = encrypt_cbc(data, key, iv) self.assertEqual(expected, actual)
def encrypt_random_string(bs: int = 16, teststring: str = None) -> bytes: iv = generate_bytes() # noinspection PyTypeChecker strings: List[Base64] = ["MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=", "MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic=", "MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw==", "MDAwMDAzQ29va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg==", "MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl", "MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA==", "MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw==", "MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8=", "MDAwMDA4b2xsaW4nIGluIG15IGZpdmUgcG9pbnQgb2g=", "MDAwMDA5aXRoIG15IHJhZy10b3AgZG93biBzbyBteSBoYWlyIGNhbiBibG93"] rand_string = from_base64(choice(strings)) if teststring: rand_string = teststring return iv + encrypt_cbc(rand_string, fixkey, iv, bs)
def encrypt_cbc_fix(data: bytes) -> bytes: global key global iv return encrypt_cbc(data, key, iv)