如何将字符串转换为 ZZ 数字?
我正在使用 NTL 库 来实现 ElGamal 加密/解密算法。 我已经知道它正在工作,但算法希望将消息转换为整数,以便可以对其进行加密。
因此,如果我输入像 1234 这样的数字,一切正常,但是我如何能够将 C++ 字符串 (std::string) 转换为 ZZ 数字,然后从该 ZZ 数字转换回字符串?
LE:
ZZ是一个代表大数的类。
例如: 18287348238476283658234881728316274273671623781254124517353
所以基本上我想以“Hello World”为例并逐个字符地运行它并获取字符的ascii代码,这样我会得到一个数字:“72 101 108 108 111 32 87 111 114 108 100英寸
然后我需要将这个数字转换回字符串“Hello World”
或者也许有更好的方法。
I'm using the NTL library to implement ElGamal encryption/decryption algorithm.
I've got it to the point that it's working but the algorithm wants the message to be converted to integers so it can be encrypted.
So if i input a number like 1234 everything works ok but how would i go to be able to convert a C++ string (std::string) to a ZZ number and then back from that ZZ number to a string?
LE:
ZZ it's a class that represent a large number.
Ex: 18287348238476283658234881728316274273671623781254124517353
So basically i'm looking to take "Hello World" for example and run it char by char and get the ascii code of the chars so i'll get a number: "72 101 108 108 111 32 87 111 114 108 100"
And then i need to convert this number back to string "Hello World"
Or maybe there's a better way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是NTL作者网站的回答:
Source: http://www.shoup.net/ ntl/doc/conversions.txt
我尝试了一下,效果很好。
Here is an answer from the NTL author's website:
Source: http://www.shoup.net/ntl/doc/conversions.txt
I tried it, and it worked perfectly.
这是一个简单的方法:
现在请注意:
Here is an easy way to do it:
Now notice that:
到目前为止,最简单的解决方案是认识到您可以将
char
转换为unsigned int
,并将unsigned int
转换为ZZ< /代码> 数字。然后将该字符串视为以 256 为基数的数字。例如,“abc”将为 256*“ab”+“c”,或 65536 *“a”+ 256 *“b”+“c”,或
((ZZ(unsigned('a')* 256) + ZZ(无符号('b'))*256) + ZZ(无符号('c'))
,By far the easiest solution is to realize that you can convert
char
s tounsigned int
s, andunsigned int
s toZZ
numbers. Then treat the string as a base-256 number. For instance, "abc" would be 256*"ab" + "c", or 65536 * "a" + 256 * "b" + "c", or((ZZ(unsigned('a')*256) + ZZ(unsigned('b'))*256) + ZZ(unsigned('c'))
,