1

Hi i'm writing a program in java to test a variant of the elgamal encryption, however my problem is not the encryption/decryption chain itself, but how to perform the operations on the input given: a text file. I have a text file with some words in it (for example the content can be: "Hello world, this is a test") and i need to perform numeric operations on them like this:

ciphertext= (message * y) mod p

where y and p are two biginteger. I tried this conversion chain: (reading one string at time):

String->Hexadecimal->Decimal

Then perform the encrypt operation, and then the inverse:

Decimal->Hexadecimal->String

But this doesn't work all the time (i'm currently investigate on this issue). fixed.

My question is, there is a better way to do this? I was reading about the byte array's but i'm not sure how to use them.

[i can post an example of the encryption/decryption chain if needed]

bisrac123
  • 15
  • 4
  • "but this doesn't work all the time"- How do we know which time it works and which time it wasn't? Show your code so that we understand where was the problem. – mazhar islam Jun 04 '15 at 17:15
  • @rakeb.void i'm currently investigate on this issue but with large prime (>1024bit) it isn't fast to test (keep in mind that even a short word from the plaintext is a pretty big number when converted in integer. Example, the word "hello" is equal to "448378203247"). I don't think my chain of conversions is good but it's the only thing that came to my mind to solve this problem, however my question is still open. – bisrac123 Jun 04 '15 at 20:37

1 Answers1

0

BigInteger has a constructor taking a byte array as argument.

Any String can be converted to a byte array, without loss, using (for example), UTF-8 encoding:

byte[] bytes = string.getBytes(StandardCharsets.UTF_8); 

Combine both, and you have an easy way to transform a String into a BigDecimal.

For the reverse operation, use BigInteger.toByteArray(), and then new String(bytes, StandardCharsets.UTF_8).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks, i think this is what i was looking for, and now i'm able to skip the hexadecimal conversion. However, i did some tests and it doesn't work with some special characters like "è" "ò" and similar (my solution with hexadecimal suffers from this problem too). I think it's because of the StandardCharsets UTF_8 but I'm new to this kind of things so i'm not sure. Any ideas to resolve this? – bisrac123 Jun 05 '15 at 13:40
  • It does work with accents. If it doesn't, then it probably means that your source files encoding doesn't match with the encoding used to compile the code. Use UTF8 everywhere (`javac -encoding UTF-8`). – JB Nizet Jun 05 '15 at 14:47
  • I created a txt file with notepad and i choose the encoding UTF-8 but it doesnt work with char like "è". Then i tried without file, using the input from the user console: Scanner user_input = new Scanner( System.in); text= user_input.next( ); but this doesn't work too. In the first case the problem can be the way i used to read/write the file, but for the input from console i have no idea why it doesn't work. – bisrac123 Jun 06 '15 at 15:46