使用 C++ 进行加密(读取密文时出错)
这是我第一次使用这个论坛,我纯粹是出于绝望才这么做的。我应该编写一个利用 Des 算法的加密程序。我成功地完美地编写了 Des 代码,并且它运行时没有任何内存错误。但是当我必须从文件中读取密文然后解密它时,我的麻烦就开始了。它有时有效,有时则无效。我尝试了很多方法来解决这个问题,但没有达到任何目的。这是没有 Des 代码本身的 main 代码。请帮我
int main()
{
Des d1,d2;
char *str=new char[1000];
char *str1=new char[1000];
char c;
ifstream *keyFile;
ofstream cipherFile ;
keyFile= new ifstream;
keyFile->open ( "key.txt", ifstream::binary) ;
binaryToHexa (keyFile);
cipherFile.open ( "ciphertext.dat", ofstream::binary) ;
std::ifstream plainFile("plaintext.txt", ifstream::binary);
plainFile.seekg(0, std::ios::end);
std::ifstream::pos_type filesize = plainFile.tellg();
plainFile.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
plainFile.read(&bytes[0], filesize);
str = new char[bytes.size() + 1]; // +1 for the final 0
str[bytes.size() + 1] = '\0'; // terminate the string
for (size_t i = 0; i < bytes.size(); ++i)
{
str[i] = bytes[i];
}
char *temp=new char[9];
for(int i=0; i<filesize; i+=8)
{
for(int j=0; j<8; j++)
{
temp[j]=str[i+j];
}
temp[8]='\0';
str1=d1.Encrypt(temp);
cipherFile<<str1;
//cout<<d2.Decrypt(str1);
}
cipherFile.close();
plainFile.close();
std::ifstream cipherFileInput("ciphertext.dat", std::ios::binary);
cipherFileInput.seekg(0, std::ios::end);
std::ifstream::pos_type filesize2 = cipherFileInput.tellg();
cipherFileInput.seekg(0, std::ios::beg);
std::vector<char> bytes2(filesize2);
char* res;
cipherFileInput.read(&bytes2[0], filesize2);
res = new char[bytes2.size() + 1]; // +1 for the final 0
res[bytes2.size() + 1] = '\0'; // terminate the string
for (size_t i = 0; i < bytes2.size(); ++i)
{
res[i] = bytes2[i];
}
char *temp2=new char[9];
for(int i=0; i<filesize2; i+=8)
{
for(int j=0; j<8; j++)
{
temp2[j]=res[i+j];
}
temp2[8]='\0';
str1=d2.Decrypt(temp2);
cout<<str1;
}
return 1;
}
this is the first time I use this forum and I did it out of pure desperation. I am supposed to write an encryption program that utilizes Des algorythm. I managed to write the Des code perfectly well and it works without any errors from the memory. But my trouble starts when I have to read the ciphertext from a file and then decrypt it. It works sometimes and others not. I tried so many ways to work around the problem but reached no where. Here is the code of the main without the Des code itself. Please help me
int main()
{
Des d1,d2;
char *str=new char[1000];
char *str1=new char[1000];
char c;
ifstream *keyFile;
ofstream cipherFile ;
keyFile= new ifstream;
keyFile->open ( "key.txt", ifstream::binary) ;
binaryToHexa (keyFile);
cipherFile.open ( "ciphertext.dat", ofstream::binary) ;
std::ifstream plainFile("plaintext.txt", ifstream::binary);
plainFile.seekg(0, std::ios::end);
std::ifstream::pos_type filesize = plainFile.tellg();
plainFile.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
plainFile.read(&bytes[0], filesize);
str = new char[bytes.size() + 1]; // +1 for the final 0
str[bytes.size() + 1] = '\0'; // terminate the string
for (size_t i = 0; i < bytes.size(); ++i)
{
str[i] = bytes[i];
}
char *temp=new char[9];
for(int i=0; i<filesize; i+=8)
{
for(int j=0; j<8; j++)
{
temp[j]=str[i+j];
}
temp[8]='\0';
str1=d1.Encrypt(temp);
cipherFile<<str1;
//cout<<d2.Decrypt(str1);
}
cipherFile.close();
plainFile.close();
std::ifstream cipherFileInput("ciphertext.dat", std::ios::binary);
cipherFileInput.seekg(0, std::ios::end);
std::ifstream::pos_type filesize2 = cipherFileInput.tellg();
cipherFileInput.seekg(0, std::ios::beg);
std::vector<char> bytes2(filesize2);
char* res;
cipherFileInput.read(&bytes2[0], filesize2);
res = new char[bytes2.size() + 1]; // +1 for the final 0
res[bytes2.size() + 1] = '\0'; // terminate the string
for (size_t i = 0; i < bytes2.size(); ++i)
{
res[i] = bytes2[i];
}
char *temp2=new char[9];
for(int i=0; i<filesize2; i+=8)
{
for(int j=0; j<8; j++)
{
temp2[j]=res[i+j];
}
temp2[8]='\0';
str1=d2.Decrypt(temp2);
cout<<str1;
}
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不是您的问题,但您的程序中至少有两个未定义的行为。您在
str
和res
的分配之外写入一个字节:这些分配应该是
:
This may not be your problem, but you have at least two undefined behaviors in your program. You write one byte beyond the allocation for
str
and forres
:Those assignments should be:
and