简体   繁体   English

为什么crypt功能在这里不起作用?

[英]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. 我链接-lcrypt,问题是我得到了相同的加密,无论我的命令行参数。 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. crypt(*enc, salt) ,您正在加密第一个参数,这是程序的名称,而不是第一个实际参数。 Try crypt(enc[1], salt) instead. 尝试使用crypt(enc[1], salt)代替。

You nearly got it. 你几乎得到了它。 only the commandline argument handling was wrong. 只有命令行参数处理错误。

if your program is called prg and you call it like this: 如果您的程序名为prg,并且您将其称为:

prg teststring

than enc[1] is "teststring" enc[1]是“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: 通常命令行args称为argc和argv:

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

that would make the relevant line like this: 这将使相关的行像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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