Click here to Skip to main content
12,764,177 members (38,031 online)
Click here to Skip to main content

Stats

128.5K views
4.1K downloads
101 bookmarked
Posted 27 Apr 2003

Open Source Software protection system - Part 1

, 18 Apr 2004
Outlines in developing an open source implementation of a new software protection system.
Encrypt.exe
hashlibproper.dll
SysInfo.dll
Encrypt.dsp
Encrypt.dsw
HashLibProper.dll
HashLibProper.lib
SysInfo.dll
/*
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

Share

About the Author

Kamal Shankar
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 | ;)

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 19 Apr 2004
Article Copyright 2003 by Kamal Shankar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid