[英]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.