3

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

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.

Is it possible to convert string <--> SecByteBlock

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

Thank you for help in advance.

The code I use is the following:

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;
}
jww
  • 97,681
  • 90
  • 411
  • 885
xtensa1408
  • 584
  • 12
  • 32
  • Now available on the Crypto++ wiki: [`SecBlock`](https://www.cryptopp.com/wiki/SecBlock). The wiki page includes conversion examples. – jww Mar 23 '18 at 19:21

2 Answers2

8

Is it possible to convert string <--> SecByteBlock

Yes. Each has a constructor that takes a pointer and a length.

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

And

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

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

Or even more directly:

byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'}; 
StringSource ss1(a, sizeof(a), true ...);
jww
  • 97,681
  • 90
  • 411
  • 885
  • Thank you for your answer. However, either you don't understand my question or maybe It's me who don't understand your answer. In fact I want to send send SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH); from one function to another. – xtensa1408 Oct 02 '14 at 10:00
  • 8
    `SecByteBlock b1(s1.data(), s1.size());` doesn't work for me... _invalid conversion from ‘const char*’ to ‘const unsigned char*_. Whay do you mean by `cast between char* and byte*` ? – 3isenHeim Sep 08 '15 at 09:33
0

A small correction to the previous answer.

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

It works perfectly.

Thank you, it solve my problem too.

  • Thanks! But why does the cast to `(const unsigned char *)` work over and the cast to `(const byte*)` the original answer not? – Stefan Jul 23 '21 at 18:02