C - 在RAM中运行程序

时间:2012-10-16 15:03:03

标签: c file ram hard-drive

我有一个程序,使用一次性密码加密将两个文件混合在一起。由于密钥文件具有如此敏感的特性,我不希望密钥文件的任何痕迹出现在计算机硬盘驱动器上,因为这可能会危及安全性。

问题是,如何在RAM中运行程序以避免在HD上留下任何痕迹?或者,从闪存驱动器运行程序是否包含闪存驱动器的密钥文件的痕迹?

以下是程序中如何处理密钥文件:

/* Check if keyfile can be opened. */
if((keyfile = fopen(argv[3], "rb"))== NULL)
{
printf("Can't open keyfile.\n");
printf("Please enter a valid filename.\n"); 
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
perror("Error");
return(1);
}                               

/* Get size of keyfile */
fstat(fileno(keyfile), &keybuf);

/* Check if keyfile is the same size as, or bigger than the sourcefile */
if((keybuf.st_size) < (statbuf.st_size))
{
printf("Source file is larger than keyfile.\n");
printf("This significantly reduces cryptographic strength.\n");
printf("Do you wish to continue? (Y/N)\n");
fgets(buffer, 20, stdin);
sscanf(buffer, "%c", &ans);
if(ans == 'n' || ans == 'N')
{
return (1);
}
if(ans == 'y' || ans == 'Y')
{
    printf("Proceeding with Encryption/Decryption.\n");
    }

/* Encrypt/Decrypt and write to output file. */
while(count < (statbuf.st_size))
{
key=fgetc(keyfile);
data=fgetc(sourcefile);

output=(key^data);

fputc(output,destfile);
count++;
}

/* Close files. */
fclose(keyfile);
fclose(sourcefile);
fclose(destfile); 

我在Google搜索时遇到inram函数,但这似乎不是我需要的。

1 个答案:

答案 0 :(得分:4)

我假设您正在从某些外部媒体读取密钥文件,并且您担心将进程与包含OTP的I / O缓冲区交换到磁盘。您可能同样关注正在编写的明文。如果您使用的是posix系统(如linux),那么您应该查看mlockmlockall函数。这些调用会将内存页锁定到RAM中并禁止它们交换到磁盘。手册页专门为这些调用调出安全用例。另一种选择可能是mmap文件。虽然它没有相同的保证,但由于映射的页面将由外部媒体支持,我怀疑它们会出现在交换空间中。