Search code examples
crypto++

How to forward file content to StringSink with Crypto++?



I'm using some test code for Crypto++ just to get used to it.
There's a function:

void DecryptFile(const char *in, const char *out, const char *passPhrase)
{
    FileSource f(in, true, new DefaultDecryptorWithMAC(passPhrase, new FileSink(out)));
}

where DecryptFile is #define DecryptFile DecryptFileW (I'm on Windows).

So, file deciphering is called via DecryptFile(name_f.c_str(), "decrypted_alphabet.txt", cpass);
If cpass is correct, then the contents of name_f.c_str() file is written to decrypted_alphabet.txt.

Now the problem: I can't find the way to forward file contents to StringSink rather than to a file! I tried manually calling this:

string out;
FileSource (in, true, new DefaultDecryptorWithMAC(cpass, new StringSink(out)));

but this returns some crappy characters like ъ!$ or 5(# (every time new). Maybe I can't explain my problem properly, so, here's piece of code, where I try to crack a 4-digit protected txt file: http://pastebin.com/FJng91Y6


Solution

  • How to forward file content to StringSink with Crypto++?
    ...
    Now the problem: I can't find the way to forward file contents to StringSink rather than to a file! I tried manually calling this:

    string out;
    FileSource (in, true, new DefaultDecryptorWithMAC(cpass, new StringSink(out)));
    

    This is working as expected.


    but this returns some crappy characters like ъ!$ or 5(# (every time new). Maybe I can't explain my problem properly, so, here's piece of code, where I try to crack a 4-digit protected txt file

    Yes, that sounds about right (if I am reading it correctly). You are generating collisions on the 4-digit passcode. To avoid the collisions, you need a longer passphrase.


    Maybe you should finish enumerating the combinations rather than stopping the processing on the first match:

    if (strlen(cpass) > 0){
        printf("Alphabet password is: %s\n", cpass);
        return;
    }
    

    Collect all the potential decryption, and then further analyze them looking for a "good" decryption. Maybe you should save them to a file using the PIN:

    • decrypt.1234
    • decrypt.4287
    • decrypt.6862
    • decrypt.8946