C++ 使用C++;(读取密文时出错)
这是我第一次使用这个论坛,我这么做纯粹是出于绝望。我应该写一个利用Des算法的加密程序。我成功地将Des代码写得非常好,并且在内存中没有任何错误。但是当我必须从文件中读取密文然后解密时,我的麻烦就开始了。它有时有效,有时无效。我尝试了很多方法来解决这个问题,但都没有成功。这是main的代码,没有Des代码本身。请帮帮我C++ 使用C++;(读取密文时出错),c++,encryption,C++,Encryption,这是我第一次使用这个论坛,我这么做纯粹是出于绝望。我应该写一个利用Des算法的加密程序。我成功地将Des代码写得非常好,并且在内存中没有任何错误。但是当我必须从文件中读取密文然后解密时,我的麻烦就开始了。它有时有效,有时无效。我尝试了很多方法来解决这个问题,但都没有成功。这是main的代码,没有Des代码本身。请帮帮我 int main() { Des d1,d2; char *str=new char[1000]; char *str1=new char[1000];
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;
}
intmain()
{
Des d1、d2;
char*str=新字符[1000];
char*str1=新字符[1000];
字符c;
ifstream*密钥文件;
流密码文件;
keyFile=新的ifstream;
keyFile->open(“key.txt”,ifstream::binary);
二进制文件(keyFile);
cipherFile.open(“ciphertext.dat”,of stream::binary);
std::ifstream纯文件(“plaintext.txt”,ifstream::binary);
seekg(0,std::ios::end);
std::ifstream::pos_type filesize=plainFile.tellg();
seekg(0,std::ios::beg);
std::向量字节(文件大小);
读取(&字节[0],文件大小);
str=new char[bytes.size()+1];//+1表示最后的0
str[bytes.size()+1]='\0';//终止字符串
对于(size_t i=0;i 对于(int i=0;i这可能不是您的问题,但您的程序中至少有两个未定义的行为。您在str
和res
的分配之外写入了一个字节:
str = new char[bytes.size() + 1]; // +1 for the final 0
str[bytes.size() + 1] = '\0'; // terminate the string
...
res = new char[bytes2.size() + 1]; // +1 for the final 0
res[bytes2.size() + 1] = '\0'; // terminate the string
这些任务应该是:
str[bytes.size()] = 0;
及
定义“不起作用”。它会崩溃吗?它会发出错误消息吗?它会给出错误的结果吗?您是否尝试过使用调试器单步执行程序以查看出错的地方?只是一种预感,因为您几乎没有提供有关该问题的信息。您的文件中可能包含一些控制字符,如回车符、换行符等,您没有遇到过这些字符r内存测试。为什么你要用new
?sry没有详细说明错误。我没有得到具体的错误,只是垃圾字符而不是解密的文本。我确实对控制字符有同样的预感,因为文件大小总是比它应该的长2个字符。如何修复它?出于我的绝望我开始用新的想法重新分配变量,可能是因为它导致了错误。我现在明白了,它不是必需的。谢谢你的帮助,但这对输出没有任何影响。我仍然得到垃圾字符。
res[bytes2.size()] = 0;