def __init__(self): self.__private_key = RSA.generate_private_key() self.public_key = RSA.public_key(self.__private_key) self.users_info = [] self.vendors_info = [] self.c = 3 # 2 to 10 self.M = 16 # 5 to 30
def new_keys(bit_length: int): public, private, primes = RSA.choose_prime_numbers(bit_length // 2) e = public[0] n = public[1] d = private[0] p = primes[0] q = primes[1] return RsaData(n, e, d, p, q)
def send_session_key_to(self, instance): print('\n++++++ SESSION ACKNOWLEDGE START ++++++++') self.session_key = Fernet.generate_key() encrypted_session_key = RSA.encrypt(instance.public_key, self.session_key) print('[VENDOR] Sending RSA encrypted session_key:', encrypted_session_key) instance.reveive_session_key_from_vendor(encrypted_session_key)
def receive_session_key(self, message): print('[VENDOR] Received RSA encrypted session_key. Decoding...') session_key = RSA.decrypt(self.__private_key, message) print('[VENDOR] Session key decoded:', session_key) self.__users.append(Vendor.User_Info(session_key)) print('++++++ SESSION ACKNOWLEDGE END ++++++++\n')
def __init__(self): self.__users = [] self.__transactions = [] self.__private_key = RSA.generate_private_key() self.public_key = RSA.public_key(self.__private_key)
# Standard from datetime import datetime import jsonpickle from secrets import choice from random import random ## SCENARIO 4 # Players bank = Bank() vendor = Vendor() user = User() symmetric_key = Fernet.generate_key() user.set_session_key(symmetric_key) user.send_session_key_to(bank, RSA.encrypt(bank.public_key, symmetric_key)) coins = 7 user_credits_message = jsonpickle.encode({ 'Coins' : coins }).encode('utf-8') user_encrypted_message = Fernet(user.session_key).encrypt(user_credits_message) response = user.send_message_to(bank, user_encrypted_message) print('[MAIN] Response:', response) response = Fernet(user.session_key).decrypt(response) response = response.decode('utf-8') if 'success' in response:
import json from blockchain import BlockChain from block import Block from flask import Flask, request, Response import requests from utils import RSA # RSA工具 rsa_tool = RSA() rsa_tool.create_keys() # 节点集合 peers = set() # 区块链对象,保存着所有区块 local_chain = BlockChain() # 消息广播 def broadcast(blocks): for peer in peers: if peer == local_server: continue chain_json = __block_list_to_json(blocks) r = requests.post('http://' + peer + '/chain', data={'chain': chain_json}) print("local chan broadcast OK") sync_peers() # 网络节点同步,把本地节点集合广播给所有节点 def sync_peers(): # post net node to each peer
def reveive_session_key_from_vendor(self, message): print('[BANK] Received RSA encrypted session_key. Decoding...') session_key = RSA.decrypt(self.__private_key, message) print('[BANK] Session key decoded:', session_key) self.vendors_info.append(session_key) print('++++++ SESSION ACKNOWLEDGE END ++++++++\n')