def decrypt(private, ciphertext, output): """Decrypt ciphertext with private key. Requires PRIVATE key file and the CIPHERTEXT encrypted with the corresponding public key. """ privatekeydata = json.load(private) assert 'pub' in privatekeydata pub = load_public_key(privatekeydata['pub']) log("Loading private key") private_key_error = "Invalid private key" assert 'key_ops' in privatekeydata, private_key_error assert "decrypt" in privatekeydata['key_ops'], private_key_error assert 'p' in privatekeydata, private_key_error assert 'q' in privatekeydata, private_key_error assert privatekeydata['kty'] == 'DAJ', private_key_error _p = phe.util.base64_to_int(privatekeydata['p']) _q = phe.util.base64_to_int(privatekeydata['q']) private_key = phe.PaillierPrivateKey(pub, _p, _q) log("Decrypting ciphertext") enc = load_encrypted_number(ciphertext, pub) out = private_key.decrypt(enc) print(out, file=output)
def deserialize(b): seckey_dict = json.loads(b) sk_record = seckey_dict['secret_key'] sk = paillier.PaillierPrivateKey( public_key=paillier.PaillierPublicKey(n=int(sk_record['n'])), p=sk_record['p'], q=sk_record['q']) return SecretKey(sk)
def getKeys(): with open('custkeys.json', 'r') as file: keys = json.load(file) pub_key = paillier.PaillierPublicKey(n=int(keys['public_key']['n'])) priv_key = paillier.PaillierPrivateKey(pub_key, keys['private_key']['p'], keys['private_key']['q']) return pub_key, priv_key