字符串到SecByteBlock转换

xte*_*408 1 c++ string converters crypto++

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

我真的没有在谷歌找到满意的答案.我正在尝试将一个SecByteBlock的密钥作为字符串发送,然后在另一部分接收为字符串,然后需要重新获得SecByteBlock.

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

我可以做一些更好的事情将密钥从一个功能发送到另一个功能吗

提前感谢您的帮助.

我使用的代码如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

jww*_*jww 5

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

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

string s1("hello world");
SecByteBlock b1((const byte*)s1.data(), s1.size());
Run Code Online (Sandbox Code Playgroud)

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());
Run Code Online (Sandbox Code Playgroud)

但是,您可能需要在StringSource和之间进行转换StringSource.

在你的情况下,它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 ...);
Run Code Online (Sandbox Code Playgroud)

甚至更直接:

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

  • `SecByteBlock b1(s1.data(),s1.size());`对我来说不起作用... _从'const char*'到'const unsigned char*_的无效转换.你的意思是`char*和byte*之间的演员? (7认同)