Convert plaintext to perform elgamal encryption

笑着哭i 提交于 2020-01-06 03:28:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!