积极答复者
c++读写文件的问题

问题
-
下面这两个源码一个是加密文件一个是解密文件,加密再解密以后总是不能完全和源文件相同,不知道是哪里代码有问题,自己找不到问题,
ifstream input;
char aa[33]="nmca";
char bb[300]="ttaaa";
input.open("777.txt",ios::binary);
ofstream output("888.txt",ios::trunc);
typedef char *(_stdcall *Connect)(char *aa,char *bb,int i);
HINSTANCE hinstDLL=NULL;
hinstDLL=LoadLibrary("crypto.dll");
if (hinstDLL)
{
Connect Proc;
Proc = (Connect)GetProcAddress (hinstDLL,"CryptMe");
if(input && output)
{
while(!input.eof())
{
memset(bb,0,300);
input.getline(bb,300);
AfxMessageBox(bb);
Proc(aa,bb,1);
output<<bb<<endl;
}
}FreeLibrary(hinstDLL);
这个是解密字符串的程序:
ifstream input;
char aa[33]="nmca";
char bb[300]="ttaaa";
input.open("888.txt",ios::binary);
ofstream output("999.txt",ios::trunc);typedef char *(_stdcall *Connect)(char *aa,char *bb,int i);
HINSTANCE hinstDLL=NULL;
hinstDLL=LoadLibrary("crypto.dll");
if (hinstDLL)
{
Connect Proc;
Proc = (Connect)GetProcAddress (hinstDLL,"CryptMe");
//int iValue = Proc(m_i);
if(input && output)
{
//string line;
while(!input.eof())
{
memset(bb,0,300);
input.getline(bb,300);
//strcpy(bb,"aa");
AfxMessageBox(bb);
Proc(aa,bb,2);
output<<bb<<endl;
}
}
//printf("%s",bb);
FreeLibrary(hinstDLL);
- 已编辑 Honny_yeyh 2012年7月25日 4:28
答案
-
因为你的文件每一行都是以\r\n结尾的所以加密程序应该改成
ifstream input;
char aa[33]="nmca";
char bb[300]="ttaaa";
input.open("777.txt",ios::binary);
ofstream output("888.txt",ios::trunc);
typedef char *(_stdcall *Connect)(char *aa,char *bb,int i);
HINSTANCE hinstDLL=NULL;
hinstDLL=LoadLibrary("crypto.dll");
if (hinstDLL)
{
Connect Proc;
Proc = (Connect)GetProcAddress (hinstDLL,"CryptMe");
if(input && output)
{
while(!input.eof())
{
memset(bb,0,300);
input.getline(bb,300,'\r');
AfxMessageBox(bb);
Proc(aa,bb,1);
output<<bb<<endl;
}
}FreeLibrary(hinstDLL);
麻烦把正确答案设为解答。
- 已标记为答案 Honny_yeyh 2012年7月27日 1:15
全部回复
-
源文件:
Serial-Number-for-combo; ed-Component;Green-Component;Blue-Component
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
ShowColorScaleGaussianRadius3 = $250000$
ShowColorScaleGaussianRadius4 = $200000$
ShowColorScaleGaussianRadius5 = $175000$
ShowColorScaleGaussianRadius6 = $150000$
ShowColorScaleGaussianRadius7 = $100000$解密文件:
<b
òØô97{▀e█╔ÒÁò½à×#¡gù_è╬&6Q¢*U4▀/▓f╗-Îô╣1»Ùzã&╦KƼuÁíiU`ÍÕÈàÿ
<b
òØô97{▀e█╔ÒÁò½à×#▀gù_è╬&6Q¢*U4▀/▓f╗-Îô╣1»Ùzã&╦KƼuÁíiU`ÍÕÈàÿ
<b
òØô97{▀e█╔ÒÁò½à×#▀gù_è╬&6Q¢*U4▀/▓f╗-Îô╣1»Ùzã&╦KƼuÁíiU`ÍÕÈàÿ
<b
òØô97{▀e█╔ÒÁò½à×#▀gù_è╬&6Q¢*U4▀/▓f╗-Îô╣1»Ùzã&╦KƼuÁíiU`ÍÕÈàÿ
<b
òØô97{▀e█╔ÒÁò½à×#▀gù_è╬&6Q¢*U4▀/▓f╗-Îô╣1»Ùzã&╦KƼuÁíiU`ÍÕÈàÿ
<b
òØô97{▀e█╔ÒÁò½à×#▀gù_è╬&6Q¢*U4▀/▓f╗-Îô╣1»Ùzã&╦KƼuÁíiU`ÍÕÈàÿ
<o0û▄▓>?6┴-·ÃõÙà¡ëúyÚkå·üvfßz pÈXõ
<o0û▄▓>?6┴-·ÃõÙà¡ëúyÚkå²üvfß pÈXõ
<o0û▄▓>?6┴-·ÃõÙà¡ëúyÚkå³üvfÔxpÈXõ
<o0û▄▓>?6┴-·ÃõÙà¡ëúyÚkå üvfÔz pÈXõ
<o0û▄▓>?6┴-·ÃõÙà¡ëúyÚkå■üvfÔ pÈXõ结果文件
Serial-Number-for-combo; ed-Component;Green-Component;Blue-Component
b
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
b
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
b
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
b
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
b
Serial-Number-for-combo;Red-Component;Green-Component;Blue-Component
b
ShowColorScaleGaussianRadius3 = $250000$
Ó
ShowColorScaleGaussianRadius4 = $200000$
Ó
ShowColorScaleGaussianRadius5 = $175000$
Ó
ShowColorScaleGaussianRadius6 = $150000$
Ó
ShowColorScaleGaussianRadius7 = $100000$
Ó
b -
可以看出: 单个字符串加密没有问题,文件GETLINE有问题,主要是结束符换行符号处理问题,不知道具体要怎么做才能处理这些字符. 我只是测试怎么加密和解密后跟源文档完全一致,学习函数用法
源码文件在 http://www.yycred.com/test.zip
TestDl1g.cpp是解密,TestDlg.cpp是加密- 已编辑 Honny_yeyh 2012年7月26日 4:24
-
因为你的文件每一行都是以\r\n结尾的所以加密程序应该改成
ifstream input;
char aa[33]="nmca";
char bb[300]="ttaaa";
input.open("777.txt",ios::binary);
ofstream output("888.txt",ios::trunc);
typedef char *(_stdcall *Connect)(char *aa,char *bb,int i);
HINSTANCE hinstDLL=NULL;
hinstDLL=LoadLibrary("crypto.dll");
if (hinstDLL)
{
Connect Proc;
Proc = (Connect)GetProcAddress (hinstDLL,"CryptMe");
if(input && output)
{
while(!input.eof())
{
memset(bb,0,300);
input.getline(bb,300,'\r');
AfxMessageBox(bb);
Proc(aa,bb,1);
output<<bb<<endl;
}
}FreeLibrary(hinstDLL);
麻烦把正确答案设为解答。
- 已标记为答案 Honny_yeyh 2012年7月27日 1:15
-