I am currently writing a simple little One-time pad program. I want it to warn the user when the keyfile
is smaller than the source
file, but when I run it my program simply ignores it.
/* Check if keyfile is the same size as, or bigger than the sourcefile */
if(sizeof keyfile < sizeof sourcefile)
{
printf("Source file is larger than keyfile.\n");
printf("This significantly reduces cryptographic strength.\n");
printf("Do you wish to continue? (Y/N)\n");
scanf("%c", &ans);
if(ans == 'n' || ans == 'N')
{
return (1);
}
if(ans == 'y' || ans == 'Y')
{
printf("Proceeding with Encryption/Decryption.\n");
}
I suspect the problem lies with my use sizeof
, but I don't know about any other options? Any suggestions as to how I can alter my code to make it work?
EDIT: I have tried adding fstat
to the best of my abilities, but it still reacts in the same manner... here is what I did:
/* Get size of sourcefile. */
if((sourcefile = fopen(argv[1], "rb"))== NULL)
{
printf("Can't open source file.\n");
printf("Please enter a valid filename.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return (1);
}
fflush(sourcefile);
fstat(fileno(sourcefile), &statbuf);
fclose(sourcefile);
/* Get size of keyfile. */
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");
return(1);
fflush(keyfile);
fstat(fileno(keyfile), &keybuf);
fclose(keyfile);
}
and altered the first line of the if statement being ignored:
if((keybuf.st_size) < (statbuf.st_size))
Why is it still being ignored by the program?
sizeof
does not return the size of a file. sizeof
is use to check the number of bytes a type takes in memory.
Look at this post if you want more information on getting a file's size.