gpt4 book ai didi

c - 如何使用c解密内存中的大文件

转载 作者:行者123 更新时间:2023-11-30 19:46:49 26 4
gpt4 key购买 nike

基于 C 的应用程序使用 key 大小为 256 的 aes。数据以二进制形式提供,经过加密并写入二进制文件中。要求是解密 RAM 中的这个二进制文件(即动态/实时加密)。问题是如何高效地实现动态加密?需要任何好的网络链接或代码引用来理解动态加密。

更简单的问题是如何使用c(Linux)解密内存中的大文件?就像在 truecrypt 中一样。

最佳答案

对文件使用mmap;然后该文件作为内存中的数据流打开。例如,一个简单的内存更改函数,可对大型(例如 400Gb)文件中的每个字节进行异或:

// The encryption function
void xor_ram (unsigned char *buffer, long len) {
while (len--) *buffer ^= *buffer++;
}

// The file we want to encrypt
int fd = open ("/path/to/file", O_RDWR);

// Figure out the file length
FILE *tmpf = fdopen (fd, "r");
fseek (tmpf, 0, SEEK_END);
long length = ftell (tmpf);

// Memory map the file using the fd
unsigned char *mapped_file = mmap (NULL, length,
PROT_READ | PROT_WRITE, MAP_PRIVATE,
fd, 0);
// Call the encryption function
xor_ram (mapped_file, length);

// All done now
munmap (mapped_file, length);
close (fd);

您可以在此处阅读 mmap 的联机帮助页:http://unixhelp.ed.ac.uk/CGI/man-cgi?mmap尽管您确实应该在您的特定平台上找到 mmap 的文档(如果您使用的是某种 UNIX 系统,则使用 man mmap,如果没有,则搜索平台库)。

关于c - 如何使用c解密内存中的大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23125964/

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