std :: string到SecByteBlock转换

问题描述 投票:1回答:2

我目前正在用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;
}
c++ string crypto++ converters
2个回答
5
投票

是否可以转换字符串< - > 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 ...);

0
投票

对前一个答案的一个小修正。

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

它完美地运作。

谢谢,它也解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.