c++ - std::string 到 SecByteBlock 的转换

标签 c++ string crypto++ converters

我目前正在用 C++(使用 crypto++)编写河豚加密/解密程序。

我真的没有在谷歌上找到满意的答案。我正在尝试将 SecByteBlock 的 key 作为字符串发送,然后在另一部分作为字符串接收,然后需要重新返回到 SecByteBlock。

是否可以转换字符串<--> SecByteBlock

我可以做些更好的事情来将 key 从一个函数发送到另一个函数吗?

提前感谢您的帮助。

我使用的代码如下:

int main(int argc, char* argv[])
{
    AutoSeededRandomPool prng;
    SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH);
    prng.GenerateBlock(key, key.size());

    byte iv[Blowfish::BLOCKSIZE];
    prng.GenerateBlock(iv, sizeof(iv));
    BLOCK test = ReadStaticBlocks("testBin.dat");

    string plain=test.bl1 ;
    string cipher, encoded, recovered;

    /*********************************\
    \*********************************/

    // Pretty print key
    encoded.clear();
    StringSource ss1(key, key.size(), true,
        new HexEncoder(
            new StringSink(encoded)
        ) // HexEncoder
    ); // StringSource
    cout << "key: " << encoded << endl;

    // Pretty print iv
    encoded.clear();
    StringSource ss2(iv, sizeof(iv), true,
        new HexEncoder(
            new StringSink(encoded)
        ) // HexEncoder
    ); // StringSource
    cout << "iv: " << encoded << endl;

    /*********************************\
    \*********************************/

    try
    {
        cout << "plain text: " << plain << endl;

        CBC_Mode< Blowfish >::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        // The StreamTransformationFilter adds padding
        //  as required. ECB and CBC Mode must be padded
        //  to the block size of the cipher.
        StringSource ss3(plain, true, 
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) // StreamTransformationFilter      
        ); // StringSource


    }
    catch(const CryptoPP::Exception& e)
    {
        cerr << e.what() << endl;
        exit(1);
    }

    /*********************************\
    \*********************************/

    // Pretty print
    encoded.clear();
    StringSource ss4(cipher, true,
        new HexEncoder(
            new StringSink(encoded)
        ) // HexEncoder
    ); // StringSource
    cout << "cipher text: " << encoded << endl;

    /*********************************\
    \*********************************/

    try
    {
        CBC_Mode< Blowfish >::Decryption d;
        d.SetKeyWithIV(key, key.size(), iv);

        // The StreamTransformationFilter removes
        //  padding as required.
        StringSource ss5(cipher, true, 
            new StreamTransformationFilter(d,
                new StringSink(recovered)
            ) // StreamTransformationFilter
        ); // StringSource

        cout << "recovered text: " << recovered << endl;
    }
    catch(const CryptoPP::Exception& e)
    {
        cerr << e.what() << endl;
        exit(1);
    }

    /*********************************\
    \*********************************/
    system("pause");
    return 0;
}

最佳答案

Is it possible to convert string <--> SecByteBlock

是的。每个都有一个带有指针和长度的构造函数。

string s1("hello world");
SecByteBlock b1((const byte*)s1.data(), s1.size());

byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'}; 
SecByteBlock b2(a, sizeof(a));
string s2((const char*)b2.data(), b2.size());

在你的例子中,StringSource还有一个构造函数,它接受一个指针和一个长度。所以你可以执行:

byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'}; 
SecByteBlock b3(a, sizeof(a));

StringSource ss1(b3.data(), b3.size(), true ...);

或者更直接:

byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'}; 
StringSource ss1(a, sizeof(a), true ...);

关于c++ - std::string 到 SecByteBlock 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55736126/

相关文章:

c++ - 在新代码中,为什么要使用 `int` 而不是 `int_fast16_t` 或 `int_fast32_t` 作为计数变量?

c++ - 中继器 :( 问题

c++ - Win32 Windows 线程安全吗?

c++ - boost .Asio : The I/O operation has been aborted because of either a thread exit or an application request

从 C 中的字符串数组创建字符串

c - 如何在不同的行中输入字符串?

c++ - 无法编译,错误 : cryptlib. h:没有那个文件或目录

c# - C# 默认使用的 RSA 算法是什么,它在 Crypto++ 中的合适参数是什么?

python - cryptopp 错误 : expected '=' , ',' 、 ';' 、 'asm' 或 '__attribute__' 在 '{' token 之前

c++ - 在 C++ 中实现原始递归组合器