I am doing a series of cyberexercises on the topic: cibersecurity. These exercises are supposed to be different ctf challenges from various difficulty levels. This challenge consist on a partial homomorphic cipher using python paillier library. The idea is that given some of the cipher data, the user must get the missing data knowing this:
- The known data are the results of various sums following the scheme xi + yi = xi + 1, where X are the known data.
- The flag is the list of all uncipher yi.
The problem is that in order to reproduce the challenge, the user must know the keys but the library lacks of importing and exporting keys methods, although you can generate the keys given n (public key, but g is autogenerated random) and public key, p, q to set the private key. The fact that g is random generated means I can not reproduce the challenge.
Library used: https://python-paillier.readthedocs.io/en/1.4.0/phe.html
I tried forcing n,g,p,q values by doing this:
from phe import paillier
#N,G,p,q are given by example.py key generation
pub = paillier.PaillierPublicKey(N)
pub.g = G
priv = paillier.PaillierPrivateKey(pub,p,q)
#ca,cb,cc are given by example.py a,b,c encrypted ciphertext values
a = paillier.EncryptedNumber(pub,ca)
b = paillier.EncryptedNumber(pub,cb)
c = paillier.EncryptedNumber(pub,cc)
print("A: ",priv.decrypt(a))
print("B: ",priv.decrypt(b))
sum = a._add_encrypted(b)
print("A + B: ",priv.decrypt(sum))
print("C: ",priv.decrypt(c))
total = sum._add_encrypted(c)
print("Total: ",priv.decrypt(total))
It seems to reproduce well the example.py data:
a = 200
b = 300
pub,priv = paillier.generate_paillier_keypair(n_length=256)
ca, cb = pub.encrypt(a), pub.encrypt(b)
print("A: ",a)
print("B: ",b)
cs = ca._add_encrypted(cb)
c = 70
cc = pub.encrypt(c)
new_cs = cs._add_encrypted(cc)
new_s = priv.decrypt(new_cs)
print("Result (Add):", new_s)
Result generated trying this solution:
A: 200
B: 300
A + B: 500
C: 70
Total: 570
I don't know if this approach is reliable enough for the challenge