Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C

BasicAdmin - Personal Organizer

Rate me:
Please Sign up or sign in to vote.
4.94/5 (14 votes)
1 Aug 2009CPOL6 min read 50.6K   5.6K   60  
Finance, contacts, notes organizer
#include "StdAfx.h"
#include "Interno.h"
#include "osrng.h"

using namespace std;
using namespace CryptoPP;

void CInterno::InternalEncrypt(RSAES_PKCS1v15_Encryptor* pEncryptor, const unsigned char * pbIn, int nLengthInBytes, unsigned char ** ppbOut)
{
	// Calculate the chunk sizes
	int nMaxMsgLen = pEncryptor->FixedMaxPlaintextLength();
	int nChunks = (nLengthInBytes%nMaxMsgLen) ? nLengthInBytes/nMaxMsgLen+1 : nLengthInBytes/nMaxMsgLen;
	int nCipherLen = pEncryptor->CiphertextLength(nMaxMsgLen);

	int nChunkLen;


	// Allocate more than will be needed
	int nBufferSize = nCipherLen * (nChunks+1);
	byte * pbBuffer = new byte[nBufferSize];
	byte * pbOut = pbBuffer;
	memset(pbOut, 0, nBufferSize);
	int nTotalEncryptedBytes = 0;

	// Encrypt in chunks
	AutoSeededRandomPool rng;
	nLengthOutBytes = 0;
	for(int n=1; n <= nChunks; n++)
	{
		// Find the chunk length
		nChunkLen =  (n == nChunks) ? (nLengthInBytes - (n-1)*nMaxMsgLen) : nMaxMsgLen;

		pEncryptor->Encrypt(rng, ((byte*)pbIn) + (n-1)*nMaxMsgLen, nChunkLen, pbOut);

		// Keep the last length
		pbOut += pEncryptor->CiphertextLength(nChunkLen);
		nLengthOutBytes += pEncryptor->CiphertextLength(nChunkLen);

		//DebugPrint("Chunk %d: (%d bytes)\r\n%s", n, nChunkLen, ToBinaryString(pbBuffer, nLengthOutBytes).c_str());
	}
	//
	// Copy bytes out (caller must deallocate memory, no garbage collection on new!)
	//
	*ppbOut = new unsigned char[nLengthOutBytes];
	memcpy(*ppbOut, pbBuffer, nLengthOutBytes);

	// free buffer
	delete pbBuffer;
}

void CInterno::InternalDecrypt(RSAES_PKCS1v15_Decryptor* pDecryptor, const unsigned char * pbIn, int nLengthInBytes, unsigned char ** ppbOut)
{
	// Find the maximum message length
	int nMaxMsgLen = pDecryptor->FixedCiphertextLength();
	int nChunks = (nLengthInBytes%nMaxMsgLen) ? nLengthInBytes/nMaxMsgLen+1 : nLengthInBytes/nMaxMsgLen;

	int nChunkLen;

	int nBufferSize = pDecryptor->MaxPlaintextLength(nMaxMsgLen) * (nChunks+1);
	byte * pbBuffer = new byte[nBufferSize];
	byte * pbOut = pbBuffer;
	memset(pbOut, 0, nBufferSize);
	int nTotalDecryptedBytes = 0;

	// Decrypt chunks
	AutoSeededRandomPool rng;
	nLengthOutBytes = 0;
	for(int n=1; n <= nChunks; n++)
	{
		nChunkLen =  (n == nChunks) ? (nLengthInBytes - (n-1)*nMaxMsgLen) : nMaxMsgLen;
		unsigned int unLen = pDecryptor->MaxPlaintextLength(nChunkLen);

		size_t nOutputLength = 0;
		nOutputLength = pDecryptor->Decrypt(rng, ((byte*)pbIn) + (n-1)*nMaxMsgLen, nChunkLen, pbOut).messageLength;

		pbOut += nOutputLength;
		nLengthOutBytes += nOutputLength;
	}

	//
	// Copy bytes out (caller must deallocate memory, no garbage collection on new!)
	//
	*ppbOut = new unsigned char[nLengthOutBytes];
	memcpy(*ppbOut, pbBuffer, nLengthOutBytes);

	// free memory
	delete pbBuffer;

}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
System developer from Argentina.

Programmed in VB 5,6,.NET, C#, Java, PL-SQL, Transac-SQL, C, C++ and even some "calculator" language.

Love to build small, useful applications.
Usually building big and complicated apps based on solid, reliable components.

Hobbies: reading, photography, chess, paddle, running.

Comments and Discussions