简体   繁体   中英

Why is the crypt function not working here?

I linked -lcrypt , the problems is that I get the same encryption no matter my command line argument. The encryption seems to only change if I change the salt. What in my code would lead to this flaw?

#define _XOPEN_SOURCE       
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <string.h>


int main(int argc, char *enc[])
{
if (argc != 2)
{  
    printf("Improper command-line arguments\n");
    return 1;
}
char *salt = "ZA";

printf("%s \n", crypt(*enc, salt));

}

In crypt(*enc, salt) , you're encrypting your first argument, which is the name of the program, not the first actual argument. Try crypt(enc[1], salt) instead.

You nearly got it. only the commandline argument handling was wrong.

if your program is called prg and you call it like this:

prg teststring

than enc[1] is "teststring"

#define _XOPEN_SOURCE       
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <string.h>


int main(int argc, char *enc[])
{
    if (argc != 2)
    {  
            printf("Improper command-line arguments\n");
                return 1;
    }
    char *salt = "ZA";

    printf("%s \n", crypt(enc[1], salt)); // <<----

}

usually the command line args are called argc and argv:

int main(int argc, char *argv[])

that would make the relevant line like this:

printf("%s \n", crypt(argv[1], salt)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM