Search code examples
javaencryptioncryptographypaillier

Can pailler cryptosystem encrypt and decrypt negative big integers?


I use paillier cryptosystem to encrypt and decrypt random data which at first are in the form of byte arrays and then i transform them to big integers and if the byte array become a negative big integer the decrypted number and the input number are different (basically it doesn't work with negative big integers). Is there a way to make this work without checking the input if it will become positive or negative ?


Solution

  • No, you cannot use negative numbers as everything is computed modulo n.

    Yes, you can use any array, as long as the value, when converted to a number, is a number smaller than n.

    For this you can use new BigInteger(1, plaintext) which will always result in a positive number. The first parameter is the sign.

    You may need an encoding for specific structures, e.g. with zero bit values for the most significant bits (message-> encode-> convert to number -> Paillier encryption -> encode ciphertext and decode ciphertext -> Paillier decryption -> decode -> message).

    See I2OSP and OS2IP for an example on how to encode / decode data to numbers.