def decrypt_with_cbc_mode(cipher_text: bytes, private_key: bytes, iv: bytes, encode_g_tilde: bytes) -> bytes: if not isinstance(private_key, bytes): raise SDKException( ErrorCode.other_error( 'the length of private key should be 32 bytes.')) if len(private_key) != 32: raise SDKException( ErrorCode.other_error( 'the length of private key should be 32 bytes.')) str_g_tilde_x = encode_g_tilde[1:33] str_g_tilde_y = encode_g_tilde[33:65] g_tilde_x = string_to_number(str_g_tilde_x) g_tilde_y = string_to_number(str_g_tilde_y) g_tilde = Point(NIST256p.curve, g_tilde_x, g_tilde_y, NIST256p.order) h_tilde = g_tilde * SigningKey.from_string( string=private_key, curve=NIST256p).privkey.secret_multiplier seed = b''.join( [encode_g_tilde, number_to_string(h_tilde.x(), NIST256p.order)]) aes_key = pbkdf2(seed, 32) try: plain_text = AESHandler.aes_cbc_decrypt(cipher_text, iv, aes_key) except ValueError as e: raise SDKException(ErrorCode.other_error(e.args[0])) return plain_text
def decrypt_with_cbc_mode(cipher_text: bytes, private_key: bytes, iv: bytes, encode_g_tilde: bytes) -> bytes: aes_key = ECIES.generate_decrypt_aes_key(private_key, encode_g_tilde) try: plain_text = AESHandler.aes_cbc_decrypt(cipher_text, iv, aes_key) except ValueError as e: raise SDKException(ErrorCode.other_error(e.args[0])) return plain_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)