加密文件中的数据时出现问题 [英] Problem in Encrypting the data in the file

查看:93
本文介绍了加密文件中的数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我开发了一个应用程序,我必须加载文件并加密文件中的数据。如果我加载了文件3到5行数据,然后文件正在加密,但如果文件中的数据超过一页,那么我的应用程序在加密时崩溃.Below是我的代码,可以帮助我解决这个问题。谢谢你提前



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

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');
}

推荐答案

调试器应该很容易找到你的bug。

你应该真的解释一下行为更清楚。

我会将此测试用于文件结尾。这可能是你的问题。



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.

 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 [ ^ ]


你好,



首先验证所有必要的地方以验证返回值。



Hello,

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

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





等...



你可以逐步调试代码你会知道,exe确切地崩溃了,那么你的问题就会得到解决。



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.


这篇关于加密文件中的数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆