def get_item_list_from_contract(identity_acct: Account) -> list: sdk = app.config['ONTOLOGY'] if not isinstance(sdk, Ontology): return list() get_item_list_func = WasmInvokeFunction('get_item_list') get_item_list_func.set_params_value(identity_acct.get_address()) tx = sdk.wasm_vm.make_invoke_transaction( app.config['CONTRACT_ADDRESS_HEX'], get_item_list_func, identity_acct.get_address(), app.config['GAS_PRICE'], app.config['GAS_LIMIT']) tx.sign_transaction(identity_acct) response = sdk.rpc.send_raw_transaction_pre_exec(tx) if not isinstance(response, dict): return list() result = response.get('Result') if result is None: return list() builder = WasmParamsBuilder(bytes.fromhex(result)) struct_len = builder.read_var_uint() album_list = list() for _ in range(struct_len): try: encrypted_ipfs_address_bytes = bytes.fromhex(builder.pop_str()) ext = builder.pop_str() aes_iv = bytes.fromhex(builder.pop_str()) encode_g_tilde = bytes.fromhex(builder.pop_str()) ipfs_address = ECIES.decrypt_with_cbc_mode( encrypted_ipfs_address_bytes, identity_acct.get_private_key_bytes(), aes_iv, encode_g_tilde) album_list.append([ipfs_address.decode('ascii'), ext]) except Exception as e: print('Decrypt with cbc mode failed: ', e.args[0]) return album_list
def test_compatibility(self): hex_private_key = '9a31d585431ce0aa0aab1f0a432142e98a92afccb7bcbcaff53f758df82acdb3' hex_public_key = ECIES.get_public_key_by_hex_private_key( hex_private_key) self.assertEqual( '021401156f187ec23ce631a489c3fa17f292171009c6c3162ef642406d3d09c74d', hex_public_key) bytes_private_key = binascii.a2b_hex(hex_private_key) public_key_bytes = ECIES.get_public_key_by_bytes_private_key( bytes_private_key) msg = b'Attack!' aes_iv, encode_g_tilde, cipher_text = ECIES.encrypt_with_cbc_mode( msg, public_key_bytes) decrypt_msg = ECIES.decrypt_with_cbc_mode(cipher_text, bytes_private_key, aes_iv, encode_g_tilde) self.assertEqual(msg, decrypt_msg) nonce, mac_tag, encode_g_tilde, cipher_text = ECIES.encrypt_with_gcm_mode( msg, b'', public_key_bytes) self.assertRaises(SDKException, ECIES.encrypt_with_gcm_mode, msg, b'', '') self.assertRaises(SDKException, ECIES.encrypt_with_gcm_mode, msg, b'', b'') decrypt_msg = ECIES.decrypt_with_gcm_mode(nonce, mac_tag, cipher_text, bytes_private_key, b'', encode_g_tilde) self.assertEqual(msg, decrypt_msg)
def test_encrypt_with_cbc_mode(self): for _ in range(10): private_key_bytes = ECIES.generate_private_key() self.assertEqual(32, len(private_key_bytes)) public_key_bytes = ECIES.get_public_key_by_bytes_private_key( private_key_bytes) self.assertEqual(33, len(public_key_bytes)) msg = b'Attack!' aes_iv, encode_g_tilde, cipher_text = ECIES.encrypt_with_cbc_mode( msg, public_key_bytes) self.assertRaises(SDKException, ECIES.encrypt_with_cbc_mode, msg, '') self.assertRaises(SDKException, ECIES.encrypt_with_cbc_mode, msg, b'') decrypt_msg = ECIES.decrypt_with_cbc_mode(cipher_text, private_key_bytes, aes_iv, encode_g_tilde) self.assertRaises(SDKException, ECIES.decrypt_with_cbc_mode, cipher_text, b'', aes_iv, encode_g_tilde) self.assertRaises(SDKException, ECIES.decrypt_with_cbc_mode, cipher_text, '', aes_iv, encode_g_tilde) self.assertEqual(msg, decrypt_msg) cipher_text = b'\x00' * len(cipher_text) self.assertRaises(SDKException, ECIES.decrypt_with_cbc_mode, cipher_text, private_key_bytes, aes_iv, encode_g_tilde)
def test_java_sdk_case(self): hex_private_key = '9a31d585431ce0aa0aab1f0a432142e98a92afccb7bcbcaff53f758df82acdb3' private_key_bytes = bytes.fromhex(hex_private_key) msg = b'1234567890' iv = bytes.fromhex('f88305e04df4bdc491ca6ff65d222386') encode_g_tilde = bytes.fromhex('0480a0e5157874371c32cdb01e7e0938d155eaed8f50eecbc6d39b71685d5c69c' 'bb3f5b5c497d2e34ab47f105f85fc39edf0588d32b7e87403d65ff1e181800590') cipher_text_bytes = bytes.fromhex('8e7f8f37c16a712f7360c5eebcb0b01a') decrypt_msg = ECIES.decrypt_with_cbc_mode(cipher_text_bytes, private_key_bytes, iv, encode_g_tilde) self.assertEqual(msg, decrypt_msg)