def main(target, prv_key, pass_phrase): aes_util = AESUtil() rsa_util = RSAUtil() with open(prv_key) as f: s = f.read() pkey = binascii.unhexlify(s) rsa_util.import_prv_key(pkey, pass_phrase) with open(target) as f2: s = f2.read() target_data = json.loads(s) p_version = target_data["version"] file_name = target_data["file_name"] c_key = target_data["content_key"] e_content = target_data["content"] sender_pub_key = target_data["sender"] encrypted_key = base64.b64decode(binascii.unhexlify(c_key)) decrypted_key = rsa_util.decrypt_with_private_key(encrypted_key) enc_data = base64.b64decode(binascii.unhexlify(e_content)) content = aes_util.decrypt_with_key(enc_data, decrypted_key) signature = target_data["signature"] target_strings = p_version + file_name + c_key + e_content + sender_pub_key sender_pubkey = RSA.importKey(binascii.unhexlify(sender_pub_key)) v_result = rsa_util.verify_signature(target_strings, signature, sender_pubkey) if(v_result): with open(file_name, mode='wb') as f3: f3.write(content)
def __init__(self, my_port=50082, core_node_host=None, core_node_port=None, pass_phrase=None): self.server_state = STATE_INIT print('Initializing server...') self.my_ip = self.__get_myip() print('Server IP address is set to ... ', self.my_ip) self.my_port = my_port self.cm = ConnectionManager(self.my_ip, self.my_port, self.__handle_message) self.mpmh = MyProtocolMessageHandler() self.core_node_host = core_node_host self.core_node_port = core_node_port self.bb = BlockBuilder() my_genesis_block = self.bb.generate_genesis_block() self.bm = BlockchainManager(my_genesis_block.to_dict()) self.prev_block_hash = self.bm.get_hash(my_genesis_block.to_dict()) self.tp = TransactionPool() self.is_bb_running = False self.flag_stop_block_build = False self.mpm_store = MessageStore() self.km = KeyManager(None, pass_phrase) self.rsa_util = RSAUtil() self.um = UTXOManager(self.km.my_address())
def initApp(self, my_port, c_host, c_port): """ ClientCoreとの接続含めて必要な初期化処理はここで実行する """ print('SimpleBitcoin client is now activating ...: ') self.km = KeyManager() self.um = UTXM(self.km.my_address()) self.rsa_util = RSAUtil() self.c_core = Core(my_port, c_host, c_port, self.update_callback) self.c_core.start() # テスト用途(本来はこんな処理しない) t1 = CoinbaseTransaction(self.km.my_address()) t2 = CoinbaseTransaction(self.km.my_address()) t3 = CoinbaseTransaction(self.km.my_address()) transactions = [] transactions.append(t1.to_dict()) transactions.append(t2.to_dict()) transactions.append(t3.to_dict()) self.um.extract_utxos(transactions) self.update_balance()
def main(target_file_name, path_to_rsa_pub_key, result_file_name, prv_key, pass_phrase): p_version = "0.1.0" aes_util = AESUtil() with open(target_file_name, 'rb') as f: target = f.read() enc_content = aes_util.encrypt(target) content_key = aes_util.get_aes_key() with open(path_to_rsa_pub_key, 'r') as f2: pkey_data = f2.read() with open(prv_key, 'r') as f3: s = f3.read() my_prvkey = binascii.unhexlify(s) rsa_util = RSAUtil() rsa_util.import_prv_key(my_prvkey, pass_phrase) enc_key = rsa_util.encrypt_with_pubkey(content_key, pkey_data) result = {} result["version"] = p_version result["file_name"] = target_file_name content_key_txt = binascii.hexlify( base64.b64encode(enc_key)).decode('ascii') result["content_key"] = content_key_txt content_txt = binascii.hexlify( base64.b64encode(enc_content)).decode('ascii') result["content"] = content_txt sender_pub_key = binascii.hexlify(rsa_util.get_my_pubkey()).decode('ascii') result["sender"] = sender_pub_key signature = rsa_util.compute_digital_signature(p_version + target_file_name + content_key_txt + content_txt + sender_pub_key) result["signature"] = signature with open(result_file_name, 'w') as f4: json.dump(result, f4, indent=4)
def main(target, prv_key, pass_phrase): aes_util = AESUtil() rsa_util = RSAUtil() with open(prv_key) as f: s = f.read() pkey = binascii.unhexlify(s) rsa_util.import_prv_key(pkey, pass_phrase) with open(target) as f2: s = f2.read() target_data = json.loads(s) encrypted_key = base64.b64decode(binascii.unhexlify(target_data["content_key"])) decrypted_key = rsa_util.decrypt_with_private_key(encrypted_key) enc_data = base64.b64decode(binascii.unhexlify(target_data["content"])) content = aes_util.decrypt_with_key(enc_data, decrypted_key) result_file_name = target_data["file_name"] with open(result_file_name, mode='wb') as f3: f3.write(content)