问题
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]
回答1:
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)
.
来源:https://stackoverflow.com/questions/30647542/convert-plaintext-to-perform-elgamal-encryption