gpt4 book ai didi

c - 解密功能无法按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:25 25 4
gpt4 key购买 nike

我正在学习基础加密/解密算法的类(class),但我被困在了这个实验室作业上。

我们得到了一个名为 simplecrypt.c 的 C 文件,该文件在运行时获取输入文件并将其加密为输出文件(输入保持不变)。我们的任务是在 simplecrypt.c 中也写一个解密函数,这样你就可以加密和解密你的文件,而不仅仅是加密它们。这就是我被困的地方。我不知道我是不是把它弄得太简单了,或者我是否在学习这门类(class)时把水淹没了,但如果有人能阐明我做错了什么,我将不胜感激。

这就是加密函数的样子。我相信这是 AES,如果我没记错的话?请注意,加密 key 已以纯文本形式提供给我们:

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

基本上,我所做的就是将 pInputBufpOutputBuf 反转,反之亦然。为什么这在理论上行得通却在实践中行不通?正如我所说,任何可以让我朝着正确方向前进的帮助都会很棒。

这是完整的代码:

#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;
}

最佳答案

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

关于c - 解密功能无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48849231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com