def encrypt_msg(self, msg: bytes) -> Tuple[bytes, bytes]: key = sha1( self._session_key.to_bytes( floor(self._session_key.bit_length() / 8) + 1, "big"))[:16] iv = gen_random_bytes(16) cbc = AESCipher(AESCipher.MODE_CBC, key, iv=iv) return (iv, cbc.encrypt(pkcs7_pad(msg)))
def _encryption_oracle(bytes_: bytes) -> Tuple[bytes, str]: key = gen_random_bytes(16) iv = gen_random_bytes(16) prefix = gen_random_bytes(random.randint(5, 10)) suffix = gen_random_bytes(random.randint(5, 10)) pt = prefix + bytes_ + suffix cbc_mode = random.choice([True, False]) if cbc_mode: cbc = AESCipher(AESCipher.MODE_CBC, key, iv=iv) ct = cbc.encrypt(pkcs7_pad(pt)) answer = "cbc" else: ecb = AESCipher(AESCipher.MODE_ECB, key) ct = ecb.encrypt(pkcs7_pad(pt)) answer = "ecb" return ct, answer
def _get_token(user_data: str) -> bytes: prefix = "comment1=cooking%20MCs;userdata=" suffix = ";comment2=%20like%20a%20pound%20of%20bacon" token = prefix + quote(user_data) + suffix return _ctr.encrypt(pkcs7_pad(token.encode("latin")))
def _encrypt_it(bytes_: bytes) -> bytes: pt = bytes_ + _SECRET ct = _ecb.encrypt(pkcs7_pad(pt)) return ct
def _encrypt(pt: bytes) -> (bytes, bytes): iv = gen_random_bytes(16) cbc = AESCipher(AESCipher.MODE_CBC, _KEY, iv=iv) ct = cbc.encrypt(pkcs7_pad(pt)) return iv, ct
def challenge09(bytes_: bytes, block_size: int) -> bytes: return pkcs7_pad(bytes_, block_size)
def _oracle(email: str) -> bytes: cookie = bytes(_profile_for(email).encode("ascii")) encrypted_cookie = _ecb.encrypt(pkcs7_pad(cookie)) return encrypted_cookie
def test_pkcs7_padding_add_block(self): a = gen_random_bytes() b = pkcs7_pad(a) c = Padding.pad(a, 16) assert b == c, "The result does not match the expected value"