LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2007, 12:30 PM   #1
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
One Time


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;
}
Other suggestions / (c)crit welcome...
 
Old 11-02-2007, 01:04 PM   #2
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
It seems to err out (or eof) when 'cm == 0', in this line
Code:
while ( ((cm = getc(fim))) && ((ck = getc(fik)) != EOF) )
 
Old 11-02-2007, 02:01 PM   #3
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
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:

Code:
while ( ((cm = getc(fim))) && ((ck = getc(fik)) != EOF) )
You probably meant:
Code:
while( ((cm = getc(fim) != EOF) && (ck = getc(fik) != EOF))
These two lines are not equivalent.
 
Old 11-02-2007, 02:46 PM   #4
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
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);
}
However, I don't seem to be getting the 0 error?
 
Old 11-02-2007, 03:45 PM   #5
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
feof():
Return Value

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 didn't try it, but you might give it a run.

Last edited by rsashok; 11-02-2007 at 04:13 PM.
 
Old 11-02-2007, 04:08 PM   #6
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
time on our RH linux FTP server is four hours ahead, but desktop time is correct?? dgr Linux - Newbie 10 10-09-2007 05:42 AM
System time vs Hardware time and Daylight Savings Time Toadman Linux - General 6 03-17-2007 08:12 AM
System time vs Hardware time and Daylight Savings Time Toadman Linux - Networking 6 03-16-2007 07:14 PM
no signal when starting xorg for the 1st time (but the second time works fine) bungalowbill Linux - Software 0 06-04-2004 09:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration