def encrypt_with_cbc_mode(plain_text: bytes, public_key: bytes) -> (bytes, bytes, bytes): if not isinstance(public_key, bytes): raise BotException(BotError.invalid_public_key) if len(public_key) != 64: raise BotException(BotError.invalid_public_key) r = randint(1, SECP256k1.order) g_tilde = r * SECP256k1.generator h_tilde = r * VerifyingKey.from_string(string=public_key, curve=SECP256k1).pubkey.point str_g_tilde_x = number_to_string(g_tilde.x(), SECP256k1.order) str_g_tilde_y = number_to_string(g_tilde.y(), SECP256k1.order) encode_g_tilde = b''.join([b'\x04', str_g_tilde_x, str_g_tilde_y]) str_h_tilde_x = number_to_string(h_tilde.x(), SECP256k1.order) seed = b''.join([encode_g_tilde, str_h_tilde_x]) aes_key = pbkdf2(seed, 32) aes_iv, cipher_text = AESHandler.aes_cbc_encrypt(plain_text, aes_key) return aes_iv, encode_g_tilde, cipher_text
def test_aes_cbc(self): key = b'Sixteen byte key' plain_text = b'Attack at dawn' iv, cipher_text = AESHandler.aes_cbc_encrypt(plain_text, key) decrypt_out = AESHandler.aes_cbc_decrypt(cipher_text, iv, key) self.assertEqual(plain_text, decrypt_out)