std :: string到SecByteBlock的转换 [英] std::string to SecByteBlock conversion

查看:234
本文介绍了std :: string到SecByteBlock的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently writing blowfish encryption/decryprion program in C++ (using crypto++).

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

I really didn't find a satisfied answer at google. I'm trying to send a key of a SecByteBlock as a string and then received in the other part as string then need to be regained to SecByteBlock.

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

Is it possible to convert string <--> SecByteBlock

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

Can I do something better to send key from one function to another?

谢谢您的帮助。

我使用的代码如下:

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;
}


推荐答案


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

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 也有一个带有指针和长度的构造函数。因此,您可以执行以下操作:

And in your case, the StringSource also has a constructor that takes a pointer and a length. So you could perform:

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

这篇关于std :: string到SecByteBlock的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆