gpt4 book ai didi

c - 长文件的RSA加密/解密

转载 作者:行者123 更新时间:2023-11-30 20:10:12 29 4
gpt4 key购买 nike

我想在 openssl/libcrypto 中使用 RSA 加密/解密一个长文件(我知道 AES 更好,但这只是为了比较)。我将输入文件分成大小为 numBlocks = inputFileLength/maxlen+1 的 block ,其中 maxlen = 200。我可以在同一循环中成功编码和解码,如下所示:

for (int i = 0; i < chunks; i++)
{
int bytesDone = i * maxlen;
int remainingLen = inLength - bytesDone;
int thisLen;

if (remainingLen > maxlen)
{
thisLen = maxlen;
} else
{
thisLen = remainingLen;
}
if((encBytes=RSA_public_encrypt(thisLen, data + bytesDone, encryptdata + bytesDone,
rsa_public, RSA_PKCS1_PADDING)) == -1)
{
printf("error\n");
}
if((decBytes=RSA_private_decrypt(encBytes, encryptdata + bytesDone, decryptdata + bytesDone,
rsa_private, RSA_PKCS1_PADDING)) == -1)
{
printf("error\n");
}
}

但是,我想将编码的缓冲区 encryptdata 保存在二进制文件中,读回二进制文件并解密。我尝试按如下方式执行此操作:

 for (int i = 0; i < chunks; i++)
{
int bytesDone = i * maxlen;
int remainingLen = inLength - bytesDone;
int thisLen;

if (remainingLen > maxlen)
{
thisLen = maxlen;
} else
{
thisLen = remainingLen;
}
if((encBytes=RSA_public_encrypt(thisLen, data + bytesDone, encryptdata + bytesDone,
rsa_public, RSA_PKCS1_PADDING)) == -1)
{
printf("error\n");
}

}

writeFile("encoded.bin",encryptdata,strlen(encryptdata));
size_t size;
unsigned char *readData = readFile("encoded.bin", size);
int inputlen = size;
for (int i = 0; (i * keylen) < inputlen; i++) //keylen = 256
{
int bytesDone = i * keylen;


if((decBytes=RSA_private_decrypt(encBytes, readData + bytesDone, decryptdata + bytesDone,
rsa_private, RSA_PKCS1_PADDING)) == -1)
{
printf("error\n");
}
}
printf("Decrypted text: %s",decryptdata);

readFilewriteFile函数如下:

void writeFile(char *filename, unsigned char *file, size_t fileLength
{

FILE *fd = fopen(filename, "wb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}

size_t bytesWritten = fwrite(file, 1, fileLength, fd);

if(bytesWritten != fileLength) {
fprintf(stderr, "Failed to write file\n");
exit(1);
}

fclose(fd);

}

unsigned char* readFile(char *filename, size_t size) {
FILE *fd = fopen(filename, "rb");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
}

// Determine size of the file
fseek(fd, 0, SEEK_END);
size_t fileLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
size = fileLength;
// Allocate space for the file
unsigned char* buffer = (unsigned char*)malloc(fileLength);

// Read the file into the buffer
size_t bytesRead = fread(buffer, 1, fileLength, fd);

if(bytesRead != fileLength) {
fprintf(stderr, "Error reading file\n");
exit(1);
}

fclose(fd);

return buffer;

}

但是,解密失败并显示错误消息段错误(核心转储),并且解密函数仅对每个 block 返回-1。任何帮助将不胜感激。

最佳答案

ReadFile修改了按值传递的参数“size”,因此当readfile函数返回时,size不受影响。

我将更改 readfile 原型(prototype)如下:

unsigned char* readFile(char *filename, size_t *size) 

然后将调用更改为

unsigned char *readData = readFile("encoded.bin", &size);

最后修改readFile大小更新为

size = fileLength;

关于c - 长文件的RSA加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47155694/

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