cryptovoting / damgard-jurik

A Python implementation of the threshold variant of the Damgard-Jurik cryptosystem.
MIT License
13 stars 3 forks source link

can you please provide some code for performing homomorphic encryption on text files or string #1

Open sandeephr opened 5 years ago

sandeephr commented 5 years ago

I need to perform homomorphic encryption on my files such as text pdf and few more can I do it in homomorphic encryption similar to AES and DES

nickboucher commented 5 years ago

Hello! The Damgard-Jurik library is designed to encrypt Python integers rather than strings, but you can certainly tranform a string into a list of integers and then encrypt that list. An example of this would be:

from damgard_jurik import keygen

public_key, private_key_ring = keygen(
    n_bits=64,
    s=1,
    threshold=3,
    n_shares=3
)
text = "String to Encrypt"
msg_list = list(map(int, text))
cipher_list = public_key.encrypt_list(msg_list)

Cheers!

nickboucher commented 5 years ago

In order to make this as easy as possible, perhaps we should extend the library to allow the encryption of strings. @swansonk14 can you think of any reason not to overload the generic encryption function with different behaviors for different arg types? If not, I'm happy to branch off and PR.

swansonk14 commented 5 years ago

I think it should definitely be possible to extend the encryption/decryption functions to handle different data types. I'll try to implement this functionality sometime this week.

sandeephr commented 5 years ago

@nickboucher I got an error when I tried running your code .........but replacing "int" with "ord" solved that error. but I got an output which prints [mpz(83), mpz(116), mpz(114), mpz(105), mpz(110), mpz(103), mpz(32), mpz(116), mpz(111), mpz(32), mpz(69), mpz(110), mpz(99), mpz(114), mpz(121), mpz(112), mpz(116)]

I think that's the ASCII value of each word I guess but what does mpz mean.

nickboucher commented 5 years ago

@sandeephr mpz is an arbitrary-precision integer. Once you have the string encoded in a numerical format you should be able to use the library as expected!

We'll release a wrapper around this within the package soon.

sandeephr commented 5 years ago

ok sir thank you

sandeephr commented 5 years ago

I have two questions

  1. I want to view the public key and the private key is there any way to view and print those keys. 2.@nickboucher sir I also wanted to know can I perform homomorphic encryption on different types of files such as pdf images mp4 mp3.
nickboucher commented 5 years ago

@sandeephr You can certainly look inside the key objects and see the underlying data. Check the __dict__ attribute on the relevant objects for the available properties. You can encrypt any file that you would like, but using homomorphic encryption on anything other than integers is probably not worth the performance hit over regular encryption methods, unless you can come up with a good reason that you need homomorphic properties on the other data. To encrypt an arbitrary file type, you could just interpret the binary encoding of the file as a list of integers and encode that list.

nickboucher commented 5 years ago

@swansonk14 I created a branch with a wrapper for string encryption. Originally I thought that it would be nice to just overload the encrypt function to support different data types, but ultimately I thought that was probably a bad idea because the plaintext data type would become less apparent with a generic decrypt method. Curious on your thoughts as well.

In the meantime, I'll make a PR and request your review! :)