Problem reading a password from a file.

This is a discussion on Problem reading a password from a file. within the C Programming forums, part of the General Programming Boards category; I have a password inside a file that's ciphered with the caesar cipher (you move the letter 3 spaces to ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    16

    [SOLVED]Problem reading a password from a file.

    I have a password inside a file that's ciphered with the caesar cipher (you move the letter 3 spaces to the right, if you have 'a' it becomes a 'd', if you have a 'b' it becomes an 'e' and so on). I'm suppose to write a program that decodes the password inside the file and compare it with a password. To read the password you must first enter the name of the file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char crypted[BUFSIZ];
    	char cleared[BUFSIZ];
    	int shift, i, a;
    	char *arch;
    	char pass[20];
    	
    	arch=(char*)malloc(sizeof(char));
    
    
    	FILE *usua;
    	printf ("\n Ingrese su id de usuario(su rut sin puntos ni dígito verificador): ");
    	scanf ("%s", arch);
    		
    	printf ("\n Ingrese su password : ");
    	scanf ("%s", pass);
    	
    	a=strlen(pass);
    	pass[a]='\0';
    		
    	usua=fopen(arch, "r");
    	fgets (crypted, BUFSIZ, usua);
    
    	shift = 3;
    	i=0;
    	
    	while (crypted[i]!='\0')
    	{
    		if (crypted[i]==99)
    		{
    			crypted[i] =122;
    			cleared[i] = crypted[i] ;
    			i++;
    		}
    		
    		if (crypted[i]==98)
    		{
    			crypted[i] =121;
    			cleared[i] = crypted[i] ;
    			i++;
    		}
    		if (crypted[i]==97)
    		{
    			crypted[i] =120;
    			cleared[i] = crypted[i] ;
    			i++;
    		}
    				
    		
    		else
    		{
    			cleared[i] = crypted[i] - shift;
    			i++;
    		}
    	}
    	cleared[i] = '\0';
    		
    	printf("\nDesencriptado:\n%s", cleared);
    	
    	i=0;
    	a=0;
    	while(pass[i]!='\0')
    	{
    		if (pass[i]==cleared[i])
    		{
    			i++;
    		}
    		else
    		{
    			a=1;
    			i++;
    		}
    	}
    	
    	if (a==1)
    	{
    		printf ("\nClave Incorrecta");
    	}
    	else
    	{
    		printf ("\nClave Correcta");
    	}
    			
    	return 0;
    }
    the file is named: 16486061
    the password inside that file (after the decoding) is: fugazi

    It decodes the password in the file, but when it comes to the comparison between the password in the file and the one the user enters it always throws the same result, no matter if the passwords match or not.
    Last edited by medeshago; 12-21-2008 at 06:00 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're comparing pass, which is a pointer to a char, with an array.

    You need to compare two strings, perhaps messageClear and messageCrypt?

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    501
    Code:
    	while (messageClear[i])
    	{
    		if (messageClear[i]==99)
    		{
    			messageClear[i] = 122;
    			messageCrypt[i] = messageClear[i] ;
    			i++;
    		}
    		else
    		{
    			messageCrypt[i] = messageClear[i] - shift;
    			i++;
    		}
    	}
    As I see it, your logic is wrong.
    Instead of decrypting the password ,you are again encrypting it again!.
    Also the return of malloc should not be cast. read the faq.
    Code:
    while (messageClear[i])
    Wouldn't the proper way of doing this would be,
    Code:
    while (messageClear[i]!='\0')
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Wouldn't

    messageCrypt[i] -= shift;

    be what you want?

    You don't want to "decrypt" the messageClear.

    Good catch, Steve!

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Quote Originally Posted by Adak View Post
    You're comparing pass, which is a pointer to a char, with an array.

    You need to compare two strings, perhaps messageClear and messageCrypt?
    I need to compare the string entered by the user (the password) with the array, comparing messageclear with messagecrypt will always throw me an error because one is the password decoded and the other is the password encoded. How can I compare pass with the password decoded?

    stevesmithx: The decoding works ok, the names of the variables are wrong. I will fix the problem with the malloc. But my problem is with the comparison, not with the decoding. I changed the name of the variables in the original post.
    Last edited by medeshago; 12-21-2008 at 04:34 AM.

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    501
    stevesmithx: The decoding works ok, the names of the variables are wrong. I will fix the problem with the malloc. But my problem is with the comparison, not with the decoding. I'm gonna edit the code and change the name of variables.
    Sorry,I misinterpreted your code.
    But still i think there are some mistakes in your logic.
    For 'c' will be converted to 'z'.What about 'b' and 'a'?
    In other words your code won't work for all the inputs.

    Good catch, Steve!
    Not really
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Quote Originally Posted by stevesmithx View Post
    Sorry,I misinterpreted your code.
    But still i think there are some mistakes in your logic.
    For 'c' will be converted to 'z'.What about 'b' and 'a'?
    In other words your code won't work for all the inputs.


    Not really
    Thanks, I didn't notice that. But my main problem continues to be the comparison.

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    501
    Quote Originally Posted by medeshago View Post
    Thanks, I didn't notice that. But my main problem continues to be the comparison.
    Did you try what Adak suggested?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Quote Originally Posted by stevesmithx View Post
    Did you try what Adak suggested?
    How can I convert an array to a pointer to a char?

  10. #10
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    501
    How can I convert an array to a pointer to a char?
    Huh?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't convert an array to a pointer to type char.

    You have two char arrays already. One for clear text, and one for encrypted.

    You need the clear text, so you can see what the clear text is, throughout the program, for debugging, if nothing else. You need the encrypted array in case the user goofs, or you want to run the program, with a second user.

    You also need something like a "messageTry" array of char's, to hold the user's attempts at
    entering their password.

    Then you can make your strcmp(messageTry, messageClear) and you'll be fine.

    or, if you prefer, you can encrypt the messageTry, and then compare it with the encrypted password:
    strcmp(messageCrypt, messageTry).

    Either way is fine.
    Last edited by Adak; 12-21-2008 at 05:17 AM.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Quote Originally Posted by Adak View Post
    You're comparing pass, which is a pointer to a char, with an array.

    You need to compare two strings, perhaps messageClear and messageCrypt?
    Quote Originally Posted by stevesmithx View Post
    Huh?
    I need to trasnform the array to a string.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just append an end of string char to the array, to do that: '\0'.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    16
    Quote Originally Posted by Adak View Post
    Just append an end of string char to the array, to do that: '\0'.
    So I have to save the password to an array and add an end of string to that array?

    I did it like this:
    Code:
    int a;
    char pass[20]
    
    printf ("\n Enter your password : ")
    scanf ("%s", pass);
    
    a=strlen(pass);
    pass[a]='\n';
    Thanks, man! I've got it working. I'm gonna change the code of the original post in case anyone needs a solution, it's not an elegant solution, but I have time to polish it.

  15. #15
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    501
    Note that reading strings using scanf can be dangerous unless you specify the size of the string buffer within the format string.
    Also be picky about choosing variable names as choosing contradicting variable names can cause a lot of confusion to others who read your code.
    Those are just my two cents.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Replies: 22
    Last Post: 12-23-2008, 12:53 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

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