#include "Header.h"
unsigned char gkey[65537];
unsigned char* gptrKey = gkey; char gPassword[256] = "SECRET";
unsigned char gPasswordHash[32];
unsigned char* gptrPasswordHash = gPasswordHash; 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; int gNumRounds = 1;
int encryptData(char* data, int dataLength)
{
int resulti = 0;
gdebug1 = 0; gdebug2 = 0;
__asm {
mov esi, gptrKey
mov esi, gptrPasswordHash
xor eax, eax
mov al, byte ptr[esi]
shl ax, 8
xor ecx, ecx
mov cl, byte ptr[esi + 1]
add ax, cx
xor ebx, ebx
xor ecx, ecx
mov ecx, dataLength
mov edi, data
LOOP1 :
mov dl, byte ptr[edi + ebx]
xor dl, byte ptr[esi + eax]
mov byte ptr[edi + ebx], dl
add ebx, 1
cmp ebx, ecx
mov ebx, 2
ja EXIT_END
jmp LOOP1
EXIT_END :
xor ebx, ebx
mov resulti, ebx
}
return resulti;
}
int encryptFile(FILE* fptrIn, FILE* fptrOut)
{
char* buffer;
unsigned int filesize;
fseek(fptrIn, 0, SEEK_END);
filesize = ftell(fptrIn);
fseek(fptrIn, 0, SEEK_SET);
if (filesize > 0x1000000) {
fprintf(stderr, "Error - Input file to encrypt is too large.\n\n");
return -1;
}
buffer = (char*)malloc(filesize);
if (buffer == NULL)
{
fprintf(stderr, "Error - Could not allocate %d bytes of memory on the heap.\n\n", filesize);
return -1;
}
fread(buffer, 1, filesize, fptrIn); encryptData(buffer, filesize);
fwrite(buffer, 1, filesize, fptrOut);
free(buffer);
return 0;
}
I already have functions to open the input file and output file:
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;
}
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;
}
void usage(char* argv[]) {
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);
}
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; err_flag = p_flag = o_flag = false;
cnt = 1; 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; }
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; }
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++;
}
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) {
strcpy(gOutFileName, gInFileName);
if (gOp == 1) {
strcat(gOutFileName, ".enc");
}
if (gOp == 2) {
strcat(gOutFileName, ".dec");
}
}
if (err_flag)
{
usage(argv);
}
return;
}
And here's the main function:
int main(int argc, char* argv[])
{
int length;
parseCommandLine(argc, argv);
gfptrIn = openInputFile(gInFileName); gfptrKey = openInputFile(gKeyFileName); gfptrOut = openOutputFile(gOutFileName);
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) {
encryptFile(gfptrIn, gfptrOut);
}
fclose(gfptrIn);
fclose(gfptrOut);
return 0;
}
What I have tried:
I was thinking of using this code instead:
void fileStart(){
FILE *mainFile;
char name[] = "file.txt";
fileOpen(mainFile, name);
}
but my teacher sent me the codes to use it, and I don't know which way to put the file name ("Key.data") for gfptKey variable.