Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / C++

Open Source Software protection system - Part 1

Rate me:
Please Sign up or sign in to vote.
2.48/5 (35 votes)
18 Apr 20049 min read 149.9K   4.3K   102  
Outlines in developing an open source implementation of a new software protection system.
/*
Sample (5 minute solution) to give a rough idea of what exactly I have on my mind. I particularly want to avoid encrypting data to HDD and running it from there (never mind that the user might dump the image from memory etc etc)
Thanks to Nir Dremer's FileEnc code 
*/

#include "stdafx.h"
#include "BlowFish.h"
#include "HashLibProper.h"
#include <stdio.h>

#pragma comment(lib,"HashLibProper.lib")//Explicit linking call to our library(you don't need to put the library in your project after this)

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	char szInputFile[MAX_PATH+1],szOutputFile[MAX_PATH+1],szKey[1024];
	GetCurrentDirectory(MAX_PATH,szInputFile);
	strcpy(szOutputFile,szInputFile);
	////////////////////Starting usage of HashLibProper routine/////////////
	llCD llCd;
	char temp1[256],temp2[256],szMsg[1024];
	llCd.GetMeDrives(3);//GetMeDrives of type DRIVE_FIXED (HDD)
	llCd.validate();
	llCd.CheckMachine(temp1,temp2);
	wsprintf(szMsg,"Your current machine configuration yeilded the string %s\nWith MD5 Digest %s",temp1,temp2);
	MessageBox(NULL,szMsg,"Information..",MB_OK|MB_ICONINFORMATION);//Shows the machine data and the MD5 digest
	/////////////////////End of HashLibProper routine/////////////
	strcpy(szKey,temp2);//We copy the MD5 digest the key here
	//MessageBox(NULL,szInputFile,"Debug",MB_OK);
	int bEnc=MessageBox(NULL,"Press OK to 'Encrypt' or 'Cancel' to decrypt ","Debug",MB_OKCANCEL);
	if(bEnc==IDOK)
	{
		strcat(szInputFile,"\\OriginalData.dat");
		strcat(szOutputFile,"\\EncryptedData.dat");
	}
	else
	{
		strcat(szInputFile,"\\EncryptedData.dat");
		strcat(szOutputFile,"\\DecryptedData.dat");
	}
	
	FILE *readFile = fopen(szInputFile, "rb");
	if (readFile == 0) {MessageBox(NULL,"Unable to open source file.","Debug",MB_OK);return -1;}
	const size_t bufferSize = 1024;
	size_t readRet = 0;
	bool abort = false;
	int encRet;

	char readBuffer[bufferSize];
	char outBuffer[bufferSize];


	FILE *writeFile = fopen(szOutputFile, "wb");
	if (writeFile == 0) {MessageBox(NULL,"Unable to open destination file.","Debug",MB_OK);return -1;}


	BlowFishEnc encryption(szKey);
	
	while (!feof(readFile))
	{
		readRet = fread(readBuffer, sizeof(char), bufferSize, readFile);
			
		//decrypt
		if(bEnc!=IDOK)
		{
	
			encRet = encryption.decryptStream(readBuffer, (DWORD)readRet, outBuffer);
			if feof(readFile)
			{
				int pos = 0;
				// removing trailing zeros - encrypted file must be x8 bytes.
				while ((pos < 8) && ((outBuffer + encRet - pos)[0] == 0)) pos++;
				// if found trailing zeros - decreasing the writing buffer marker (not writing them).
				if (pos) encRet -= (pos - 1);
			}
		}else
		{
			//encrypt
			encRet = encryption.encryptStream(readBuffer, (DWORD)readRet, outBuffer);
		}

		fwrite(outBuffer, sizeof(char), encRet, writeFile);
	}

	fflush(writeFile);

	fclose(writeFile);
	fclose(readFile);
	return 0;
}



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Kamal Shankar is a programming freak (or so he feels).He currently lives in the Salt Lake City and loves doing what he has been since 1990 - coding horribly Wink | ;)

Comments and Discussions