Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / MFC

Open source software protection - Part 2

Rate me:
Please Sign up or sign in to vote.
3.28/5 (31 votes)
23 Sep 20038 min read 163.4K   14.9K   119  
This is a better alternative to my previous soulution
#include "StdAfx.h"
#include "BlowFish.h"
#include "Sboxs.h"

#define S(x,i)(SBoxes[i][x.w.byte##i])
#define bf_F(x)(((S(x,0) + S(x,1)) ^ S(x,2)) + S(x,3))
#define ROUND(a,b,n)(a.dword ^= bf_F(b) ^ PArray[n])

BlowFishEnc::BlowFishEnc(const char *pwd) : EncryptionInterface(pwd)
{
 	PArray = new DWORD[18];
 	SBoxes = new DWORD[4][256];

	int			i, j;
	DWORD		data, datal, datar;
	union aword	temp;

	int keyLen = (int)strlen(_encryptionKey);

	// first fill arrays from data tables
	for(i = 0; i < 18; i++)
		PArray[i] = bf_P[i];

	for(i = 0; i < 4; i++)
	{
	 	for(j = 0; j < 256; j++)
	 		SBoxes[i][j] = bf_S[i][j];
	}

	j = 0;
	for(i = 0; i < NPASS + 2; ++i)
	{
		temp.dword = 0;
		temp.w.byte0 = _encryptionKey[j];
		temp.w.byte1 = _encryptionKey[(j+1) % keyLen];
		temp.w.byte2 = _encryptionKey[(j+2) % keyLen];
		temp.w.byte3 = _encryptionKey[(j+3) % keyLen];
		data = temp.dword;
		PArray[i] ^= data;
		j = (j + 4) % keyLen;
	}

	datal = 0;
	datar = 0;

	for(i = 0; i < NPASS + 2; i += 2)
	{
		BlowFishEnc_encipher(&datal, &datar);
		PArray[i] = datal;
		PArray[i + 1] = datar;
	}

	for(i = 0; i < 4; ++i)
	{
		for(j = 0; j < 256; j += 2)
		{
		  BlowFishEnc_encipher(&datal, &datar);
		  SBoxes[i][j] = datal;
		  SBoxes[i][j + 1] = datar;
		}
	}
}

BlowFishEnc::~BlowFishEnc()
{
	delete PArray;
	delete[] SBoxes;
}

DWORD BlowFishEnc::encryptStream(const char *plain, const DWORD size, char *cipher)
{
	if (plain != cipher)
		memcpy(cipher, plain, size);
	
	DWORD lOutSize = GetOutputLength(size);
	for(DWORD lCount = 0; lCount < lOutSize; lCount += 8)
	{
		if (lCount + 8 > size)
		{
			// point at byte past the end of actual data
			char *po = (char*)cipher + (size - lCount);
			// padding with 255 (FF) characters.
			for(DWORD i = 0; i < (lOutSize - size); i++)
				*po++ = (char)0;
		}
		BlowFishEnc_encipher((DWORD*)cipher, (DWORD*)(cipher + 4));
		cipher += 8;
	}
	return lOutSize;
}

DWORD BlowFishEnc::decryptStream(const char *cipher, const DWORD size, char *plain)
{
	if (plain != cipher)
		memcpy(plain, cipher, size);

	for (DWORD lCount = 0; lCount < size; lCount += 8)
	{
	 	BlowFishEnc_decipher((DWORD*)plain, (DWORD*)(plain + 4));
		plain += 8;
	}
	
	return size;
}
void BlowFishEnc::BlowFishEnc_encipher(DWORD *xl, DWORD *xr)
{
	union aword  Xl, Xr;

	Xl.dword = *xl;
	Xr.dword = *xr;

	Xl.dword ^= PArray[0];
	ROUND(Xr, Xl, 1);
	ROUND(Xl, Xr, 2);
	ROUND(Xr, Xl, 3);
	ROUND(Xl, Xr, 4);
	ROUND(Xr, Xl, 5);
	ROUND(Xl, Xr, 6);
	ROUND(Xr, Xl, 7);
	ROUND(Xl, Xr, 8);
	ROUND(Xr, Xl, 9);
	ROUND(Xl, Xr, 10);
	ROUND(Xr, Xl, 11);
	ROUND(Xl, Xr, 12);
	ROUND(Xr, Xl, 13);
	ROUND(Xl, Xr, 14);
	ROUND(Xr, Xl, 15);
	ROUND(Xl, Xr, 16);
	Xr.dword ^= PArray[17];

	*xr = Xl.dword;
	*xl = Xr.dword;
}

void BlowFishEnc::BlowFishEnc_decipher(DWORD *xl, DWORD *xr)
{
   union aword  Xl;
   union aword  Xr;

   Xl.dword = *xl;
   Xr.dword = *xr;

   Xl.dword ^= PArray[17];
   ROUND(Xr, Xl, 16);
   ROUND(Xl, Xr, 15);
   ROUND(Xr, Xl, 14);
   ROUND(Xl, Xr, 13);
   ROUND(Xr, Xl, 12);
   ROUND(Xl, Xr, 11);
   ROUND(Xr, Xl, 10);
   ROUND(Xl, Xr, 9);
   ROUND(Xr, Xl, 8);
   ROUND(Xl, Xr, 7);
   ROUND(Xr, Xl, 6);
   ROUND(Xl, Xr, 5);
   ROUND(Xr, Xl, 4);
   ROUND(Xl, Xr, 3);
   ROUND(Xr, Xl, 2);
   ROUND(Xl, Xr, 1);
   Xr.dword ^= PArray[0];

   *xl = Xr.dword;
   *xr = Xl.dword;
}

DWORD BlowFishEnc::GetOutputLength(DWORD lInputLong)
{
	DWORD 	lVal;

	lVal = lInputLong % 8;
	if(lVal != 0)
		return lInputLong + 8 - lVal;
	else
		return lInputLong;
}

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