gpt4 book ai didi

c - 使用 libgcrypt 使用 aes256-cbc 在 C 中加密和解密文件的问题

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

prova 是一个纯文本文件,其中包含hello i am a pc

加密:

FILE *fp = fopen("prova", "r+");
FILE *fpout = fopen("out", "w+");
while(!feof(fp)){
memset(plain_text, 0, sizeof(plain_text));
retval = fread(plain_text, 1, 16, fp);
txtLenght = sizeof(plain_text);
encBuffer = malloc(txtLenght);
algo = gcry_cipher_map_name(name);
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
gcry_cipher_encrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
fwrite(encBuffer, 1, 16, fpout);
}

解密:

FILE *fp = fopen("out", "r+");
FILE *fpout = fopen("origdec", "w+");
while(!feof(fp)){
memset(plain_text, 0, sizeof(plain_text));
retval = fread(plain_text, 1, 16, fp);
txtLenght = sizeof(plain_text);
encBuffer = malloc(txtLenght);
algo = gcry_cipher_map_name(name);
gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);
gcry_cipher_decrypt(hd, encBuffer, txtLenght, plain_text, txtLenght);
fwrite(encBuffer, 1, 16, fpout);
}

地点:

char key[32] = {0x80};
char iniVector[16] = {0};
char plain_text[16];
char *encBuffer = NULL;

问题:当我破译加密文件时,origdec 文件包含纯文本以及一些随机无用且不可读的字符。

最佳答案

当你读取你的字符串时,

fread(plain_text, 1, 16, fp);

您可能会得到“你好,我是一台 PC”(15 个字节)加上一个返回。

然后你加密 16 个字节,解密这 16 个字节,你仍然没有字符串终止符,因此 printf 写入额外的东西(或者可能是核心转储)。

您需要在字符串末尾添加一个二进制零。尝试:

fread(plain_text, 1, 16, fp);
plain_text[15] = 0x0;

看看这是否改变了什么。

更新

您的代码中有几个错误。例如,您总是重新初始化密码,并且总是重新分配 encBuffer,从而导致内存泄漏。我已经纠正了一些错误;仍然存在将加密文件填充到 16 字节的功能。有一些技术可以去除填充;例如,您可能想看看 PKCS#7。

我已经任意初始化了一些常量,并采用了就地加密/解密(您不限于 bufSize 为 16;但您将不得不检查填充策略)。

#include <stdio.h>
#include <gcrypt.h>

int main()
{
char iniVector[16];
char *encBuffer = NULL;
FILE *fp, *fpout;
char *key = "topolino e minni";
gcry_cipher_hd_t hd;
int bufSize = 16, bytes, algo = GCRY_CIPHER_AES128, keyLength = 16, blkLength = 16;

memset(iniVector, 0, 16);

encBuffer = malloc(bufSize);

fp = fopen("prova", "r");
fpout = fopen("out", "w");

gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);

while(!feof(fp))
{
bytes = fread(encBuffer, 1, bufSize, fp);
if (!bytes) break;
while(bytes < bufSize)
encBuffer[bytes++] = 0x0;
gcry_cipher_encrypt(hd, encBuffer, bufSize, NULL, 0);
bytes = fwrite(encBuffer, 1, bufSize, fpout);
}
gcry_cipher_close(hd);
fclose(fp);
fclose(fpout);

// Decrypt. Same algo as before

gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
gcry_cipher_setkey(hd, key, keyLength);
gcry_cipher_setiv(hd, iniVector, blkLength);

fp = fopen("out", "r");
fpout = fopen("origdec", "w");
while(!feof(fp))
{
bytes = fread(encBuffer, 1, bufSize, fp);
if (!bytes) break;
gcry_cipher_decrypt(hd, encBuffer, bufSize, NULL, 0);
bytes = fwrite(encBuffer, 1, bufSize, fpout);
}
gcry_cipher_close(hd);

free(encBuffer); encBuffer = NULL;
return 0;
}

关于c - 使用 libgcrypt 使用 aes256-cbc 在 C 中加密和解密文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14174486/

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