Click here to Skip to main content
15,900,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have developed an application in which i have to load a file and encrypt the data in the file.If i load the file which is having 3 to 5 lines of data then the file is encrypting properly but if the data in the file is more than one page then my application is getting crashed while encrypting.Below is my code ,can some one help me in solving this.Thank you in Advance

C++
CString FunctionEncrypt(CString str)
{
	CString sPath = L"D:\\";


	//Getting the name from the given path
	CString sData = str;
	CString sComPath;
	int iLen = sData.GetLength();
	int iVal = sData.ReverseFind(_T('.'));
	iLen = iLen - iVal;
	sData = sData.Left(iVal);
	iLen = sData.GetLength();
	iVal = sData.ReverseFind(_T('\\'));
	iLen = iLen - iVal;
	sData = sData.Right(iLen);
	iLen = sData.GetLength();
	iVal = sData.ReverseFind(_T('\\'));
	iLen = iLen - iVal;
	sData = sData.Right(iLen-1);
	sComPath = sPath + L"test";

	//Getting the name from the given path

	//string encrypt_decrypt[] = {"encrypt","decrypt"}, buffer, key, newbuffer = "";
	string encrypt_decrypt[] = {"decrypt"}, buffer, key, newbuffer = "";
	char filename[MAX_PATH];
	char OrgFileName[MAX_PATH];
	char OutputName[MAX_PATH];
	char encryptmap[10000];
	int maplength;
	ofstream out;
//	ifstream in;
	sprintf_s(filename, "%s", CT2CA(str));
	sprintf_s(OutputName, "%s", CT2CA(sComPath));
	key = "abcd1234";
	
	do
	{
		in.open(filename);
		strcpy(OrgFileName, filename);
		if (in.fail())           
		{
			in.open(filename);
		}

		//Enter the name that you want your Output file to be named
		out.open(OutputName);

		//Checks the length of the key for the encryption to be done		
		if (key.length()>128)
		{
			cin >> key;
		}

		maplength=key.length();

		mapitE(key,encryptmap);
		//char line[256];
		
		getline(in,buffer);
		//getline(in,sizeof(buffer));
			
		while(in)
		{ 
			encryptdecrypt(buffer,encryptmap,maplength,newbuffer);
			out << newbuffer;
			newbuffer.erase(0);
			getline(in,buffer);
		}

		out.close();
		in.close();
		in.clear();
		out.clear();
		newbuffer.erase(0);
		remove(OrgFileName);		
	}while (FALSE);
	return sData;
}

void encryptdecrypt(string buffer,char map[MAX_PATH],int len,string& newbuffer)
{
	int i=0;
	char t;
	char code;
	int iCount = buffer.length();
	for (i=0;i<icount;i++)>
	{
		if(isalpha(buffer[i]))
		{
			if(islower(buffer[i]))
			{
				code='a';
			}
			else
			{
				code='A';
			}

		t=buffer[i]-code;
		t=t+map[i%len];
		t=t%26;
		t=t+code;
		newbuffer.push_back(t);
		}
		else
		{
		newbuffer.push_back(buffer[i]);
		}
	}
	newbuffer.push_back('\n');
}


void mapitE(string key,char map[MAX_PATH])
{
	int i;
	int iC = key.length();
	for (i=0;i<ic;i++)>
	map[i]=key[i]-'a';
}

void mapitD(string key,char map[MAX_PATH])
{
	int i;
	int iCnt = key.length();
	for (i=0;i<icnt;i++)>
   	map[i]=26-(key[i]-'a');
}
Posted
Updated 10-Oct-13 21:37pm
v2
Comments
CPallini 11-Oct-13 3:57am    
Why don't you use the debugger? It would help you (or, at least help us in order to help you...)

The debugger should have found your bug easily.
You should really explain the behaviour a bit more clearly.
I would use this test for end of file. It may be your problem.

C++
 getline(in,buffer);
//getline(in,sizeof(buffer));

while(!in.eof())
{
    encryptdecrypt(buffer,encryptmap,maplength,newbuffer);
    out &lt;&lt; newbuffer;
    newbuffer.erase(0);
    getline(in,buffer);
}



http://mathbits.com/MathBits/CompSci/Files/End.htm[^]
 
Share this answer
 
v2
Hello,

First put validation for all necessary place to validate the return value.

C#
int iVal = sData.ReverseFind(_T('.'));
if(iVal>0)
{
    iLen = iLen - iVal;
}
    sData = sData.Left(iVal);


etc...

and u can debug the code step by step u will come know, where exactly exe is crashing, then your problem will be resolved.
 
Share this answer
 
Comments
Rocky_Bas 11-Oct-13 5:52am    
application is crashing in this line

if(isalpha(buffer[i]))
[no name] 11-Oct-13 8:02am    
Well.....is i out of range?
Is this the actual code? There seem to be lots of errors in the post. You have to be precise.

int iCount = buffer.length();
for (i=0;i<icount;i++)>

Invest time in learning to use the debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900