def test_elliptic(self): data = self.test_string k = generate_eth_key() sk_hex = k.to_hex() pk_hex = k.public_key.to_hex() self.assertEqual(data, decrypt(sk_hex, encrypt(pk_hex, data))) k = generate_key() sk_hex = k.to_hex() pk_hex = k.public_key.format(False).hex() self.assertEqual(data, decrypt(sk_hex, encrypt(pk_hex, data))) self.assertEqual( data, decrypt(bytes.fromhex(sk_hex), encrypt(bytes.fromhex(pk_hex), data))) k = generate_key() sk_hex = k.to_hex() pk_hex = k.public_key.format(True).hex() self.assertEqual(data, decrypt(sk_hex, encrypt(pk_hex, data))) self.assertEqual( data, decrypt(bytes.fromhex(sk_hex), encrypt(bytes.fromhex(pk_hex), data))) self.assertRaises(TypeError, encrypt, 1, data) self.assertRaises(TypeError, decrypt, 1, encrypt(bytes.fromhex(pk_hex), data))
def icies_test(self, sk_hex, pk_hex, data): # assymtetric encryption/decryption test print(decrypt(sk_hex, encrypt(pk_hex, data))) secp_k = generate_key() sk_bytes = secp_k.secret # bytes pk_bytes = secp_k.public_key.format(True) # bytes print(decrypt(sk_bytes, encrypt(pk_bytes, data)))
def test_elliptic(self): data = self.test_string k = generate_eth_key() prvhex = k.to_hex() pubhex = k.public_key.to_hex() self.assertEqual(data, decrypt(prvhex, encrypt(pubhex, data))) k = generate_key() prvhex = k.to_hex() pubhex = k.public_key.format(False).hex() self.assertEqual(data, decrypt(prvhex, encrypt(pubhex, data))) k = generate_key() prvhex = k.to_hex() pubhex = k.public_key.format(True).hex() self.assertEqual(data, decrypt(prvhex, encrypt(pubhex, data)))
def ies(ptxt): sk = generate_key() sk_bytes = sk.secret pk_bytes = sk.public_key.format(True) ctxt = encrypt(pk_bytes, ptxt.encode()) print("Ctxt:", ctxt) return decrypt(sk_bytes, ctxt)
def __init__(self, api_url, api_user, api_password): self.api_url = api_url self.api_user = api_user self.api_password = api_password self.key = generate_key() self.share_secret = '' self.token = ''
def encrypt_query(self, query: bytes): """ Encrypt the query to send to the server query - The query """ # Generate the ephemeral key ephemeral_key = generate_key() # Generate the symetric key print("Ephemeral: ", type(ephemeral_key)) print("Server PK: ", type(self.server_pk)) aes_key = encapsulate(ephemeral_key, self.server_pk) # Encrypt the query (this adds the nonce) cipher_text = aes_encrypt(aes_key, query) # Return the message and the symetric key return ephemeral_key.public_key.format(False) + cipher_text, aes_key
def encrypt(receiver_pubhex: str, msg: bytes) -> bytes: """ Encrypt with eth public key Parameters ---------- receiver_pubhex: str Receiver's ethereum public key hex string msg: bytes Data to encrypt Returns ------- bytes Encrypted data """ disposable_key = generate_key() receiver_pubkey = hex2pub(receiver_pubhex) aes_key = derive(disposable_key, receiver_pubkey) cipher_text = aes_encrypt(aes_key, msg) return disposable_key.public_key.format(False) + cipher_text
# account2 = web3.eth.accounts[2] # contract_address = "0xd558ae5fad9E835048Bb62e9545a830553B9C1bA" from ecies.utils import generate_eth_key, generate_key from ecies import encrypt, decrypt eth_k = generate_eth_key() print(eth_k) sk_hex = eth_k.to_hex() # hex string print(sk_hex) pk_hex = eth_k.public_key.to_hex() # hex string print(pk_hex) data = b'this is a test' encrypt(pk_hex, data) # print(decrypt(sk_hex, encrypt(pk_hex, data))) # b'this is a test' secp_k = generate_key() sk_bytes = secp_k.secret # bytes pk_bytes = secp_k.public_key.format(True) # bytes # decrypt(sk_bytes, encrypt(pk_bytes, data)) # b'this is a test' ''' 客户端功能 用户:发送方、接收方 5个步骤:存款、打开通道、转账、关闭通道、取款 存款:创建票据,用户界面接口Deposit(public_key, transfer_value) 计算新票据:系统自动生成随机数r,urcm = commit(public_key, transfer_value, random_r) 计算零知识证明:在linux系统中生成proof,其中public_input = (urcm, transfer_value), private_input = (public_key, random_r) groth16_prove(proving_key, circuit, input)==>proof.json + input.json groth16_verify(verfication_key, proof)==>true/false 发布交易:向混币合约转账transfer_value个以太币/代币,交易data
addr = r[:-1] registered.append(addr) addr2name[addr] = str(count) count += 1 f.close() if registration: # register some clients tx_hashes = [] nonce = w3.eth.getTransactionCount(myAddr) for i in range(client_num): # this address will be send to the operator by the client in the final # application, but for now we will just use one of the existing addresses addr = registered[i] # generate a key pair and store addr2keys[addr] = generate_key() pk = addr2keys[addr].public_key.format(True) # build transaction tx = darkPool.functions.register_client(addr, pk).buildTransaction({ 'from': myAddr, 'nonce': nonce + i, 'gas': 6721975, # from truffle docs 'gasPrice': 100000000000 # from truffle docs }) # sign transaction sign_tx = w3.eth.account.signTransaction(tx, myKey)
def generate_bitcoin_keys(): generated_key = generate_key() public_key = generated_key.public_key.format(False).hex() private_key = generated_key.to_hex() return public_key, private_key
keyPair = RSA.generate(bits=1024) msg = b'Test' hash = int.from_bytes(sha512(msg).digest(), byteorder='big') print("hash", hash) signature = pow(hash, keyPair.d, keyPair.n) print("signature : ", hex(signature)) hashFromSignature = pow(signature, keyPair.e, keyPair.n) print("hashFromSignature", hashFromSignature) ///////////// from ecies.utils import generate_key from ecies import encrypt, decrypt secret_key = generate_key() sk_bytes = secret_key.secret pk_bytes = secret_key.public_key.format(True) message = b"test" encrypted_message = encrypt(pk_bytes, message) print(encrypted_message) decrypted_message = decrypt(sk_bytes, encrypted_message) print(decrypted_message)
def get_keys(): secp_k = generate_key() prvkey_hex = secp_k.to_hex() pubkey_hex = secp_k.public_key.format(True).hex() return prvkey_hex, pubkey_hex