Skip to content

Instantly share code, notes, and snippets.

@tuxxy
Last active October 31, 2019 03:16
Show Gist options
  • Save tuxxy/de72560094663a6ed7af41565010394c to your computer and use it in GitHub Desktop.
Save tuxxy/de72560094663a6ed7af41565010394c to your computer and use it in GitHub Desktop.
This is a demo of the multiplicative homomorphic qualities of unpadded RSA encryption.
#!/usr/bin/env python3
from Cryptodome.PublicKey import RSA
# A proof of concept demo showing the multiplicative limited
# homomorphic qualities of unpadded RSA
# (Unpadded RSA is not secure by modern standards)
def gen_key(size):
key = RSA.generate(size)
return key
def encrypt_integer(integer, key):
return pow(integer, key.e, key.n)
def decrypt_integer(integer, key):
return pow(integer, key.d, key.n)
def homomorphise(x1, x2, key):
return (x1*x2) % key.n
if __name__ == '__main__':
print("Generating key...")
key = gen_key(4096)
print("Done!")
x1_plain = int(input("Enter first integer: "))
x2_plain = int(input("Enter second integer: "))
x1 = encrypt_integer(x1_plain, key) # Ex: 2
x2 = encrypt_integer(x2_plain, key) # Ex: 4
hom_c = homomorphise(x1, x2, key) # Ex: 109238740192837409162348...
p = decrypt_integer(hom_c, key) # Ex: 8
print("\nCiphertext 1:\n{}\n\nCiphertext 2:\n{}\n\nHomomorphised Ciphertext:\n{}\n\nPlaintext:\n{}".format(
x1, x2, hom_c, p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment