bool InternetHandler::InternetFiletoMemoryBuffer (char *szUrl, MemoryBuffer *memOut, unsigned int maxsize)
{
	
	DWORD dwBytesread = 0;
	MemoryBuffer memTemp;

	HINTERNET myInternet = InternetOpen ("CedeCrypt", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);

	if (myInternet != NULL) {
		
		HINTERNET myUrl = InternetOpenUrl (myInternet, szUrl, NULL, NULL, INTERNET_FLAG_RELOAD, NULL);

		if (myUrl != NULL) {

			memTemp.SetSize (maxsize);

			if (InternetReadFile (myUrl, memTemp.GetBuffer (), maxsize, &dwBytesread) == false) {
				InternetCloseHandle (myInternet);
				memTemp.Clear ();
				return false;
			} else {
				if (dwBytesread > 0) {
					memOut->SetSize (dwBytesread);
					memOut->Write (memTemp.GetBuffer (), 0, dwBytesread);
					memTemp.Clear ();
					InternetCloseHandle (myInternet);
					return true;
				} else {
					InternetCloseHandle (myInternet);
					memTemp.Clear ();
					return false;
				}
			}
		} else {
			InternetCloseHandle (myInternet);
			return false;
		}
	} else {
		return false;
	}
	

	//return false;
}
void Encryption::GenHive ()
{
	// This generates the first pass of the main key hive.
	// This is only done once, because the initial hive comes from the seed
	// after this, we then use expand hive to create the final key hive.
	int s = 0;

	char szMult[2];
	char szPower[1];
	char szProduct[SIZE_STRING];

	unsigned long lMult = 0;
	unsigned long lPower = 0;
	unsigned long lProduct = 0;

	MemoryBuffer memHive;
	memHive.SetSize (m_memSeed.GetAppendPointer ()*SIZE_STRING);

	for (s=0;s<m_memSeed.GetAppendPointer ()-3;s++) {

		// Copy 3 bytes at the current position into the Multiplier buffer
		ZeroMemory (szMult, 2);
		ZeroMemory (szPower, 1);
		strncpy (szMult, (char *) m_memSeed.GetBuffer ()+s, 2);
		strncpy (szPower, (char *) m_memSeed.GetBuffer ()+s+2, 1);

		lMult = atoi (szMult);
		lPower = atoi (szPower);

		ZeroMemory (szProduct, SIZE_STRING);
		lProduct = Raise (lMult, lPower);
		ultoa (lProduct, szProduct, 10);
		
		//OutputInt ("lProduct: ", lProduct);
		//OutputText ("szProduct: ", szProduct);

		memHive.Append ((char *) szProduct, strlen (szProduct));
	}

	m_memHive.SetSize (memHive.GetAppendPointer ());
	m_memHive.Append (memHive.GetBuffer (), memHive.GetAppendPointer ());
	memHive.Clear ();
}
void Encryption::SeedChecksum ()
{
	// This function computes the checksum of the input passphrase
	// and then adds this checksum value to each byte value in the current seed string.

	MemoryBuffer memNewseed;

	char szChecksum [SIZE_STRING];
	ZeroMemory (szChecksum, SIZE_STRING);

	char szSeedpart [SIZE_STRING];
	ZeroMemory (szSeedpart, SIZE_STRING);

	char szNewseed [SIZE_STRING];
	ZeroMemory (szNewseed, SIZE_STRING);

	unsigned int iSeedpart;

	itoa (m_seedChecksum, szChecksum, 10);

	memNewseed.SetSize (strlen (szChecksum) * SIZE_STRING);

	for (int s=0;s<m_memSeed.GetAppendPointer ()-strlen(szChecksum);s++) {
		ZeroMemory (szSeedpart, SIZE_STRING);
		strncpy (szSeedpart, (char *) m_memSeed.GetBuffer ()+s, strlen (szChecksum));

		iSeedpart = atoi (szSeedpart);
		iSeedpart+=m_seedChecksum;

		ZeroMemory (szNewseed, SIZE_STRING);
		itoa (iSeedpart, szNewseed, 10);

		memNewseed.Append ((char *) szNewseed, strlen (szNewseed));
	}

	m_memSeed.Clear ();
	m_memSeed.SetSize (memNewseed.GetAppendPointer ());
	m_memSeed.Append (memNewseed.GetBuffer (), memNewseed.GetAppendPointer ());
	memNewseed.Clear ();

	//OutputSeed ();
}
unsigned long Encryption::ExpandHive (MemoryBuffer *phive)
{
	MemoryBuffer tmpHive;

	int s = 0;
	char szMult[2];
	char szPower[1];
	char szProduct[SIZE_STRING];

	unsigned long lMult = 0;
	unsigned long lPower = 0;
	unsigned long lProduct = 0;

	tmpHive.SetSize (phive->GetAppendPointer ()*SIZE_STRING);

	for (s=0;s<phive->GetAppendPointer ()-3;s++) {
		// Copy 3 bytes at the current position into the Multiplier buffer
		ZeroMemory (szMult, 2);
		ZeroMemory (szPower, 1);
		strncpy (szMult, (char *) phive->GetBuffer ()+s, 2);
		strncpy (szPower, (char *) phive->GetBuffer ()+s+2, 1);

		lMult = atoi (szMult);
		lPower = atoi (szPower);

		ZeroMemory (szProduct, SIZE_STRING);
		lProduct = Raise (lMult, lPower);
		ultoa (lProduct, szProduct, 10);

		tmpHive.Append ((char *) szProduct, strlen (szProduct));
	}

	phive->Clear ();
	phive->SetSize (tmpHive.GetAppendPointer ());
	phive->Append (tmpHive.GetBuffer (), tmpHive.GetAppendPointer ());
	tmpHive.Clear ();
	return phive->GetAppendPointer ();
}
bool StandardEncryption::EncryptBuffer (MemoryBuffer *memSource, PCHAR szPassword, bool bEncrypt)
{
	HCRYPTPROV hCryptProv; 
	HCRYPTHASH hHash; 
	HCRYPTKEY hKey;
	DWORD dwBufferlen;
	DWORD dwBufsize;
	MemoryBuffer memOutput;

	// Get the handle to the default provider. 
	if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROVIDER , 0)) {
	   //printf("A cryptographic provider has been acquired. \n");
	} else {
	   MyHandleError("Error during CryptAcquireContext!"); 
	   return false;
	}

	if(!szPassword ) { 
		return false;
	} else { 
		// Create a hash object. 
		if(CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) {
			//printf("A hash object has been created. \n");
		} else { 
			 MyHandleError("Error during CryptCreateHash!");
			 return false;
		}

		//-------------------------------------------------------------------
		// Hash the password.
		if(CryptHashData(hHash, (BYTE *)szPassword, strlen(szPassword), 0)) {
			//printf("The password has been added to the hash. \n");
		} else {
			MyHandleError("Error during CryptHashData."); 
			return false;
		}

		//-------------------------------------------------------------------
		// Derive a session key from the hash object. 
		if(CryptDeriveKey(hCryptProv, m_Currentalg, hHash, m_dwKeylength, &hKey)) {
			//printf("An encryption key is derived from the password hash. \n"); 
		} else {
			MyHandleError("Error during CryptDeriveKey!");
			return false;
		}
		//-------------------------------------------------------------------
		// Destroy hash object. 
		if(hHash) {
			if(!(CryptDestroyHash(hHash))) {
			   MyHandleError("Error during CryptDestroyHash"); 
			   return false;
			}
			hHash = 0;
		}

		// Encrypt / Decrypt data.
		if (bEncrypt == true) {
			// First get the size of the buffer needed.
			dwBufferlen = memSource->GetSize ();
			dwBufsize = memSource->GetSize ();

			CryptEncrypt (hKey, 0, TRUE, 0, NULL, &dwBufferlen, dwBufsize);

			if (dwBufferlen > 0) {
				dwBufsize = dwBufferlen;
				memOutput.SetSize (dwBufferlen);
				memOutput.Write (memSource->GetBuffer (), 0, memSource->GetSize ());

				if (!CryptEncrypt (hKey, 0, FALSE, 0, (BYTE *) memOutput.GetBuffer (), &dwBufferlen, dwBufsize)) {
					MyHandleError ("Error during Encrypt.");
					return false;
				} else {
					memSource->Clear ();
					memSource->SetSize (memOutput.GetSize ());
					memSource->Write (memOutput.GetBuffer (), 0, memOutput.GetSize ());
					memOutput.Clear ();
				}
			} else {
				OutputText ("Unable to obtain encrypted buffer size.");
				return false;
			}		
		} else {
			dwBufferlen = memSource->GetSize ();

			memOutput.SetSize (dwBufferlen);
			memOutput.Write (memSource->GetBuffer (), 0, memSource->GetSize ());

			if (!CryptDecrypt (hKey, 0, FALSE, 0, (BYTE *) memOutput.GetBuffer (), &dwBufferlen)) {
				MyHandleError ("Error during Decrypt.");
				return false;
			} else {
				memSource->Clear ();
				memSource->SetSize (dwBufferlen);
				memSource->Write (memOutput.GetBuffer (), 0, dwBufferlen);
				memOutput.Clear ();
			}
		}

		//-------------------------------------------------------------------
		// Destroy the session key. 
		if(hKey)
		{
			if(!(CryptDestroyKey(hKey))) {
				MyHandleError("Error during CryptDestroyKey");
				return false;
			}
				
		}

		//-------------------------------------------------------------------
		// Release the provider handle. 

		if(hCryptProv)
		{
			if(!(CryptReleaseContext(hCryptProv, 0))) {
				MyHandleError("Error during CryptReleaseContext");
				return false;
			}
				
		}
			return true;
		}
}
bool Encryption::DoCipher (HWND hWnd, MemoryBuffer *pinbuffer, MemoryBuffer *poutbuffer, bool bEncrypt)
{
	// Single threaded encryption function. Operates in a seperate thread, but a single thread
	// only. Unfortunately on a Core Duo, or HT processor, it will only max out one of the CPU
	// windows.

	MemoryBuffer tmpBuffer;
	unsigned long rndHive = 0;
	int algsize = 100;
	unsigned long hivepointer = 0;
	unsigned long inputoffset = 0;
	unsigned long iprogress = 0;
	unsigned long ipercentage = 0;
	int algpointer = 0;
	unsigned long i = 0;

	int cTrans = 0; // Our transposition value
	int cAlg = 0; // The algorithm we use alongside the transposition value
	unsigned long cProduct = 0; // This value is the current result of the transposition multiplication
	bool cShift = 0;  // The transposition shift - up or down
	BYTE cByte = 0; // The current byte we're working on.

	if (m_bHivecreated == false) {
		OutputText ("Encryption: ", "Key hive not created.");
		return false;
	} else {
	
		tmpBuffer.SetSize ((pinbuffer->GetSize ()+sizeof (unsigned long))*2);
		OutputInt ("DoCipher: ", pinbuffer->GetSize ()*2);

		// Generate a random number between 0 and the size of the hive
		// minus 100 (we use 100 single digits for determining the transposition
		// shift mechanism and transposition multiplier

		if (bEncrypt == true) {
			rndHive = GetRand (m_memHive.GetAppendPointer ()-algsize);
			tmpBuffer.Append (&rndHive, sizeof (unsigned long));
		} else {
			memcpy (&rndHive, pinbuffer->GetBuffer (), sizeof (unsigned long));
			inputoffset+=sizeof (unsigned long);
		}

		for (i=0;i<pinbuffer->GetSize ()-inputoffset;i++) {
			
			
			cTrans = GetHiveValue (hivepointer, 2);
			cAlg = GetHiveValue (rndHive+algpointer, 1);

			if (cAlg <= 5) {
				cShift = true; // true means we transpose up
			} else {
				cShift = false; // false means we transpose down
			}

			// Now transpose the current byte given the current hive value
			// algorithm value and shift direction.
			cByte = pinbuffer->GetByte (i+inputoffset);
			cProduct = cTrans * cAlg;

			if (bEncrypt == true) {
				if (cShift == true) {
					cByte+=cProduct;
				} else {
					cByte-=cProduct;
				}
			} else {
				if (cShift == true) {
					cByte-=cProduct;
				} else {
					cByte+=cProduct;
				}
			}
			

			tmpBuffer.Append (&cByte, sizeof (BYTE));

			hivepointer+=2;
			algpointer++;

			if (hivepointer >= m_memHive.GetAppendPointer ()-2) {
				hivepointer = 0;
			}

			if (algpointer >= algsize) {
				algpointer = 0;
			}

			iprogress++;
			if (iprogress > 10000) {
				iprogress = 0;
				ipercentage = (i * 10) / (pinbuffer->GetSize ()-inputoffset);
				PostMessage (hWnd, CRYPT_MSG, CRYPT_PROGRESSSINGLE, (LPARAM) ipercentage*10);
			}
		
		}

		poutbuffer->SetSize (tmpBuffer.GetAppendPointer ());
		poutbuffer->Append (tmpBuffer.GetBuffer (), tmpBuffer.GetAppendPointer ());
		tmpBuffer.Clear ();
		PostMessage (hWnd, CRYPT_MSG, CRYPT_PROGRESSSINGLE, (LPARAM) -1);
		return true;
	}
}