使用 C++ 进行加密(读取密文时出错)

发布于 2024-11-08 06:12:33 字数 2186 浏览 0 评论 0原文

这是我第一次使用这个论坛,我纯粹是出于绝望才这么做的。我应该编写一个利用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-11-15 06:12:33

这可能不是您的问题,但您的程序中至少有两个未定义的行为。您在 strres 的分配之外写入一个字节:

    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;

res[bytes2.size()] = 0;

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 for 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

Those assignments should be:

str[bytes.size()] = 0;

and

res[bytes2.size()] = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文