views:

197

answers:

4

I'm playing with the El Gamal cryptosystem, and my goal is to be able to encipher and decipher long sequences of text.

El Gamal requires the plaintext to be an integer. I have turned my string into a byte[] using the .getBytes() method for Strings, and then created a BigInteger out of the byte[]. After encryption/decryption, I turn the BigInteger into a byte[] using the .toByteArray() method for BigIntegers, and then create a new String object from the byte[].

I am using a 1035 bit key, and this works perfectly when I encipher/decipher with strings up to 129 characters. With 130 or more characters, the output produced from my decipher method is garbled.

Can someone suggest how to solve this issue?

A: 

You can try

BigInteger pText = new BigInteger(plaintext.getBytes("UTF-8"));

to make the encoding/decoding and enciphering/deciphering more symmetric, but I'm not sure if that's the root cause.

By the way, you should never silently consume an Exception. The very least you can do is just catch (UnsupportedEncodingException e).

polygenelubricants
The UTF encoding is something I had tried to do in order to fix the problem. My bug happened before that. I've modified my code to reflect my original problem.
angstrom91
@angst: thanks for the `main()`, I was able to play around with it, but I couldn't figure out what was wrong, sorry.
polygenelubricants
A: 

You need to use positive numbers for your operations. So you must construct BigInteger like this,

BigInteger pText = new BigInteger(1, plaintext.getBytes());
// 1: select a random integer k such that 1 <= k <= p-2
BigInteger k = abs(new BigInteger(p.bitLength() - 2, sr));
ZZ Coder
Ah, thanks for the tip. It didn't solve my problem, but at least I can rule that cause out.
angstrom91
@ZZC, @angst: actually, the constructor ensures that it's positive already.
polygenelubricants
+4  A: 

Just like in RSA, you cannot encrypt a value larger than the modulus in ElGamal.

GregS
+1. This is correct. @angstrom91: you should really take the time to try to understand the algorithms you implement. Then you will not run into these kind of problems. You will have to split up the string and encode each chunk.
back2dos
Oh yeah, it makes sense that UTF-*8* takes 8 bits to encode, not 4! Hence the limit of 129 characters, not 258 like I was expecting!
angstrom91
A: 

If you want to encrypt certain data with asymmetric cryptographic algorithm, you can do this only for really short data block. The reasons are both "technical" (the algorithm works this way) and "practical" (asymmetric cryptography is slow).

The right way to encrypt the large block of data using asymmetric cryptographic algorithm is

  1. generate random ("session") key for some symmetric algorithm (AES, RC4, 3DES, you name it).
  2. use this algorithm to encrypt the data
  3. use your asymmetric algorithm to encrypt the session key
  4. store the encrypted key near the data.
  5. stop reinventing the wheel
Eugene Mayevski 'EldoS Corp