Encrypt File

150 views
Skip to first unread message

Stefano Mtangoo

unread,
Jul 12, 2013, 5:14:29 PM7/12/13
to cryptop...@googlegroups.com
Hi,
I wanted to Encrypt my file so I checked the Test and the decrypt functions there does not work or at least I cannot get them work.
First I tried the EncryptFile and it works fine but decryptfile fails. And then I tried encryptString and works fine but the counterpart does not!
They all fail with error: "DefaultDecryptor: cannot decrypt message with this passphrase"

I use the same passphrase (#define PASS "SomePassword"

I'm stucked!

Code:

wxString DecryptString(const wxString& instr, const wxString& passPhrase)
{


    std::string outstr;
    const char* charToDecrypt = instr.ToStdString().c_str();
    const char* pass = passPhrase.ToStdString().c_str();

    try
    {
        CryptoPP::HexDecoder decryptor(new CryptoPP::DefaultDecryptorWithMAC(pass, new CryptoPP::StringSink(outstr)));
        decryptor.Put((byte *)charToDecrypt, strlen(charToDecrypt));
        decryptor.MessageEnd();
    }
    catch (CryptoPP::Exception& e)
    {
        wxPuts(e.what());
    }

    return wxString(outstr);
}

Stefano Mtangoo

unread,
Jul 13, 2013, 4:05:54 AM7/13/13
to cryptop...@googlegroups.com


On Saturday, July 13, 2013 12:14:29 AM UTC+3, Stefano Mtangoo wrote:
Hi,
I wanted to Encrypt my file so I checked the Test and the decrypt functions there does not work or at least I cannot get them work.
First I tried the EncryptFile and it works fine but decryptfile fails. And then I tried encryptString and works fine but the counterpart does not!
They all fail with error: "DefaultDecryptor: cannot decrypt message with this passphrase"

I use the same passphrase (#define PASS "SomePassword"

I'm stucked!
Any Idea as to what I do wrong?

David Irvine

unread,
Jul 13, 2013, 6:48:46 AM7/13/13
to Stefano Mtangoo, Crypto++ Users
I am rushing about just now, but there are some good examples here https://github.com/maidsafe/MaidSafe-Common/blob/master/src/maidsafe/common/crypto.cc

Hope that helps (there are more examples in utils and rsa in this lib)

Stefano Mtangoo

unread,
Jul 13, 2013, 4:53:37 PM7/13/13
to cryptop...@googlegroups.com, Stefano Mtangoo
the code is great but its a bit complex for a newbie like me. I need just a simple encrypt/decrypt function. That is I can encrypt a file and then be able to read the contents(decrypt it). The test seems so much simple but its not working. I don't know why
I use Ubuntu!

David Irvine

unread,
Jul 13, 2013, 7:43:53 PM7/13/13
to Stefano Mtangoo, Crypto++ Users

On Sat, Jul 13, 2013 at 9:53 PM, Stefano Mtangoo <mwinj...@gmail.com> wrote:
the code is great but its a bit complex for a newbie like me. I need just a simple encrypt/decrypt function. That is I can encrypt a file and then be able to read the contents(decrypt it). The test seems so much simple but its not working. I don't know why
I use Ubuntu!

Sorry about that the test should work on ubuntu fine but you would need the superproject installed (see https://github.com/maidsafe/MaidSafe/wiki/Build-Instructions) and that may be too much hassle. We use boost::filesystem to read / write files as it's just a little more cross platform. If you start with a string encrypt decrypt then add reading from file instead of stream you will find it easier. In cryptopp use filestream instead of stringstream if you want to stick to that lib for file operations.

Now perhaps even better for you is to look in test.cpp which is shipped with cryptopp and at near line 500 you will find what your looking for exactly I think.

Make sure to read up on the various modes and be careful of ECB etc. they are not as secure as you think. There is no choice but to do a lot of research when messing with these algorithms, every system is designed to be secure only in very particular situations, so a strong algorithm in the wrong place is very weak.

Anyway I hope this helps.

(here is line 500 of test.cpp just in case)

void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
{
SecByteBlock key = HexDecodeString(hexKey);
SecByteBlock iv = HexDecodeString(hexIV);
CTR_Mode<AES>::Encryption aes(key, key.size(), iv);
FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
}

Best Regards
David Irvine

Jeffrey Walton

unread,
Jul 18, 2013, 3:37:07 PM7/18/13
to cryptop...@googlegroups.com


On Friday, July 12, 2013 5:14:29 PM UTC-4, Stefano Mtangoo wrote:
... 
Code:

wxString DecryptString(const wxString& instr, const wxString& passPhrase)
{
    std::string outstr;
    const char* charToDecrypt = instr.ToStdString().c_str();
    const char* pass = passPhrase.ToStdString().c_str();

    try
    {
        CryptoPP::HexDecoder decryptor(new CryptoPP::DefaultDecryptorWithMAC(pass, new CryptoPP::StringSink(outstr)));
        decryptor.Put((byte *)charToDecrypt, strlen(charToDecrypt));
        decryptor.MessageEnd();
    }
    catch (CryptoPP::Exception& e)
    {
        wxPuts(e.what());
    }

    return wxString(outstr);
}

    const char* charToDecrypt = instr.ToStdString().c_str();
    const char* pass = passPhrase.ToStdString().c_str();

With all things being equal, there's likely an embedded NULL in the charToDecrypt c-string. Don't convert it to a c-style string.

I've never worked with WX, but the following would probably be a step in the right direction:

    const std:string& encrypted = instr.ToStdString();
    const std:string& pass = passPhrase.ToStdString();

And then, perhaps something like:

    // http://www.cryptopp.com/docs/ref/class_default_decryptor_with_m_a_c.html
    CryptoPP::DefaultDecryptorWithMAC decryptor(pass.data(), pass.size(), new CryptoPP::StringSink(outstr)));
    decryptor.Put((byte *)encrypted.data(), encrypted.size());
    decryptor.MessageEnd();

Or:

    StringSource ss(
        encrypted, true,
        new DefaultDecryptorWithMAC decryptor(
            pass.data(), pass.size(),
            new CryptoPP::StringSink(outstr)
        )
    );

Jeff

Stefano Mtangoo

unread,
Jul 19, 2013, 3:30:08 PM7/19/13
to cryptop...@googlegroups.com
Hi Jeff,

I have tried both solutions does not work. I have tried all I could with no avail. Can you try to decrypt this string below and see if it works? I want to confirm its not something funny with the encryption nor password!

String: C53ED32B2F20C2C19786F507671D9BA6AE80298C1C6C04583037498F4A910A3255FD02E53664118AEA315A577A80FCEF
Password: password

I have just encrypted with:


CryptoPP::StringSource ss(
        charToEncrypt, true,
        new CryptoPP::DefaultEncryptorWithMAC(
            (byte*)pass.data(), pass.size(),
            new CryptoPP::HexEncoder(

                new CryptoPP::StringSink(outstr)
            )
        )
    );


Jeff

Jeffrey Walton

unread,
Jul 22, 2013, 2:47:59 AM7/22/13
to cryptop...@googlegroups.com


On Friday, July 19, 2013 3:30:08 PM UTC-4, Stefano Mtangoo wrote:
...

I have tried both solutions does not work. I have tried all I could with no avail.
Can you try to decrypt this string below and see if it works? I want to confirm its not something funny with the encryption nor password!

String: C53ED32B2F20C2C19786F507671D9BA6AE80298C1C6C04583037498F4A910A3255FD02E53664118AEA315A577A80FCEF
Password: password
$ ./cryptopp-test.exe
Decrypted: 2013-07-19

Jeff

 

Stefano Mtangoo

unread,
Jul 22, 2013, 5:11:33 AM7/22/13
to cryptop...@googlegroups.com
Correct!
Can you post the function you used to decrypt it?
Mine fails!

Jeff

 

Stefano Mtangoo

unread,
Jul 22, 2013, 9:42:06 AM7/22/13
to cryptop...@googlegroups.com

Don't know why but this works


wxString DecryptString(const wxString& instr, const wxString& passPhrase)
{
    std::string outstr;

    try
    {
        CryptoPP::HexDecoder decryptor(new CryptoPP::DefaultDecryptorWithMAC(passPhrase.ToStdString().c_str(), new CryptoPP::StringSink(outstr)));
        decryptor.Put((byte *)instr.ToStdString().data(), instr.ToStdString().size());
        decryptor.MessageEnd();

        return outstr;

    }
    catch (CryptoPP::Exception& e)
    {
        wxPuts(e.what());
    }

    return wxString(outstr);
}


 

Jeff

 

Jeffrey Walton

unread,
Jul 22, 2013, 2:52:37 PM7/22/13
to cryptop...@googlegroups.com


On Monday, July 22, 2013 9:42:06 AM UTC-4, Stefano Mtangoo wrote:


On Monday, July 22, 2013 12:11:33 PM UTC+3, Stefano Mtangoo wrote:


On Monday, July 22, 2013 9:47:59 AM UTC+3, Jeffrey Walton wrote:

I have tried both solutions does not work. I have tried all I could with no avail.

Don't know why but this works
...
 
        CryptoPP::HexDecoder decryptor(new CryptoPP::DefaultDecryptorWithMAC(passPhrase.ToStdString().c_str(), new CryptoPP::StringSink(outstr)));
        decryptor.Put((byte *)instr.ToStdString().data(), instr.ToStdString().size());
        decryptor.MessageEnd();
Its the same code as in the wiki page, sans the use of a StringSource or FileSource.

The StringSource or FileSource will call Put2 on its data, and them call MessageEnd (IIRC). Put2 pumps data to its attached transformation, and in the case of the wiki page that's the HexDecoder. Though you did not use a StringSource or FileSource, you did the same.

Jeff
 

Stefano D. Mtangoo

unread,
Jul 22, 2013, 2:55:52 PM7/22/13
to cryptop...@googlegroups.com
All in all am glad it works now and I can proceed with my stalled program.
I hope next stages won't be that hectic :)

Jeff
 
--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to cryptopp-user...@googlegroups.com.
More information about Crypto++ and this group is available at http://www.cryptopp.com.
---
You received this message because you are subscribed to a topic in the Google Groups "Crypto++ Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cryptopp-users/16U0M2B_V7Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cryptopp-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Stefano D. Mtangoo
Mob: +255 754710410
Twitter: @mtangoo
Web. http://hosannahighertech.co.tz
Linkedin: http://www.linkedin.com/pub/stefano-mtangoo/45/644/281
The purpose of man is to know his Maker Be known by his Maker
And make his Maker known So that others may know his Maker as their Maker(Emeal Zwayne)
Reply all
Reply to author
Forward
0 new messages