转换纯文本以执行Elgamal加密 [英] Convert plaintext to perform elgamal encryption
问题描述
我正在用Java编写程序来测试Elgamal加密的变体,但是我的问题不是encryption/decryption
链本身,而是如何对给定的输入执行操作:一个文本文件.我有一个带有一些单词的文本文件(例如,内容可以是:"Hello world, this is a test"
),我需要像这样对它们执行数字运算:
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:
密文=(消息* y)mod p
ciphertext= (message * y) mod p
,其中y
和p
是两个biginteger
.我尝试了此转换链:(一次读取一个字符串):
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
但这并不是一直都有效(我目前正在对此问题进行调查).已修复.
我的问题是,有更好的方法吗?我正在阅读有关byte array
的内容,但不确定如何使用它们.
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]
推荐答案
BigInteger has a constructor taking a byte array as argument.
使用(例如)UTF-8编码,任何字符串都可以无损地转换为字节数组:
Any String can be converted to a byte array, without loss, using (for example), UTF-8 encoding:
byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
将两者组合在一起,您就可以轻松地将String转换为BigDecimal.
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)
.
这篇关于转换纯文本以执行Elgamal加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!