BlowFish encrypt/decrypt of memoryblock


#1

I’m sure i’m doing something dumb here, but every time I call MemoryBlock::toString() after encrypting, i get a jassert error in String::fromUTF8().

    MatkatMusic::SHA3 hasher(MatkatMusic::SHA3::Bits512);
        auto s = hasher( "hello" );
        DBG( String(s.size()) );
        //a single byte from hasher is represented by 2 digits in the string.
        BlowFish bf(s.c_str(), s.length()/2);

        auto test = String( hasher("goodbye") );
        //test is 64 bytes long.
        DBG( "String: "+test );
        //StringRef sr(test);
        //test.toStdString().data();
        MemoryBlock m(test.toStdString().data(), 128);
        DBG( "memoryblock: " + m.toString() );

        uint32* data = (uint32*)m.getData();
        uint32* t = data;
        for( int i = 1; i < m.getSize(); ++i) {
            bf.encrypt(*t++, *t);
        }
        DBG( "memoryblock: " + m.toString() );
        for( int i = 127; i > 1; --i) {
            bf.decrypt(*t--, *t);
        }
        DBG( "memoryblock: " + m.toString() );

this is the output that I get:

128
String:      b321643f425454e0e588b392f034f340223b563a9b0b56bb42a7f9aa37a76324ac7ed21646be6104a2dfb998c7c4bfa6af9a54f99120c3b6e75c42895a052026
memoryblock: b321643f425454e0e588b392f034f340223b563a9b0b56bb42a7f9aa37a76324ac7ed21646be6104a2dfb998c7c4bfa6af9a54f99120c3b6e75c42895a052026
JUCE Assertion failure in juce_String.cpp:2155

the failure occurs on the DBG() after the first encrypt.

Regarding the weird encrypt/decrypt usage, I was trying to encrypt the byte pairs of my memory block in the following order, just to see what it would do:

bf.encrypt(m[0],m[1]);
bf.encrypt(m[1],m[2]);
bf.encrypt(m[2],m[3]);
...
bf.encrypt(m[125], m[126]);
bf.encrypt(m[126], m[127]);

and then decrypt in the same style, but backwards.

bf.decrypt(m[127], m[126]);
bf.decrypt(m[126], m[125]);
bf.decrypt(m[125], m[124]);
...
bf.decrypt(m[2], m[1]);
bf.decrypt(m[1], m[0]);

#2

The encrypted data won’t be a vaild string. If you need it as a String then you’d need convert it to hex or something like that.

Also, I’m pretty sure this is undefined behaviour:

That is: it is undefined whether the second argument gets the original pointer or the incremented one.