def test_old_keys(self): alice = ECC() curve, px, py, i = ECC._old_decode_pubkey(alice._old_get_pubkey()) curve2, pv, i = ECC._old_decode_privkey(alice._old_get_privkey()) self.assertEqual(curve, curve2) alice2 = ECC(curve=curve, pubkey_x=px, pubkey_y=py, raw_privkey=pv) self.assertEqual(alice2.get_pubkey(), alice.get_pubkey()) self.assertEqual(alice2.get_privkey(), alice.get_privkey())
def test_ecies(self): print("\nTEST: ECIES") alice = ECC() plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ciphertext = alice.encrypt(plaintext, alice.get_pubkey()) print(hexlify(ciphertext)) self.assertEqual(plaintext, alice.decrypt(ciphertext))
class PasswordCrypto: """ Small wrapper around pyelliptic to perform assymetric encryption of passwords. """ def __init__(self, pubkey): self.pubkey = b64decode(pubkey) self.actor = ECC(pubkey=self.pubkey) def encrypt(self, pwd): return b64encode(self.actor.encrypt(pwd, self.actor.get_pubkey()))
else: self._transport.log("Order not for us") if msg.get('id'): if msg['id'] in self._orders: self._orders[msg['id']]['state'] = msg['state'] else: self._orders[msg['id']] = msg # Test Code if run directly if __name__ == '__main__': seller = ECC(curve='secp256k1') class FakeTransport(): _myself = ECC(curve='secp256k1') def add_callback(self, section, cb): pass def send(self, msg, to=None): print 'sending', msg def log(self, msg): print msg transport = FakeTransport() rep = Orders(transport) rep.on_order( order(None, transport._myself.get_pubkey(), seller.get_pubkey(), 'new', 'One!', ["dsasd", "deadbeef"]))
self._transport.log("Order not for us") elif state == 'received': if myself == seller: pass # ok elif myself == buyer: self.receive_order(msg) else: self._transport.log("Order not for us") if msg.get('id'): if msg['id'] in self._orders: self._orders[msg['id']]['state'] = msg['state'] else: self._orders[msg['id']] = msg if __name__ == '__main__': seller = ECC(curve='secp256k1') class FakeTransport(): _myself = ECC(curve='secp256k1') def add_callback(self, section, cb): pass def send(self, msg, to=None): print 'sending', msg def log(self, msg): print msg transport = FakeTransport() rep = Orders(transport) rep.on_order(order(None, transport._myself.get_pubkey(), seller.get_pubkey(), 'new', 'One!', ["dsasd", "deadbeef"]))
ciphername = "aes-256-cbc" iv_hex = b"000102030405060708090A0B0C0D0E0F" iv = unhexlify(iv_hex) key_hex = b"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4" key = unhexlify(key_hex) plain_hex = b"6bc1bee22e409f96e93d7e117393172a" plaintext = unhexlify(plain_hex) ctx = Cipher(key, iv, 1, ciphername=ciphername) enc = ctx.ciphering(plaintext) print(hexlify(enc)) ctx = Cipher(key, iv, 0, ciphername=ciphername) assert ctx.ciphering(enc) == plaintext print("\nTEST: ECIES") alice = ECC() plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ciphertext = ECC.encrypt(plaintext, alice.get_pubkey()) print(hexlify(ciphertext)) assert alice.decrypt(ciphertext) == plaintext print("\nTEST: ECIES/RC4") alice = ECC() plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ciphertext = ECC.encrypt(plaintext, alice.get_pubkey(), ciphername="rc4") print(hexlify(ciphertext)) assert alice.decrypt(ciphertext, ciphername="rc4") == plaintext
print("\nTEST: AES-256-CBC") ciphername = "aes-256-cbc" iv_hex = b"000102030405060708090A0B0C0D0E0F" iv = unhexlify(iv_hex) key_hex = b"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4" key = unhexlify(key_hex) plain_hex = b"6bc1bee22e409f96e93d7e117393172a" plaintext = unhexlify(plain_hex) ctx = Cipher(key, iv, 1, ciphername=ciphername) enc = ctx.ciphering(plaintext) print(hexlify(enc)) ctx = Cipher(key, iv, 0, ciphername=ciphername) assert ctx.ciphering(enc) == plaintext print("\nTEST: ECIES") alice = ECC() plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ciphertext = ECC.encrypt(plaintext, alice.get_pubkey()) print(hexlify(ciphertext)) assert alice.decrypt(ciphertext) == plaintext print("\nTEST: ECIES/RC4") alice = ECC() plaintext = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ciphertext = ECC.encrypt(plaintext, alice.get_pubkey(), ciphername="rc4") print(hexlify(ciphertext)) assert alice.decrypt(ciphertext, ciphername="rc4") == plaintext
if (len(argv) != 2): print('Error: No mixnet size given.\n\nUsage:\n{} mixnet_size'.format(argv[0])) exit(1) CONFIG_FILENAME = 'mixnets.conf' MIXNET_SIZE = int(argv[1]) BASE_PORT_WEB = 8000 BASE_PORT_P2P = 10000 MIXNET_NAME = 'sample_mixnet' mixnet_config = {'global_config' : {'testnet' : 'True'}, 'mixing_peers' : {}, 'mixing_networks' : {MIXNET_NAME : {}}} for mp in xrange(MIXNET_SIZE): mp_id = 'mp{0:02d}'.format(mp) mp_rank = mp mp_crypter = ECC(curve='secp256k1') mp_pubkey = mp_crypter.get_pubkey().encode('hex') mp_privkey = mp_crypter.get_privkey().encode('hex') mixnet_config['mixing_peers'][mp_id] = {'web_addr' : '{}.cp:{}'.format(mp_id, BASE_PORT_WEB + mp_rank), 'pubkey' : mp_pubkey, 'prvkey' : mp_privkey} mixnet_config['mixing_networks'][MIXNET_NAME][mp_id] = {'rank' : mp_rank, 'p2p_addr' : 'localhost:{}'.format(BASE_PORT_P2P + mp_rank)} conf = ConfigObj() for k in mixnet_config.keys(): conf[k] = mixnet_config[k] conf.filename = CONFIG_FILENAME conf.write()
if myself == seller: pass # ok elif myself == buyer: self.receive_order(msg) else: self._transport.log("Order not for us") # Store order if msg.get('id'): if self._orders.find( {id:msg['id']}): self._orders.update({'id':msg['id']}, { "$set": { 'state':msg['state'] } }, True) else: self._orders.update({'id':msg['id']}, { "$set": { msg } }, True) if __name__ == '__main__': seller = ECC(curve='secp256k1') class FakeTransport(): _myself = ECC(curve='secp256k1') def add_callback(self, section, cb): pass def send(self, msg, to=None): print 'sending', msg def log(self, msg): print msg transport = FakeTransport() rep = Orders(transport) rep.on_order(order(None, transport._myself.get_pubkey(), seller.get_pubkey(), 'new', 'One!', ["dsasd", "deadbeef"]))