コード例 #1
0
ファイル: chat.py プロジェクト: ivanpankov365/ChatNum
def main(m, n, filenames):
    files = [open(filename, 'w') for filename in filenames]

    config.set_default_curve()

    alice_privkey = keys.UmbralPrivateKey.gen_key()
    alice_pubkey = alice_privkey.get_pubkey()

    bob_privkey = keys.UmbralPrivateKey.gen_key()
    bob_pubkey = bob_privkey.get_pubkey()

    mock_kms = MockNetwork()

    sys.stderr.write('Server with PID %s is ready to pipe messages.\n' % os.getpid())

    for line in sys.stdin:
        ciphertext, capsule = pre.encrypt(alice_pubkey, line.rstrip('\n').encode('utf8'))

        alice_kfrags = pre.split_rekey(alice_privkey, bob_pubkey, m, n)

        policy_id = mock_kms.grant(alice_kfrags)

        bob_cfrags = mock_kms.reencrypt(policy_id, capsule, m)

        bob_capsule = capsule
        for cfrag in bob_cfrags:
            bob_capsule.attach_cfrag(cfrag)

        decrypted = pre.decrypt(ciphertext, bob_capsule, bob_privkey, alice_pubkey)

        for file in files:
            file.write('%s\n' % decrypted.decode('utf8'))
            file.flush()

        mock_kms.revoke(policy_id)
コード例 #2
0
policy_id = mock_kms.grant(alice_kfrags)
assert type(policy_id) == str

# 5. Perform re-encryption request
bob_capsule = capsule
bob_capsule.set_correctness_keys(alice_pubkey, bob_pubkey, alice_signing_pubkey)
bob_cfrags = mock_kms.reencrypt(policy_id, bob_capsule, 10)
assert len(bob_cfrags) == 10

# 6. Simulate capsule handoff, and set the correctness keys.
#  Correctness keys are used to prove that a cfrag is correct and not modified
#  by a proxy node in the network. They must be set to use the `decrypt` and
#  `attach_cfrag` funtions.
for cfrag in bob_cfrags:
    bob_capsule.attach_cfrag(cfrag)

decrypted_data = pre.decrypt(ciphertext, bob_capsule, bob_privkey, alice_signing_pubkey)
assert decrypted_data == plaintext


# 7. Perform revoke request
mock_kms.revoke(policy_id)


# 8. This should throw a `ValueError`.
try:
    mock_kms.reencrypt(policy_id, capsule, 10)
except ValueError:
    print("An Error was thrown indicating the expected response. Tests have run without problem.")