C programming incorrect encrypting while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ocli5568
    New Member
    • Sep 2009
    • 7

    C programming incorrect encrypting while loop

    this main is supposed to receive user input (file names, flags and a keyword), then encrypt the file according to the user input, then close the file pointers. however it goes wrong for some reason. All of the functions have been independantly tested and work but the main fails.

    when i first run the program (in the while loop) it encrypts the data properly. and if i quit the while loop and restart the program it will decrypt it properly.

    Although when i encrypt the data (and stay in the while loop) then try and decrypt it immediately following it fails to decrypt it properly. no idea why...?

    Code:
    int main(void)
    {
    char dataInput[BUFSIZE], dataOutput[BUFSIZE], keyword[BUFSIZE];
    int flags[NFLAGS];
    flags[QUIT] = 0;
    flags[HELP] = 0;
    
    while (flags[QUIT] != 1)
    {	
    	while (parse_user_input(dataInput, dataOutput, keyword, flags) == 0)
    		printf("Error: Invalid input. type h for help.\n");
    
    	if (flags[QUIT] == 1)
    		break;
    
    	else if (flags[HELP] == 1){
    		help_message(flags[HELP]);
    		flags[HELP] = 0;
    	}
    
    	else
    	{
    		FILE *fin, *fout;
    		if (flags[SEED] == 0){
    			int i;
    			for (i = 0; keyword[i] != NUL; ++i)
    			flags[SEED] = keyword[i] + 31*flags[SEED];
    			}
    
    		fin = open_file(dataInput, 0);
    		fout = open_file(dataOutput, 1);
    
    		if (fin == 0 || fout == 0)
    			printf("Error opening file");
    		else
    		{
    		ranf_start(flags[SEED]);
    
    		encrypt(fin, fout, keyword, flags[MODULO]);
    
    		fclose(fout);
    		fclose(fin);
    		}
    	}
    
    }
    return 0;
    }
    any advice would help a lot. Final step of my project!!!
    Last edited by Banfa; Sep 13 '09, 03:29 PM. Reason: A
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I do not see any way in which this program would ever decrypt data unless your encryption algorithm happened to be ROT13

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Banfa
      I do not see any way in which this program would ever decrypt data unless your encryption algorithm happened to be ROT13
      ... or any other encryption method that is involutary, i.e. E(x) ==E^-1(x), so E(E(x)) == x.

      kind regards,

      Jos

      Comment

      Working...