开发者

encryption using C++ (errors reading ciphertext)

开发者 https://www.devze.com 2023-03-07 13:45 出处:网络
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 a

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 ciphe开发者_如何转开发rtext 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;
}


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消