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

我真的在Google上找不到满意的答案。我正在尝试将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;
}

最佳答案



是。每个都有一个构造函数,该构造函数需要一个指针和一个长度。

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 ...);

09-25 21:27