ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First off, sorry for the size (but it's complete(ish)!). The problem is that sometimes it cut's out decrypting, presumably on an EOF (either -1 or 255), I thought it could only be coming from the encrypted msg (out.txt) file so I took the catch off, but to no avail, therefore how to handle?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void rand_init()
{
long ltime;
unsigned int itime;
ltime = time(NULL);
itime = (unsigned) ltime/2; // ? mod max of int?
srand(itime);
}
unsigned int rand_gen()
{
while(1)
{
unsigned int n = abs(rand() % 255);
if(n != EOF) { return n; }
}
}
int gen_random_file(char * fname, int size)
{
FILE *fo;
int i;
rand_init();
if ((fo = fopen(fname, "wb")) != NULL)
{
for(i=0;i<size;i++)
{
unsigned int ic = rand_gen();
char cc = (char)ic;
putc(cc, fo);
}
fclose(fo);
}
}
int filesize(char * fname)
{
struct stat stbuf;
if(stat(fname, &stbuf) == -1)
{
return -1;
}
return stbuf.st_size;
}
int encrypt(char * fname_key, char * fname_msg, char * fname_out, int mode)
{
FILE *fim, *fik, *fo;
char cm, ck;
int n;
int i = 0;
if ((fim = fopen(fname_msg, "rb")) != NULL)
{
if ((fik = fopen(fname_key, "rb")) != NULL)
{
if ((fo = fopen(fname_out, "wb")) != NULL)
{
//while ( ((cm = getc(fim)) != EOF) && ((ck = getc(fik)) != EOF) )
while ( ((cm = getc(fim))) && ((ck = getc(fik)) != EOF) )
{
if(mode)
{
n = ( ((unsigned int)((unsigned char)cm)) + ((unsigned int)((unsigned char)ck)) ) % 256;
}
else
{
n = ( ((unsigned int)((unsigned char)cm)) - ((unsigned int)((unsigned char)ck)) ) % 256;
}
putc((char)n, fo);
i++;
}
fclose(fo);
}
fclose(fik);
}
fclose(fim);
}
printf("i: %d \n", i);
return 0;
}
int main(int argc, char *argv[])
{
char *fname_msg = "msg.txt";
char *fname_key = "key.txt";
char *fname_out = "out.txt";
int len = filesize(fname_msg);
printf("len: %d \n", len);
// GENERATE A NEW KEY
int iret = gen_random_file(fname_key, len); // char * fname, int size
// ENCRYPT
iret = encrypt(fname_key, fname_msg, fname_out, 1); // char * fname_key, char * fname_msg, char * fname_out
// DECRYPT
iret = encrypt(fname_key, fname_out, "dec.txt", 0); // char * fname_key, char * fname_msg, char * fname_out
return 0;
}
I'd recommend not to use getc() for reading binary streams. Use something else, for example read(). If you want to use getc(), then test for the end of file with feof(), don't compare with EOF. There is a logical problem in the line:
Yes, if you see first code that same line is commented out.
I've now changed char's to ints and use the 'f' variations, and now am trying feof(), however it always seems to go over by 1? This is how the loop looks now:
Code:
while( (feof(fim)==0) && (feof(fik)==0) )
{
cm = fgetc(fim);
ck = fgetc(fik);
if(mode)
n = (cm + ck ) % 256;
else
n = (cm - ck ) % 256;
putc(n, fo);
}
The feof function returns a nonzero value after the first read operation tht attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.
Do something like this:
Code:
int buf_im[1024], buf_ik[1024], i,im_count, ik_count, lim;
while( !feof(fim) && !feof(fik) )
{
im_count = read(buf_im, sizeof(char), 1024, fim);
ik_count = read(buf_ik, sizeof(char), 1024, fik);
lim = min(im_count, ik_count);
for(i = 0; i < lim; i++)
{
cm = buf_im[i];
ck = buf_ik[i];
cm = (mode) ? (cm + ck) : (cm - ck);
n = cm & 0x0ff; /* this is the same as n % 256, but faster */
putc(n, fo);
}
}
I tried that first, it also overruns... I've gone back to:
Code:
while ( ((cm = fgetc(fim)) != EOF) && ((ck = fgetc(fik)) != EOF) )
{
if(mode)
n = (cm + ck ) % 256;
else
n = (cm - ck ) % 256;
putc(n, fo);
}
I'm not sure if it was and int / char thing, however from over 100 runs i've had no errors. Later i'll devise a way of setting the key and conjuring the error and see if it still invokes it?
Cheers!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.