将自动种子 PNRG 的输出放入 Crypto++ 中

发布于 2024-12-12 14:38:25 字数 858 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静水深流 2024-12-19 14:38:26

将任意字节(字符)附加到字符串末尾将导致包含一些不可打印的字符:

http://en.wikipedia.org/wiki/Control_character

您没有提及您想要或期望的内容。您希望该字符串与发送到 std::cout 的字符串相同吗?如果是这样,您可以通过 #include 使用字符串流:

std::stringstream ss;
for( unsigned int i = 0; i < BLOCKSIZE; i++ )
{
    ss << "0x" << std::setbase(16) << std::setw(2) << std::setfill('0');
    ss << static_cast<unsigned int>(pcbScratch[i]);
}
str = ss.str();

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>:

std::stringstream ss;
for( unsigned int i = 0; i < BLOCKSIZE; i++ )
{
    ss << "0x" << std::setbase(16) << std::setw(2) << std::setfill('0');
    ss << static_cast<unsigned int>(pcbScratch[i]);
}
str = ss.str();
四叶草在未来唯美盛开 2024-12-19 14:38:26

您还可以使用 Crypto++ 内置的 HexEncoder

std::cout << "The generated random block is:" << std::endl;
string str = "0x";

StringSource ss(pcbScratch, BLOCKSIZE, true,
    new HexEncoder(
        new StringSink(str),
        true,   // uppercase
        2,      // grouping
        " 0x"   // separator
    ) // HexDecoder
); // StringSource

StringSource “拥有”HexEncoder,因此无需调用 delete.

You can also use Crypto++'s built in HexEncoder:

std::cout << "The generated random block is:" << std::endl;
string str = "0x";

StringSource ss(pcbScratch, BLOCKSIZE, true,
    new HexEncoder(
        new StringSink(str),
        true,   // uppercase
        2,      // grouping
        " 0x"   // separator
    ) // HexDecoder
); // StringSource

The StringSource 'owns' the HexEncoder, so there's no need to call delete.

层林尽染 2024-12-19 14:38:25

您将需要一些输出编码,例如

  • base64
  • 十六进制

,因为您看到的是原始二进制数据,被解释为文本。随机字符是 AFAICT (谷歌)的结果

,你应该能够使用这样的东西

#include <base64.h>

string base64encoded; 
StringSource(str, true, new Base64Encoder(new StringSink(base64encoded)));

You will want to some output encoding, e.g.

  • base64
  • hex

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

#include <base64.h>

string base64encoded; 
StringSource(str, true, new Base64Encoder(new StringSink(base64encoded)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文