c - 如何使用“gfptrkey”变量打开名为“Key.data”的文件?
问题描述
这是全局变量,其中之一是 gfptrKey。
#include "Header.h"
// Global Variables
unsigned char gkey[65537];
unsigned char* gptrKey = gkey; // used for inline assembly routines, need to access this way for Visual Studio
char gPassword[256] = "SECRET";
unsigned char gPasswordHash[32];
unsigned char* gptrPasswordHash = gPasswordHash; // used for inline assembly routines, need to access this way for Visual Studio
unsigned char gdebug1, gdebug2;
FILE* gfptrIn = NULL;
FILE* gfptrOut = NULL;
FILE* gfptrKey = NULL;
char gInFileName[256];
char gOutFileName[256];
char gKeyFileName[256];
int gOp = 0; // 1 = encrypt, 2 = decrypt
int gNumRounds = 1;
已经具有打开主文件的输入文件和输出文件的功能。
FILE* openInputFile(char* filename)
{
FILE* fptr;
long sz;
fptr = fopen(filename, "rb");
if (fptr == NULL)
{
fprintf(stderr, "\n\nError - Could not open input file %s!\n\n", filename);
exit(-1);
}
fseek(fptr, 0, SEEK_END);
sz = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
if (sz == 0)
{
fprintf(stderr, "Error - File size is zero for \"%s\".\n\n", filename);
fprintf(stderr, "Aborting operation - try again!\n\n");
fclose(fptr);
exit(0);
}
return fptr;
} // openInputFile
FILE* openOutputFile(char* filename)
{
FILE* fptr;
fptr = fopen(filename, "wb+");
if (fptr == NULL)
{
fprintf(stderr, "\n\nError - Could not open output file %s!\n\n", filename);
exit(-1);
}
return fptr;
} // openOutputFile
如果出现错误,以下是其他 2 个功能:
void usage(char* argv[]) // cryptor.exe -e -i <input file> –k <keyfile> -p <password> [–r <#rounds>]
{
printf("\n\nUsage:\n\n");
printf("To Encrypt:\n");
printf("%s -e <message_filename> -k <keyfile> -p <password> [-r <#rounds>]\n\n", argv[0]);
printf("To Decrypt:\n");
printf("%s -d <message_filename> -k <keyfile> -p <password> [-r <#rounds>]\n\n", argv[0]);
printf("-e filename :encrypt the specified file\n");
printf("-d filename :decrypt the specified file\n");
printf("-p password :the password to be used for encryption [default='password']\n");
printf("-r <#rounds> :number of encryption rounds (1 - 3) [default = 1]\n");
printf("-o filename :name of the output file [default='encrypted.txt' or 'decrypted.txt'\n\n");
printf("The order of the options is irrelevant.\n\n");
exit(0);
} // usage
void parseCommandLine(int argc, char* argv[])
{
int cnt;
char ch;
bool i_flag, o_flag, k_flag, p_flag, err_flag;
i_flag = k_flag = false; // these must be true in order to exit this function
err_flag = p_flag = o_flag = false; // these will generate different actions
cnt = 1; // skip program name
while (cnt < argc)
{
ch = *argv[cnt];
if (ch != '-')
{
fprintf(stderr, "All options must be preceeded by a dash '-'\n\n");
usage(argv);
}
ch = *(argv[cnt] + 1);
if (0)
{
}
else if (ch == 'e' || ch == 'E')
{
if (i_flag == true || gOp != 0)
{
fprintf(stderr, "Error! Already specifed an input file - can't encrypt/decrypt multiple files.\n\n");
usage(argv);
}
i_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-e'\n\n");
usage(argv);
}
strncpy(gInFileName, argv[cnt], 256);
gOp = 1; // encrypt
}
else if (ch == 'd' || ch == 'D')
{
if (i_flag == true || gOp != 0)
{
fprintf(stderr, "Error! Already specifed an input file - can't decrypt/encrypt multiple files.\n\n");
usage(argv);
}
i_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-d'\n\n");
usage(argv);
}
strncpy(gInFileName, argv[cnt], 256);
gOp = 2; // decrypt
}
else if (ch == 'o' || ch == 'O')
{
if (o_flag == true)
{
fprintf(stderr, "Error! Already specifed an output file.\n\n");
usage(argv);
}
o_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-o'\n\n");
usage(argv);
}
strncpy(gOutFileName, argv[cnt], 256);
}
else if (ch == 'k' || ch == 'K')
{
if (k_flag == true)
{
fprintf(stderr, "Error! Already specifed a key file.\n\n");
usage(argv);
}
k_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-k'\n\n");
usage(argv);
}
strncpy(gKeyFileName, argv[cnt], 256);
}
else if (ch == 'p' || ch == 'P')
{
if (p_flag == true)
{
fprintf(stderr, "Error! Already specifed a password.\n\n");
usage(argv);
}
p_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must enter a password after '-p'\n\n");
usage(argv);
}
strncpy(gPassword, argv[cnt], 256);
}
else if (ch == 'r' || ch == 'R')
{
int x;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must enter number between 1 and 3 after '-r'\n\n");
usage(argv);
}
x = atoi(argv[cnt]);
if (x < 1 || x > 3)
{
fprintf(stderr, "Warning! Entered bad value for number of rounds. Setting it to one.\n\n");
x = 1;
}
gNumRounds = x;
}
else
{
fprintf(stderr, "Error! Illegal option in argument. %s\n\n", argv[cnt]);
usage(argv);
}
cnt++;
} // end while
if (gOp == 0)
{
fprintf(stderr, "Error! Encrypt or Decrypt must be specified.\n\n)");
err_flag = true;
}
if (i_flag == false)
{
fprintf(stderr, "Error! No input file specified.\n\n");
err_flag = true;
}
if (k_flag == false)
{
fprintf(stderr, "Error! No key file specified.\n\n");
err_flag = true;
}
if (p_flag == false)
{
fprintf(stderr, "Warning! Using default 'password'.\n\n");
}
if (o_flag == false && err_flag == false) // no need to do this if we have errors
{
strcpy(gOutFileName, gInFileName);
if (gOp == 1) // encrypt
{
strcat(gOutFileName, ".enc");
}
if (gOp == 2) // decrypt
{
strcat(gOutFileName, ".dec");
}
}
if (err_flag)
{
usage(argv);
}
return;
} // parseCommandLine
这是我试图弄清楚如何使用'gfptrKey'变量打开文件的主要功能,文件的名称是“Key.data”。
int main(int argc, char* argv[])
{
int length; // resulti;
// fprintf(stdout, "\n\nCrypto Order:%s\n\n", CRYPTO_ORDER);
// parse command line parameters
parseCommandLine(argc, argv); // sets global variables, checks input options for errors
// open the input and output files
gfptrIn = openInputFile(gInFileName); // reading the file
gfptrKey = openInputFile(gKeyFileName); // reading the keyfile data
gfptrOut = openOutputFile(gOutFileName); // writing the file
length = fread(gkey, 1, 65537, gfptrKey);
if (length != 65537)
{
fprintf(stderr, "Error! Length of key file is not at least 65537.\n\n");
exit(-1);
}
fclose(gfptrKey);
gfptrKey = NULL;
if (gOp == 1) // encrypt
{
encryptFile(gfptrIn, gfptrOut);
}
/*else
{
decryptFile(gfptrIn, gfptrOut);
}*/
fclose(gfptrIn);
fclose(gfptrOut);
return 0;
} // main
但是我需要的是通过使用 gfptrKey 变量而不是输出文件来输入“Key.data”的文件。
解决方案
推荐阅读
- c# - 如何在 GetFiles 中获取具有多个关键字的文件数
- azure - 加拿大中部地区是否可以使用名为“Microsoft Graph 数据连接”的新功能?
- r - 如何在 geom_points() 中细分一个类
- batch-file - 从 Windows 快捷方式文件编码 cmd.exe
- git - 提交到镜像仓库的 Git 镜像
- laravel - laravel : 否则内容为空
- flutter - 如何从 ChangeNotifier 导航?
- javascript - 子路由在延迟加载中不起作用
- html - 如何将 div 和 iframe 放在中心?
- laravel - 在 Elastic Beanstalk CLI 中使用 ebextensions 文件启用 mod_rewrite