将自动种子 PNRG 的输出放入 Crypto++ 中
我正在使用 Cryptopp 生成随机字符串。 这是代码:
const unsigned int BLOCKSIZE = 16 * 8;
byte pcbScratch[ BLOCKSIZE ];
// Construction
// Using a ANSI approved Cipher
CryptoPP::AutoSeededX917RNG<CryptoPP::DES_EDE3> rng;
rng.GenerateBlock( pcbScratch, BLOCKSIZE );
// Output
std::cout << "The generated random block is:" << std::endl;
string str = "";
for( unsigned int i = 0; i < BLOCKSIZE; i++ )
{
std::cout << "0x" << std::setbase(16) << std::setw(2) << std::setfill('0');
std::cout << static_cast<unsigned int>( pcbScratch[ i ] ) << " ";
str += pcbScratch[i];
}
std::cout << std::endl;
std::cout << str <<std::endl;
我在代码中放入了一个新的变量:string str = ""
。 然后在 for 中为每个结果追加字符串的一部分。 但我的输出很脏!我只看到奇怪的 ASCII 字符。 怎样才能把弦设置好呢?
谢谢。
I'm using Cryptopp to generate a random string.
This is the code:
const unsigned int BLOCKSIZE = 16 * 8;
byte pcbScratch[ BLOCKSIZE ];
// Construction
// Using a ANSI approved Cipher
CryptoPP::AutoSeededX917RNG<CryptoPP::DES_EDE3> rng;
rng.GenerateBlock( pcbScratch, BLOCKSIZE );
// Output
std::cout << "The generated random block is:" << std::endl;
string str = "";
for( unsigned int i = 0; i < BLOCKSIZE; i++ )
{
std::cout << "0x" << std::setbase(16) << std::setw(2) << std::setfill('0');
std::cout << static_cast<unsigned int>( pcbScratch[ i ] ) << " ";
str += pcbScratch[i];
}
std::cout << std::endl;
std::cout << str <<std::endl;
I've put int the code a new var: string str = ""
.
Then in the for append for each result, the part of the string.
But my output is dirty! I see only strange ASCII char.
How can I set well the string?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将任意字节(字符)附加到字符串末尾将导致包含一些不可打印的字符:
http://en.wikipedia.org/wiki/Control_character
您没有提及您想要或期望的内容。您希望该字符串与发送到 std::cout 的字符串相同吗?如果是这样,您可以通过
#include
使用字符串流:Appending arbitrary bytes (chars) to the end of a string is going to result in that containing some non-printable characters:
http://en.wikipedia.org/wiki/Control_character
You don't mention what you wanted or expected. Did you want the string to be the same as what got sent to std::cout? If so, you can use a stringstream via
#include <sstream>
:您还可以使用 Crypto++ 内置的
HexEncoder
:StringSource
“拥有”HexEncoder
,因此无需调用delete.
You can also use Crypto++'s built in
HexEncoder
:The
StringSource
'owns' theHexEncoder
, so there's no need to calldelete
.您将需要一些输出编码,例如
,因为您看到的是原始二进制数据,被解释为文本。随机字符是 AFAICT (谷歌)的结果
,你应该能够使用这样的东西
You will want to some output encoding, e.g.
because what you are seeing is the raw binary data, interpreted as if it were text. Random characters are the consequence
AFAICT (google) you should be able to use something like this