0

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 ?

LPepe
  • 3
  • 1
  • How do you transform the big integer to byte array and back? – kennytm Jan 28 '17 at 18:31
  • i was using something like this `BigInteger m1 = new BigInteger(data);` and `m1.toByteArray()` but i change it as Maarten Bodewes proposed in his answer so the biginteger will always be positive – LPepe Jan 31 '17 at 15:07

1 Answers1

0

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.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263