def testBoxing(): msg = b'Hey there, a msg for you' # Generate the key pairs for Alice and bob, if secret keys already exist # they can be passed in, otherwise new keys will be automatically generated bob = SecretKey() alice = SecretKey() """ Alice: aA (a is alices private key, A is Alice's public key) A = G*a Bob: bB B = G*b hash(a*B) == hash(b*A) : hypothesis hash(a*G*b) == hash(b*G*a) : substitution hash(G*a*b) == hash(G*a*b) : commutative property of ECC math True! """ # Create the boxes, this is an object which represents the combination of the # sender's secret key and the receiver's public key bob_box = Box(bob.sk, alice.pk) alice_box = Box(alice.sk, bob.pk) # Bob's box encrypts messages for Alice bob_ctxt = bob_box.encrypt(msg) # Alice's box decrypts messages from Bob bclear = alice_box.decrypt(bob_ctxt) # Alice can send encrypted messages which only Bob can decrypt alice_ctxt = alice_box.encrypt(msg) aclear = bob_box.decrypt(alice_ctxt) print(bob.for_json()) print("bob's public key" + bob.hex_pk().hex()) print("bob's secret key" + bob.hex_sk().hex())
def testBoxing(): msg = b'Hey there, a msg for you' # Generate the key pairs for Alice and bob, if secret keys already exist # they can be passed in, otherwise new keys will be automatically generated bob = SecretKey() alice = SecretKey() """ Alice: aA (a is alices private key, A is Alice's public key) A = G*a Bob: bB B = G*b hash(a*B) == hash(b*A) : hypothesis hash(a*G*b) == hash(b*G*a) : substitution hash(G*a*b) == hash(G*a*b) : commutative property of ECC math True! """ # Create the boxes, this is an object which represents the combination of the # sender's secret key and the receiver's public key bob_box = Box(bob.sk, alice.pk) alice_box = Box(alice.sk, bob.pk) # Bob's box encrypts messages for Alice bob_ctxt = bob_box.encrypt(msg) # Alice's box decrypts messages from Bob bclear = alice_box.decrypt(bob_ctxt) # Alice can send encrypted messages which only Bob can decrypt alice_ctxt = alice_box.encrypt(msg) aclear = bob_box.decrypt(alice_ctxt) print(bob.for_json()) print("bob's public key" + bob.hex_pk().hex()) print("bob's secret key" + bob.hex_sk().hex())
class CryptoBox(): def __init__(self, keyobj): self.keyobj = keyobj self.box = None def box_with(self, peer_pk): # create a box with peer_pk (in pk bin format) self.box = Box(self.keyobj.sk, peer_pk) def encrypt(self, msg): return self.box.encrypt(msg) def decrypt(self, msg): return self.box.decrypt(msg)
def test_dump_resource(self, mock_crypto_box_keypair): request = MagicMock() request.registry.arch_pubkey = 'c' * 32 mock_crypto_box_keypair.return_value = ["a" * 32, "b" * 32] context = { 'id': uuid.uuid4().hex, 'rev': '1-{}'.format(uuid.uuid4().hex), 'dateModified': datetime.now().isoformat(), 'doc_type': 'Tenders' } request.context.serialize.return_value = context dump = dump_resource(request) res, key = dump['item'], dump['pubkey'] decrypt_box = Box("b" * 32, "c" * 32) decrypted_data = decrypt_box.decrypt(b64decode(res)) decrypted_data = json.loads(decrypted_data) self.assertNotEqual(res, json.dumps(context)) self.assertEqual(decrypted_data, context)