How to encrypt / decrypt a text file in wxWidgets?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
linb
In need of some credit
In need of some credit
Posts: 6
Joined: Thu Apr 26, 2012 9:38 am

How to encrypt / decrypt a text file in wxWidgets?

Post by linb » Fri Jul 27, 2012 4:35 am

I need a function:

1. A user can give a password to a text file;
2. Another user only input the password to open the text file;

I can't find any crypto funciton in wxWidegets, how can I encrypt/decrypt a text file in a simple way?

RomanV
Knows some wx things
Knows some wx things
Posts: 32
Joined: Tue Mar 27, 2007 10:29 am
Contact:

Re: How to encrypt / decrypt a text file in wxWidgets?

Post by RomanV » Fri Jul 27, 2012 7:30 am

There are no encryption functions in wxWidgets. You must use third-party library like:
Libgcrypt http://directory.fsf.org/wiki/Libgcrypt
Libcrypt
Libxcrypt
Libcrypto++

User avatar
bishop.gis
Earned a small fee
Earned a small fee
Posts: 20
Joined: Fri May 25, 2012 6:47 pm

Re: How to encrypt / decrypt a text file in wxWidgets?

Post by bishop.gis » Fri Jul 27, 2012 7:46 am

I use OpenSSl or xor. If openssl lib linked the program use it, else xor.
Like this:

Code: Select all

#ifdef HAVE_OPENSSL

#include <openssl/evp.h>
#include <openssl/rand.h>

#define EVP_KEY_SIZE	32
#define EVP_IV_SIZE		8
#define BUFSIZE			1024

bool Crypt(const wxString &sText, wxString &sCryptText)
{

	GByte *pabyKey = GetKey();
	GByte *pabyIV = GetIV();

	EVP_CIPHER_CTX* ctx = CreateCTX(pabyKey, pabyIV, false);
	if(!ctx)
	{
		wxLogError(_("Crypt: Failed EVP_EncryptInit!"));
		CPLFree( pabyKey );
		CPLFree( pabyIV );
		return false;
	}

	CPLString pszText(sText.mb_str(wxConvUTF8));
	int outlen;
	unsigned char outbuf[BUFSIZE];

	bool bResult = EVP_EncryptUpdate(ctx, outbuf, &outlen, (const unsigned char*)pszText.data(), pszText.length() * sizeof(pszText[0]) + 1);

	if(!bResult)
	{
		wxLogError(_("Crypt: Failed EVP_EncryptUpdate!"));
		CPLFree( pabyKey );
		CPLFree( pabyIV );
		return bResult;
	}

	int nLen = outlen;
	bResult = EVP_EncryptFinal(ctx, &outbuf[outlen], &outlen);
	nLen += outlen;

	CPLString pszOutput(CPLBinaryToHex(nLen, outbuf));
	sCryptText = wxString(pszOutput, wxConvUTF8);

	CPLFree( pabyKey );
	CPLFree( pabyIV );
	EVP_CIPHER_CTX_cleanup(ctx);
	//EVP_CIPHER_CTX_free(ctx);

	return bResult;
}

bool Decrypt(const wxString &sText, wxString &sDecryptText)
{

	GByte *pabyKey = GetKey();
	GByte *pabyIV = GetIV();

	EVP_CIPHER_CTX* ctx = CreateCTX(pabyKey, pabyIV, true);
	if(!ctx)
	{
		wxLogError(_("Decrypt: Failed EVP_DecryptInit!"));
		CPLFree( pabyKey );
		CPLFree( pabyIV );
		return false;
	}

	int nTextBytes;
	GByte *pabyText = CPLHexToBinary( sText.mb_str(wxConvUTF8), &nTextBytes );

	int outlen;
	unsigned char outbuf[BUFSIZE];

	bool bResult = EVP_DecryptUpdate(ctx, outbuf, &outlen, pabyText, nTextBytes);
	if(!bResult)
	{
		wxLogError(_("Decrypt: Failed EVP_DecryptUpdate!"));
		CPLFree( pabyKey );
		CPLFree( pabyIV );
		CPLFree( pabyText );
		return bResult;
	}

	int nLen = outlen;
	bResult = EVP_DecryptFinal(ctx, &outbuf[outlen], &outlen);
	nLen += outlen;
	outbuf[nLen] = 0;

	CPLString szCryptText((const char*)outbuf);
	sDecryptText = wxString(szCryptText, wxConvUTF8);

	CPLFree( pabyKey );
	CPLFree( pabyIV );
	CPLFree( pabyText );

	EVP_CIPHER_CTX_cleanup(ctx);
	//EVP_CIPHER_CTX_free(ctx);

	return bResult;
}

#else

#define hibyte(a) ((a>>8) & 0xFF)
#define lobyte(a) ((a) & 0xFF)
#define PASSWD wxT("some_passwd")

bool Crypt(const wxString &sText, wxString &sCryptText)
{
    wxString sPass(PASSWD);
    size_t pos(0);
    for(size_t i = 0; i < sText.Len(); ++i)
    {
        if(pos == sPass.Len())
            pos = 0;
        wxUint32 val1 = sText[i].GetValue();
        wxUint32 val2 = sPass[pos].GetValue();
        wxUint32 Symbol = val1 ^ val2;
        char SymbolHi = hibyte(Symbol);
        char SymbolLo = lobyte(Symbol);//(Symbol >> 4) & 0xff;
		sCryptText += wxDecToHex(SymbolHi);
		sCryptText += wxDecToHex(SymbolLo);
        pos++;
    }
	return true;
}

bool Decrypt(const wxString &sText, wxString &sDecryptText)
{
    wxString sPass(PASSWD);
    size_t pos(0);
    for(size_t i = 0; i < sText.Len(); i += 4)
    {
        if(pos == sPass.Len())
            pos = 0;
		wxString sHex = sText[i];
		sHex += sText[i + 1]; 
        char SymbolHi = wxHexToDec(sHex);
        sHex = sText[i + 2];
		sHex += sText[i + 3];
        char SymbolLo = wxHexToDec(sHex);
        wxUint32 Symbol = (SymbolHi << 8) + SymbolLo;
        wxUint32 val2 = sPass[pos].GetValue();
        Symbol = Symbol ^ val2;
        sDecryptText += wxUniChar(Symbol);
        pos++;
    }
    return true;
}
#endif //HAVE_OPENSSL

Post Reply