我是 C 编程语言的新手。我想寻求有关加密文本文件中的单词并显示它的指导。这是我到目前为止所做的。我想提前为格式道歉,因为我是堆栈溢出的新手。
File-reader1.c 是我打算用来读取文本文件的程序。
文件阅读器1.c
#include <stdio.h>
#include <string.h>
int main()
{
FILE *ptr_file; //declaring a file
char buf[1000];
char * hash_type_1 = "$1$"; // md5 hash
char * hash_type_2 = "$6$"; // sha512 hash
char * salt_1 ="$"; //a simple salt
char * result;
char encyption_scheme[20];
ptr_file = fopen("small_wordlist","r"); //opening the file
if (!ptr_file)
return 1;
while (fgets(buf,1000, ptr_file) != NULL)
printf("%s",buf);
// prepare the first call using md5
strcpy(encyption_scheme,hash_type_1);
strcat(encyption_scheme,salt_1);
result = crypt(buf,encyption_scheme);
printf("MD5 Result\n");
printf("%s\n",result);
// prepare the second call using sha-512
strcpy(encyption_scheme,hash_type_2);
strcat(encyption_scheme,salt_1);
result = crypt(buf,encyption_scheme);
printf("SHA-512\n");
printf("%s\n",result);
fclose(ptr_file); //closing the file
return 0;
}
Small_Wordlist 是一个包含我要加密的单词列表的文件。
小词表
Andy
Noel
Liam
Paul
我的输出如下:
Noel
Liam
Andy
Paul
MD5 Result
$1$$.NeC/EbTUibao2by.HNV01
SHA-512
$6$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1
如您所见。 SHA 512 和 MD5 只进行了一次哈希运算。但是,我希望所有四个词都用两种方案进行散列处理。您能否指导我完成完成它所需的步骤?提前谢谢你:)
最佳答案
咯咯笑...
如果添加 {
会发生什么之后
while (fgets(buf,1000, ptr_file)!=NULL)
和一个}
之前
fclose(ptr_file);//closing the file
你只是在读取和输出buf
并且只加密单词列表中的最后一个单词 :)
如果不清楚,您看起来像是打算执行以下操作:
while (fgets(buf,1000, ptr_file)!=NULL)
{
printf("%s",buf);
/* prepare the first call using md5 */
strcpy(encyption_scheme,hash_type_1);
strcat(encyption_scheme,salt_1);
result = crypt(buf,encyption_scheme);
printf("MD5 Result\n");
printf("%s\n",result);
/* prepare the second call using sha-512 */
strcpy(encyption_scheme,hash_type_2);
strcat(encyption_scheme,salt_1);
result = crypt(buf,encyption_scheme);
printf("SHA-512\n");
printf("%s\n",result);
}
为了避免包含结尾的 '\n'
(由 fgets
阅读并包含)在您的加密中,我建议在调用 crypt
之前将其删除使用类似于以下内容的内容:
#define MAXC 1000
...
while (fgets(buf,MAXC, ptr_file)!=NULL)
{
size_t len = strlen (buf); /* get length */
if (len && buf[len - 1] == '\n') /* check last char '\n' */
buf[--len] = 0; /* overwrite with nul-char */
else if (len == MAXC - 1) { /* handle line too long */
fprintf (stderr, "error: line too long.\n");
/* handle error as desired */
}
printf ("%s\n",buf);
/* prepare the first call using md5 */
strcpy(encyption_scheme,hash_type_1);
strcat(encyption_scheme,salt_1);
result = crypt(buf,encyption_scheme);
printf("MD5 Result\n");
printf("%s\n",result);
/* prepare the second call using sha-512 */
strcpy(encyption_scheme,hash_type_2);
strcat(encyption_scheme,salt_1);
result = crypt(buf,encyption_scheme);
printf("SHA-512\n");
printf("%s\n",result);
}
(注意不要在代码中使用魔数(Magic Number),例如 1000
分散在各处,相反,如果您需要一个常量,#define
一个 (或更多))
检查一下,如果您还有其他问题,请告诉我。
关于c - 从文件中读取单词并在 C 中逐行加密每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949450/