Thread: The crypt function in crypt.h

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89

    The crypt function in crypt.h

    I'm a beginner (and I will probably be forever…), so I visited the FAQ section. Right now I'm studying the FAQ > Environment and user data page and I try to make the examples there work by modifying them to make sense.

    I failed with the last one however.
    Here's the example code from that page:
    Code:
    #include <sys/types.h> 
    #include <pwd.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    
    struct passwd *p;
    p = getpwnam( "username" );
    if (p) 
    {
       if( strcmp( crypt( "Password", p->pw_passwd ), p->pw_passwd ) == 0 ) 
          {
            puts( "match" );
          } 
       else 
       {
            puts( "no match" );
       }
    }
    Of course this doesn't work out of the box, so I modified it a bit:
    Code:
    #include <sys/types.h> 
    #include <pwd.h> 
    #include <unistd.h> 
    #include <stdio.h>
    #include <string.h>
    #include <crypt.h>
    
    int
    main(int argc, char *argv[])
    {
       if (argc==2) {
          struct passwd *p;
          p=getpwnam(getlogin());
          if(p) {
             if(strcmp(crypt(argv[1], p->pw_passwd), p->pw_passwd)==0)
                puts("Right!");
             else
                puts("Wrong!");
          }
          return 0;
       }
       else {
          puts("Syntax:");
          puts("Password <password>");
          return 1;
       }
    }
    Maybe I did an obvious mistake, I don't know. Maybe I just misunderstood the whole concept.

    The problem is that whatever I try with as input, as long as there are exactly one parameter, the output is ”Wrong!”, even when the correct password is used.

    So what obvious little thing did I miss?

    Here's what the output from running it would look like, if the correct password is ”ThisIsTheCorrectPa55word”:
    Code:
    $ ./PasswordTest
    Syntax:
    PasswordTest <password>
    $ ./PasswordTest aligraljrhgor
    Wrong!
     $ ./PasswordTest ThisIsTheCorrectPa55word
    Wrong!
    $
    Compiled with:
    Code:
    gcc PasswordTest.c -lcrypt -o PasswordTest
    There were no errors when compiling.

    gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    Last edited by guraknugen; 07-14-2013 at 01:58 PM. Reason: Missing space…

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by guraknugen View Post
    I'm a beginner (and I will probably be forever…), so I visited the FAQ section. Right now I'm studying the FAQ > Environment and user data page and I try to make the examples there work by modifying them to make sense.

    I failed with the last one however.
    Here's the example code from that page:
    Code:
    #include <sys/types.h> 
    #include <pwd.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    
    struct passwd *p;
    p = getpwnam( "username" );
    if (p) 
    {
       if( strcmp( crypt( "Password", p->pw_passwd ), p->pw_passwd ) == 0 ) 
          {
            puts( "match" );
          } 
       else 
       {
            puts( "no match" );
       }
    }
    Of course this doesn't work out of the box, so I modified it a bit:
    Code:
    #include <sys/types.h> 
    #include <pwd.h> 
    #include <unistd.h> 
    #include <stdio.h>
    #include <string.h>
    #include <crypt.h>
    
    int
    main(int argc, char *argv[])
    {
       if (argc==2) {
          struct passwd *p;
          p=getpwnam(getlogin());
          if(p) {
             if(strcmp(crypt(argv[1], p->pw_passwd), p->pw_passwd)==0)
                puts("Right!");
             else
                puts("Wrong!");
          }
          return 0;
       }
       else {
          puts("Syntax:");
          puts("Password <password>");
          return 1;
       }
    }
    Maybe I did an obvious mistake, I don't know. Maybe I just misunderstood the whole concept.

    The problem is that whatever I try with as input, as long as there are exactly one parameter, the output is ”Wrong!”, even when the correct password is used.

    So what obvious little thing did I miss?

    Here's what the output from running it would look like, if the correct password is ”ThisIsTheCorrectPa55word”:
    Code:
    $ ./PasswordTest
    Syntax:
    PasswordTest <password>
    $ ./PasswordTest aligraljrhgor
    Wrong!
     $ ./PasswordTest ThisIsTheCorrectPa55word
    Wrong!
    $
    Compiled with:
    Code:
    gcc PasswordTest.c -lcrypt -o PasswordTest
    There were no errors when compiling.

    gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    It seems to me you may have interpreted getlogin() wrong. I'm a Windows guy myself, but from reading the documentation here :

    getlogin - Linux Command - Unix Command

    It seems it retrieves your user password. Is that your user password that you gave us? Otherwise that may be your problem.

    You could change this in your code :

    Code:
    p=getpwnam(getlogin());
    
    to this :

    Code:
    p=getpwnam("ThisIsTheCorrectPa55word");
    
    As I said, I do not know a lot about linux. That is my guess on what is wrong though.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Could it be that your system doesn't use crypt, but instead uses something like ecb_crypt(3): fast DES encryption - Linux man page?

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    What do you get by code below?
    Code:
    printf ("%s\n", p->pw_passwd);
    man shadow.h

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Maybe Ubuntu/Linaro doesn't use shadow?

    Couldn't edit previous post, it's:
    man shadow

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char *crypt(const char *key, const char *salt);

    #define _GNU_SOURCE /* See feature_test_macros(7) */
    #include <crypt.h>

    Link with -lcrypt.

    DESCRIPTION
    crypt() is the password encryption function. It is based on the Data
    Encryption Standard algorithm with variations intended (among other
    things) to discourage use of hardware implementations of a key search.

    key is a user's typed password.

    salt is a two-character string chosen from the set [a–zA–Z0–9./]. This
    string is used to perturb the algorithm in one of 4096 different ways.
    Does not looks like your salt is 2 char string as described in man page.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by HelpfulPerson View Post
    Is that your user password that you gave us?
    Well, a fake one, yes. I replaced my real password with that text string, just to illustrate what I was doing, I think I mentioned that, but maybe I forgot. When I tested the program, I entered my real user password.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by HelpfulPerson View Post
    It seems to me you may have interpreted getlogin() wrong. I'm a Windows guy myself, but from reading the documentation here :

    getlogin - Linux Command - Unix Command

    It seems it retrieves your user password.
    No, it retrieves the login name, in my case ”guraknugen” (I also tested this). If it was possible to retrieve the password, it wouldn't be very safe, would it…? As far as I understand, the password can't be retrieved with less than brutal force.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    I saw another crypt example where they used a much longer salt than that, but I didn't test it, I think… Maybe you are right. Well, it certainly looks like it…

    I'll take a look at it tomorrow, as I am a little bit too busy this evening, unfortunately.
    Thank you, and thank all the others who replied! I'll take a closer look at all your replies and come back later.


  10. #10
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by fnprintf View Post
    Maybe Ubuntu/Linaro doesn't use shadow?

    Couldn't edit previous post, it's:
    man shadow
    At least the man page is installed. 96 lines all together.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by oogabooga View Post
    Could it be that your system doesn't use crypt, but instead uses something like ecb_crypt(3): fast DES encryption - Linux man page?
    Maybe, I don't know… I'll take a look at it later. Not much time for it this evening, unfortunately… Thanks for replying!

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Try testing it with test passwd entry first.

    Add the following line to /etc/passwd:
    Code:
    testing:$1$5L1tFZUF$60ivH79nKUxC.q4DyhNIE1:2013:2013:DELETE AFTER TESTING:/dev/null:/sbin/nologin
    Username: testing
    Password: ThisIsTheCorrectPa55word

    For more on passwd: man 5 passwd
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <pwd.h>
    #include <crypt.h>
    
    int
    main (int argc, char *argv[])
    {
        struct passwd *pwd = NULL;
        char *username = "testing", *result = NULL;
        
        if (argc != 2)
        {
            printf ("Usage: %s <password>\n", argv[0]);
            return 0;
        }
        
        pwd = getpwnam (username);
    
        if (pwd)
        {
            result = crypt (argv[1], pwd->pw_passwd);
            
            if (!result)
                return 2;
            
            printf ("Result: %s\nPasswd: %s\n", result, pwd->pw_passwd);
            
            if (!strcmp (result, pwd->pw_passwd))
                printf ("Right!\n");
            else
                printf ("Wrong!\n");
        }
        else
        {
            printf ("'%s' not found.\n", username);
            return 1;
        }
    
        free (result);
        return 0;
    }
    Compile with -lcrypt.

    https://www.gnu.org/software/libc/ma...ode/crypt.html
    Last edited by fnprintf; 07-15-2013 at 02:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to crypt the source code of Ufasoft Miner to make it undetectable
    By dannytrevor765 in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 10-18-2012, 10:41 AM
  2. Help with strncpy - crypt code
    By szylvia in forum C++ Programming
    Replies: 15
    Last Post: 05-17-2011, 04:38 AM
  3. Bitshift Crypt
    By ratte in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 02:48 PM
  4. CRYPT don' t save file
    By krakz in forum C Programming
    Replies: 5
    Last Post: 02-28-2003, 04:51 AM
  5. Problem with crypt
    By Unregistered in forum C Programming
    Replies: 15
    Last Post: 03-12-2002, 05:35 AM