2014-02-12 33 views
0

我链接-lcrypt,问题是我得到相同的加密,无论我的命令行参数。如果我改变盐,加密似乎只会改变。我的代码中会有什么会导致这个缺陷?为什么crypt函数在这里不起作用?

#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)); 

} 

回答

1

crypt(*enc, salt),你加密你的第一的说法,这是程序,而不是第一个实际参数的名称。改为尝试crypt(enc[1], salt)

1

你差不多了。只有命令行参数处理是错误的。

如果你的程序被称为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)); // <<---- 

} 

通常的命令行参数被称为argc和argv:

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

这将使相关的行如下:

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

更改第一个参数为enc [1]工作....但我还是不明白这一点:printf(“%s \ n”,crypt(“drain”,salt));无论第一个参数如何,我都会得到相同的加密,只有更改盐才能更改加密。这是为什么? – user3303819

+1

@ user3303819 - 我不明白你的评论。 –

相关问题