[英]Why is the crypt function not working here?
我链接-lcrypt,问题是我得到了相同的加密,无论我的命令行参数。 加密似乎只有在我更改盐时才会改变。 我的代码会导致这个缺陷?
#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));
}
在crypt(*enc, salt)
,您正在加密第一个参数,这是程序的名称,而不是第一个实际参数。 尝试使用crypt(enc[1], salt)
代替。
你几乎得到了它。 只有命令行参数处理错误。
如果您的程序名为prg,并且您将其称为:
prg 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)); // <<----
}
通常命令行args称为argc和argv:
int main(int argc, char *argv[])
这将使相关的行像这样:
printf("%s \n", crypt(argv[1], salt));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.