Problems with XOR

This is a discussion on Problems with XOR within the C Programming forums, part of the General Programming Boards category; This program compiles and runs, but for some reason xor isn't working....it doesn't save the variable in it's xor'ed form....so ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    Problems with XOR

    This program compiles and runs, but for some reason xor isn't working....it doesn't save the variable in it's xor'ed form....so for some reason it's having no effect. In execution of the program I've put spots where it spits out the password it read in, i run xor on it before that so it will come out how it should have went to the file.....this is really making me mad. Thanks everyone.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    #include <dos.h>
    #include <process.h>
    
    int i=0;
    int choice=3;
    int didBack=1;
    int goAhead=1;
    int hackTry=0;
    char password[]="4gn890ng08sdhv089vh2390trh820h";
    char pword[51];
    char newPword[51];
    char Key[51];
    FILE * passFile;
    
    int main()
    {
    	system("cls");
    	printf("Login V1.0\nCreated By Drew Peterson\n\n");
    	/*DO A CHECK HERE FOR THE PASSWORD FILE, IF IT DOESN'T EXIST, PROMPT FOR THE DEFAULT (FIRST) PASSWORD*/
    	passFile=fopen("c:\\pass.dat","r");
    	if(passFile!=NULL)
    	{
    		fgets(password,50,passFile);
    		fclose(passFile);
    	}
    	else
    	{
    		passFile=NULL;
    		passFile=fopen("c:\\pass.dat","w+");
    		if(passFile==NULL)
    		{
    			printf("\nFile Error, Press Any Key To Reboot");
    			getch();
    			fflush(stdin);
    			//outportb(0xCF9,4);
    			//outportb(0xCF9,2);
    		}
    		else
    		{
    			printf("Enter Desired Password: ");
    			gets(pword);
    			fflush(stdin);
    			/*ENCRYPT*/
    			for(i=0;i<51;i++)
    			{
    				pword[i]=pword[i]^Key[i];
    			}
    			fputs(pword,passFile);
    			fclose(passFile);
    			/*DECRYPT*/
    			for(i=0;i<51;i++)
    			{
    				pword[i]=pword[i]^Key[i];
    			}
    			printf("\n\nCongratulations, Your Password Is: ");
    			printf("%s",pword);
    			printf("\nYour Computer Must Now Reboot, Press Any Key");
    			getch();
    			fflush(stdin);
    			//outportb(0xCF9,4);
    			//outportb(0xCF9,2);
    
    		}
    	}
    
    
    	printf("Press 1 To Log In\nPress 2 To Change Password\n\n");
    	while(choice>2||choice<1)
    	{
    		scanf("%d",&choice);
    		fflush(stdin);
    	}
    	if(choice==1)
    	{
    		while(goAhead==1)
    		{	
    			/*DECRYPT*/
    			for(i=0;i<51;i++)
    			{
    				pword[i]=pword[i]^Key[i];
    			}
    			printf("%s",password);
    			printf("Enter Password: ");
    			for (i=0;i<50;i++)
    			{
    				if(didBack==0)
    				{
    					i=i-2;
    				}
    				didBack=1;
    				pword[i]=getch();
    				if(pword[i]=='\r')
    				{
    					pword[i]='\0';
    					break;
    				}
    				if(pword[i]=='\b'&&pword[i]>=0)
    				{
    					didBack=0;
    					printf("\b \b");
    				}
    				else
    				{
    					printf("*");
    				}
    				fflush(stdin);
    
    			}
    			if(strcmp(password,pword)==0)
    			{
    				goAhead=0;
    			}
    			else
    			{
    				printf("\nIncorrect Password\n\n");
    				hackTry++;
    				if(hackTry>=5)
    				{
    					printf("\nYou Have Entered An Incorrect Password Too Many Times,\nPress Any Key To Reboot");
    					//outportb(0xCF9,4);
    					//outportb(0xCF9,2);
    				}
    			}
    		}
    		printf("\nYou Have Successfully Logged In\nPress Any Key To Continue\n");
    		getch();
    		fflush(stdin);
    		//execl("c:\\com.com",0);
    	}
    	else
    	if(choice==2)
    	{
    		/*DECRYPT*/
    		for(i=0;i<51;i++)
    		{
    			pword[i]=pword[i]^Key[i];
    		}
    		while(goAhead==1)
    		{
    			printf("Enter Current Password: ");
    			for (i=0;i<50;i++)
    			{
    				if(didBack==0)
    				{
    					i=i-2;
    				}
    				didBack=1;
    				pword[i]=getch();
    				if(pword[i]=='\r')
    				{
    					pword[i]='\0';
    					break;
    				}
    				if(pword[i]=='\b'&&pword[i]>=0)
    				{
    					didBack=0;
    					printf("\b \b");
    				}
    				else
    				{
    					printf("*");
    				}
    				fflush(stdin);
    
    			}
    			if(strcmp(password,pword)==0)
    			{
    				goAhead=0;
    			}
    			else
    			{
    				printf("\nIncorrect Password\n\n");
    				hackTry++;
    				if(hackTry>=5)
    				{
    					printf("\nYou Have Entered An Incorrect Password Too Many Times,\nPress Any Key To Reboot");
    					getch();
    					//outportb(0xCF9,4);
    					//outportb(0xCF9,2);
    				}
    			}
    		}
    		if(goAhead==0)
    		{
    			printf("\nEnter New Password: ");
    			gets(newPword);
    			fflush(stdin);
    			/*ENCRYPT*/
    			for(i=0;i<51;i++)
    			{
    				pword[i]=pword[i]^Key[i];
    			}
    			passFile=fopen("c:\\pass.dat","w+");
    			fputs(pword,passFile);
    			fclose(passFile);
    			printf("Your Password Has Been Changed, Make Note Of It In A Safe Place\nYour Computer Must Now Reboot, Press Any Key");
    			getch();
    			fflush(stdin);
    			//outportb(0xCF9,4);
    			//outportb(0xCF9,2);
    		}
    	}
    
    	/*will only get to this point if it failed*/
    	printf("Failure To Load Operating System, Press Any Key To Reboot");
    	getch();
    	/*code to reboot*/
    	//outportb(0xCF9,4);
    //	outportb(0xCF9,2);
    
    	exit(0);
    }
    Last edited by Waldo2k2; 01-21-2003 at 06:26 PM.
    PHP and XML
    Let's talk about SAX

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    im not having trouble that it's saving nothing to the file, it's saving what the user typed in UNCHANGED. That's what's so confusing, it's like it never executes the xor code. Now i've done this before just like this....it's soooo confusing as to what could be wrogn....and i don't think it would interpret 0 as the end of the string, it has to be \0

    but thanks so far


    [edit]
    found a bug that may have led you to think the string was being messed with, i had &s instead of %s in a print....still same outcome as before though
    [/edit]
    Last edited by Waldo2k2; 01-21-2003 at 06:26 PM.
    PHP and XML
    Let's talk about SAX

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    and i don't think it would interpret 0 as the end of the string, it has to be \0
    You're wrong. Salem's right. End of story.

    The decimal value of zero is in fact the same exact thing as '\0'. Enjoy.
    Code:
    #include <stdio.h>
    int main( void )
    {
        return printf("%d is %d\n", 0, '\0' );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    ok thanks for the info...but there's a problem, that's not pertinent here because:
    it's not cutting the string short at this point,
    the word i typed was baseball....the key is 4gn890ng08sdhv089vh2390trh820h....none of the letters are in there....the only problem is xor isn't working....can someone just compile this and see what I mean? thanks.
    PHP and XML
    Let's talk about SAX

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    WOW!!!!

    I need a vacation


    password="4gn890ng08sdhv089vh2390trh820h";

    i was xoring Key.....which needs to have "4gn890ng08sdhv089vh2390trh820h" as it's value....so yeah you guys were right and i was right that the 0 had nothing to do with it.
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 05:55 PM
  2. Problems with Xor encryption
    By lala123 in forum C Programming
    Replies: 22
    Last Post: 12-13-2007, 08:34 PM
  3. XOR and File I/O Problems...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2005, 03:21 AM
  4. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 08:35 PM
  5. Xor problems
    By Zionaster in forum C++ Programming
    Replies: 6
    Last Post: 07-10-2003, 03:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21