Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中使用crypt函数时出错_C_Encryption_Cs50 - Fatal编程技术网

在C中使用crypt函数时出错

在C中使用crypt函数时出错,c,encryption,cs50,C,Encryption,Cs50,我在C中使用crypt函数,在这里我给命令行输入一个加密字。我使用/usr/share/dict/words中的单词,并使用crypt函数对它们进行加密,然后将crypt函数的加密输出与命令行输入进行比较。如果单词相同,则使用printf语句将非加密代码作为输出。 代码如下所示 #include<stdio.h> #define _XOPEN_SOURCE #include<unistd.h> #include<cs50.h>

我在C中使用crypt函数,在这里我给命令行输入一个加密字。我使用/usr/share/dict/words中的单词,并使用crypt函数对它们进行加密,然后将crypt函数的加密输出与命令行输入进行比较。如果单词相同,则使用printf语句将非加密代码作为输出。 代码如下所示

    #include<stdio.h>
    #define _XOPEN_SOURCE
    #include<unistd.h>
    #include<cs50.h>
    #include<string.h>
    int
    main(int argc, string argv[]){
    char line[80];
    string crypto;
    if(argc>2||argc<2)
     {
     printf("ERROR. Enter only one crypt");
     return 1;
     }
    string crypti=argv[1];
    FILE *fr;
    string as;
    fr=fopen("/usr/share/dict/words","r");
    if(!fr)
        {
      printf("File can't be read");
      exit(-1);
        }
    while(fgets(line,80,fr)!=NULL)
      {
      as=crypt(line,"50");
      if(strcmp(as,crypti)==0)
       {
     printf("%s",line);
      break;
       }
       }
    fclose(fr);
   }
#包括
#定义_XOPEN_源
#包括
#包括
#包括
int
main(int argc,字符串argv[]){
字符行[80];
字符串加密;

如果(argc>2 | | argc将以下片段添加到
while(fgets…
body)的开头:

  size_t len = strlen(line);
  if (len)
    line[len-1]='\0';
fgets
读取的缓冲区末尾通常有一个换行符
\n
(读取时)

您的原始代码与
“password”
配合使用,因为
crypt实际上只使用密钥的前8个字符。它也可以与长度为8或更长的任何单词配合使用

另外,通过在格式字符串中添加换行符或(如果不想输出额外的换行符)调用
fflush(stdout)
,确保在打印结果后刷新输出:


谢谢。它似乎可以工作8个字符或更多。但仍然无法让它工作少于8个字符。它适合我(我在
/usr/share/dict/words
中没有“abaca”,但abacus在那里,可以为
508kXyEKZ232U
找到)。如果不是以换行符结尾,可能您的shell提示符会使您很难看到输出:请尝试将
\n
添加到您的格式字符串。在我使用fflush(stdout)之后,它似乎可以工作。非常感谢。
printf("%s\n",line);
/* or */
printf("%s",line);
fflush(stdout);