解密功能不按预期工作

时间:2018-02-18 06:59:23

标签: c encryption

我参加了基础加密/解密算法课程,并且我坚持完成这一个实验任务。

我们给出了一个名为simplecrypt.c的C文件,该文件在运行时会获取一个输入文件并将其加密为输出文件(输入保持不变)。我们的任务是在simplecrypt.c中编写解密函数,以便您可以加密和解密文件,而不仅仅是加密它们。这就是我被困的地方。我不知道我是不是太简单了,或者我用这个课程把水弄到了头上,但是如果有人能说清楚我做错了什么,我就是这样做的。我非常感激。

这就是加密功能的样子。我相信它的AES,如果我没弄错的话?请注意,加密密钥已以纯文本形式提供给我们:

int crypt(unsigned char *key,
             unsigned char *pInputBuf,
             unsigned char *pOutputBuf,
             long nLength)
{
    int nKeyPos = 0;
    long n;
    unsigned char KeyA = 0;

    if((pInputBuf != NULL) && (pOutputBuf != NULL))
    {
        for(n = 0; n < KEY_BUF; n++)
            KeyA ^= key[n];

        nKeyPos = KeyA%KEY_BUF;

        for(n = 0; n < nLength; n++)
        {
            pOutputBuf[n] = pInputBuf[n]^(key[nKeyPos]*KeyA);
            KeyA += pOutputBuf[n];
            nKeyPos = pOutputBuf[n]%KEY_BUF;
        }
        return 0;   // success
    }
    return 1;
}

这就是我所做的解密:

int decrypt(unsigned char *key,
             unsigned char *pInputBuf,
             unsigned char *pOutputBuf,
             long nLength)
{
    int nKeyPos = 0;
    long n;
    unsigned char KeyA = 0;

    if((pInputBuf != NULL) && (pOutputBuf != NULL))
    {
        for(n = 0; n < KEY_BUF; n++)
            KeyA ^= key[n];

        nKeyPos = KeyA%KEY_BUF;

        for(n = 0; n < nLength; n++)
        {
            // This is where I made changes.
            pInputBuf[n] = pOutputBuf[n]^(key[nKeyPos]*KeyA);
            KeyA += pInputBuf[n];
            nKeyPos = pInputBuf[n]%KEY_BUF;
        }
        return 0;   // success
    }
    return 1;
}

基本上,我所做的就是用pInputBuf反转pOutputBuf,反之亦然。当它在理论上有效时,为什么这在实践中不起作用?正如我所说,任何可以让我朝着正确方向前进的帮助都会很棒。

这是完整的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KEY_BUF 20
#define FNAME_BUF 256


void stripnl(char *str) 
{
    while(strlen(str) && ((str[strlen(str) - 1] == 13) || (str[strlen(str) - 1] == 10))) 
    {
        str[strlen(str) - 1] = 0;
    }
}


long writefile(unsigned char *pOutputBuf, long size) 
{
    FILE *outfile;
    char fname[FNAME_BUF];
    unsigned char *p = pOutputBuf;
    long i=0;

    // Read in the filename
    printf("Enter the name of the output file: ");
    fgets(fname, sizeof(fname), stdin);

    // We need to get rid of the newline char
    stripnl(fname);

    // Open the file. If NULL is returned there was an error, corrected to VS2012 standard
    // if((outfile = fopen(fname, "wb")) == NULL) 
    if((fopen_s(&outfile, fname, "wb")) != 0) 
    {
        printf("Error Opening File.\n");
        return(0);
    }

    // Put characters in outfile
    while( (i < size) && (fputc(*(p++), outfile) != EOF) )
        i++;

    fclose(outfile);  // Close the file
    return i;
}


char* readfile(unsigned char *pInutBuf, long *nLength) 
{
    FILE *infile;
    char fname[FNAME_BUF];
    int ch=0;
    long i;

    // Read in the filename
    printf("Enter the name of the input file: ");
    fgets(fname, sizeof(fname), stdin);

    // We need to get rid of the newline char
    stripnl(fname);

    // Open the file. If anything else than 0 is returned there was an error
    if((fopen_s(&infile, fname, "rb")) != 0) 
    {
        printf("Error Opening File.\n");
        return(0);
    }

    // get file size
    fseek(infile, 0L, SEEK_END);
    *nLength = ftell(infile);
    // rewind
    fseek(infile, 0L, SEEK_SET);

    pInutBuf = (unsigned char *) malloc(*nLength+1);

    // Read in characters and place them in "buffer"
    ch = fgetc(infile);
    for(i=0; (i < *nLength) && (feof(infile) == 0); i++)
    {
        pInutBuf[i] = (unsigned char)ch;
        ch = fgetc(infile);
    }

    // end of buffer
    pInutBuf[i] = '\0';

    fclose(infile);
    return pInutBuf;
}


int crypt(unsigned char *key,
             unsigned char *pInputBuf,
             unsigned char *pOutputBuf,
             long nLength)
{
    int nKeyPos = 0;
    long n;
    unsigned char KeyA = 0;

    if((pInputBuf != NULL) && (pOutputBuf != NULL))
    {
        for(n = 0; n < KEY_BUF; n++)
            KeyA ^= key[n];

        nKeyPos = KeyA%KEY_BUF;

        for(n = 0; n < nLength; n++)
        {
            pOutputBuf[n] = pInputBuf[n]^(key[nKeyPos]*KeyA);
            KeyA += pOutputBuf[n];
            nKeyPos = pOutputBuf[n]%KEY_BUF;
        }
        return 0;   // success
    }
    return 1;
}

int decrypt(unsigned char *key,
     unsigned char *pOutputBuf,
     unsigned char *pInputBuf,
     long nLength)
{
     int nKeyPos = 0;
     long n;
     unsigned char KeyA = 0;

     if ((pInputBuf != NULL) && (pOutputBuf != NULL))
     {
          for (n = 0; n < KEY_BUF; n++)
               KeyA ^= key[n];

          nKeyPos = KeyA%KEY_BUF;

          for (n = 0; n < nLength; n++)
          {
               pInputBuf[n] = pOutputBuf[n] ^ (key[nKeyPos] * KeyA);
               KeyA += pInputBuf[n];
               nKeyPos = pInputBuf[n] % KEY_BUF;
          }
          return 0; // success
     }
     return 1;
}


int main(int argc, char *argv[])
{
    // KEY_BUF+1 corrected to VS2012 standard, we never use the last +1 ('\0') uchar
    unsigned char key[KEY_BUF+1]="abcdefghijklmnopqrst";
    unsigned char *pInputBuf=NULL;
    unsigned char *pOutputBuf=NULL;
    long nLength=0, n;

    if((pInputBuf = readfile(pInputBuf, &nLength)) == NULL)
        return 1;

    pOutputBuf = (unsigned char *) malloc(nLength);

    //if(crypt(key, pInputBuf, pOutputBuf, nLength) != 0)
        //return 2;

    if(decrypt(key, pOutputBuf, pInputBuf, nLength) != 0)
        return 2;

    if(writefile(pOutputBuf, nLength) == 0)
        return 3;
/*
    for(n = 0; n < nLength; n++)
        printf("%c", pInputBuf[n]);
    printf("\n");

    for(n = 0; n < nLength; n++)
        printf("%X", pOutputBuf[n]);
    printf("\n");
*/
    // Free up memory
    if(pInputBuf)
        free(pInputBuf);
    if(pOutputBuf)
        free(pOutputBuf);

    getchar(); // pause and wait for key
    return 0;
}

1 个答案:

答案 0 :(得分:4)

crypt()中,您可以根据加密密文修改KeyA / nKeyPos。 在decrypt()中,您可以根据解密纯文本更改KeyA / nKeyPos。

试试这个:

int crypt( unsigned char *key,
           const unsigned char *pPlaintext, //input
           unsigned char *pCiphertext,      //output
           long nLength)
{
    ...
    for(n = 0; n < nLength; n++)
    {
        pCiphertext[n] = pPlaintext[n] ^ (key[nKeyPos]*KeyA);
        KeyA += pCiphertext[n];
        nKeyPos = pCiphertext[n] % KEY_BUF;
    }
}

int decrypt( unsigned char *key,
             const unsigned char *pCiphertext, //input
             unsigned char *pPlaintext,        //output
             long nLength)
{
    ...
    for(n = 0; n < nLength; n++)
    {
        pPlaintext[n] = pCiphertext[n] ^ (key[nKeyPos]*KeyA);
        KeyA += pCiphertext[n];
        nKeyPos = pCiphertext[n] % KEY_BUF;
    }
}