
-----------------------------------
Tallguy
Fri Nov 18, 2011 2:00 pm

Encryption
-----------------------------------
hello, hello

So what i need help is - we are doing a simple encryption program where the user in COMMAND LINE arguments (-d/-h etc) then have a key # for encryption (21,30 etc) and the CMD  would look like..
[code]gcc cipher.c -d 20 file1[/code]

the # is how many roll overs we have to the char hex vaule (A => F with a key of 5 (65=>70))

I have the arguments done, the issue i have is when i open the file with the initial text (called test.txt for me), i cant CLOSE the read file, it just spits out random characters that i have no idea where its coming from.

and never goes into my for loop..

[code]

#include 
#include 
#include 
#include 
#include 
#define MAXLINE 1000

int main (int argc, char *argv[]){
   // char line [MAXLINE];
   // long lineno =0;
    int c;
    int i;
    //FILE *fp;
    int fin;
    int fout;
    char buffer [80];
    /******************************
     Handles the arguments [-d][-e][-h][-?]
     *****************************/
    while (--argc >0 && (*++argv)[0]=='-') {
        while (c=*++argv[0]) {
            switch (c) {
                case 'd':
                    printf ("decrypt\n");
                    break;
                case 'e':
                    printf ("encrypt\n");
                    break;
                case 'h':
                    printf ("help\n");
                    break;
                case '?':
                    printf ("help?\n");
                    break;
                default:
                    perror ("open file");
                    break;
            }
        }
    }
    /******************************
     Handles file passed in
     *****************************/
    printf ("a\n");

        fin = open (" ", O_RDONLY);
  //  printf ("%d\n",fin);
    printf ("b\n");

       while (read (fin,buffer,80)!= 0){
           if (write (fout,buffer,80)==-1){
               perror ("writing");
               exit (-1);
           }

        }

    printf ("c\n");
   
    for (i=0; i>80; i++) {
      printf ("%d\n",buffer[i]);
    }
    printf ("d\n");


printf ("\n");

}

[/code]

-----------------------------------
DemonWasp
Fri Nov 18, 2011 2:09 pm

RE:Encryption
-----------------------------------
Err...why are you trying to open a file entitled " ", rather than the "test.txt" file name you mentioned earlier?

It doesn't look like your code ever captures the file name given as an argument, never mind actually using it.

-----------------------------------
Tallguy
Fri Nov 18, 2011 2:14 pm

RE:Encryption
-----------------------------------
whoops..i tried it severally diff ways, when i have "test.txt" i get "this is a sentence? ??a?0??a?0??a???$?;VZc" 
'this is a sentence' is what is inside the .txt file, but i have ZERO print statements for it ??

-----------------------------------
DemonWasp
Fri Nov 18, 2011 2:32 pm

RE:Encryption
-----------------------------------
You never open() fout, which means that the value is garbage from the stack. There are a number of hard-numbered "files" that can be accessed, such as stdin / stdout / stderr, so I suppose it's possible--though unlikely--that you're getting 1 (stdout) or 2 (stderr). It's also possible that if the file descriptor is invalid then it gets piped to stderr (though, why anyone would want that is beyond me).

Then, there's also the problem that you never check exactly how many bytes are read. Since your file is considerably smaller than your buffer (19-22 bytes of file, 80 bytes of buffer), you should only be able to read that many bytes, even though you requested up to 80. The rest of your buffer is then filled with garbage (whatever happened to be in memory there last). When you write() 80 bytes from your buffer, it includes that garbage.

As a general rule:

[code]
#define BUFFER_SIZE 1024


int fin = open ( "myfile.txt", O_RDONLY );

// Check that you opened the file correctly
if ( fin < 0 ) {
    printf ( "Error opening file: %d\n", fin );
    return -1;
}

char buffer[BUFFER_SIZE];

int bytes_read = 0;

// Read bytes from file, keeping track of how many you read.
while ( ( bytes_read = read ( fin, buffer, BUFFER_SIZE ) >= 0 ) {
    // do stuff with the contents of buffer (up to index bytes_read - 1)
}

// Close the file
int err = 0;
if ( ( err == close ( fin ) ) != 0 ) {
    println ( "Error occurred while closing file: %d\n", err );
}
[/code]

-----------------------------------
Tallguy
Fri Nov 18, 2011 8:09 pm

Re: Encryption
-----------------------------------
thank you for your continually help..much appreciated 

so i strted over again to work at it in a different angle, i want to pass the arguments to separate functions (encrypt, decrypt etc)
this is my input file 
02_process.c:19: error: conflicting types for ?encrypt?
/usr/include/unistd.h:586: error: previous declaration of ?encrypt? was here
02_process.c:97: error: conflicting types for ?encrypt?
/usr/include/unistd.h:586: error: previous declaration of ?encrypt? was here


and dont know why im passing 
[code]    encrypt (argv[2]);
[/code]

into 

[code]void encrypt (char * key){
    fprintf(stderr, "key: %s\n", key );
	return;
    
}[/code]

and getting an error?

-----------------------------------
Tony
Fri Nov 18, 2011 8:33 pm

RE:Encryption
-----------------------------------
1 - isn't argv of "const char *" type?
2 - you already have "encrypt" declared in an included file, causing conflicts

-----------------------------------
DemonWasp
Fri Nov 18, 2011 8:35 pm

RE:Encryption
-----------------------------------
This kind of error is pretty specific but not very intuitive. Here's what's going on:

1. You included 
2. In unistd.h, encrypt is defined as void encrypt(char block .
3. In your code, encrypt is defined as void encrypt (char *).
4. These are not equivalent declarations, so they cannot share the same name (in C*)
5. Since you have encrypt declared twice (once as a forward declaration), it complains about the same thing twice.

* If you compile as C++, then you can do that, though I don't recommend that course of action.

The simplest thing to do here is probably to rename your function to something that isn't already taken, such as shift_cipher_encrypt, which is what it looks like you're implementing.

@Tony: no, argv is char **. In fact, it's acceptable practice for programs to change the value of argv[0] to change the name they appear to be running under; see this page, under the bit about Apple/Darwin specific extensions: http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B

-----------------------------------
Tallguy
Fri Nov 18, 2011 8:53 pm

RE:Encryption
-----------------------------------
@DemonWasp i love you..just saying..will work on it and post final solution

-----------------------------------
Tony
Fri Nov 18, 2011 9:27 pm

RE:Encryption
-----------------------------------
@DemonWasp -- must have been thinking of something else. Thx.

-----------------------------------
Tallguy
Sat Nov 19, 2011 3:34 pm

Re: Encryption
-----------------------------------
[code]
#include 
#include 
#include 
#include 
#include 
#include 
#define BUFFER_SIZE 1024



#define OPTIONS "[-d] [-e] [-h] [-?] [output_file ] key input_file"

void display_usage( char *, char * );
void cypher_encrypt (char *);
void cypher_decrypt(char *);
int cypher_openFile(char *);
void cypher_saveFile(char *);

int main(int argc, char *argv[] ) {

	int aflag = 0;
	int bflag = 0;
	char *cvalue = NULL;
	//int index;
	int c;

	/* 
	 * MOD #1:  Declare a const char * with the options you want to process
	 */
	static const char *optString = "deh:";
    /******************************
     Handles the arguments [-d][-e][-h][-?]
     *****************************/
	opterr = 0;
	while ((c = getopt (argc, argv, optString)) != -1)
		switch (c) {
			case 'd':
				aflag = 1;
               cypher_decrypt(argv[2]);

                break;
			case 'e':
				bflag = 1;
                cypher_encrypt (argv[2]);
                break;
			case 'h':
				cvalue = optarg;
                display_usage(argv[0], OPTIONS);
				exit( EXIT_FAILURE );
				break;
			case '?':
				display_usage(argv[0], OPTIONS);
				exit( EXIT_FAILURE );
				break;  	/* Will never execute */
			default:
				/* Will never execute */
                perror ("open file");

				exit( EXIT_FAILURE );
		}
		
		/*
	         * Check that you have 2 and only 2 reminding arguments
	  	 */

		if ( optind != 2 ) {
			display_usage( argv[0], OPTIONS);
			exit( EXIT_FAILURE );
		}		

        //printf ("%s\n", argv[2]);   
    
    //cypher_saveFile  (argv[3]);
    cypher_openFile (argv[4]);


	return 0;
}

/*********************************************************************
 * display_usuage
 ********************************************************************/
void display_usage( char * prog, char *opts ) {

	fprintf(stderr, "usage: %s %s\n", prog, opts );
	return;

}

void cypher_encrypt (char * key){
    fprintf(stderr, "Encrypting with the key: %s\n", key );

	return;
}

void cypher_decrypt (char * key){
    fprintf(stderr, "Decrypting with the key: %s\n", key );
	return;
}

int cypher_openFile (char * file_toOpen){
    // char line [MAXLINE];
    // long lineno =0;
    int c;
    int i;
    //FILE *fp;
    int fin;
    int fout;
    int count=0;
    char buffer [BUFFER_SIZE]; 
    
    fin = open (file_toOpen, O_RDONLY);
    //  printf ("%d\n",fin);
    
    while (read (fin,buffer,BUFFER_SIZE)!=0){
       /* if (read (fin,buffer,BUFFER_SIZE)==-1){
            perror ("writing");
            exit (-1);
        }*/
       count++;
       
    }

    
    int err = 0;
    if ( ( err == close ( fin ) ) != 0 ) {
    } 
    printf ("-----------------\n");

    for (i=0; i