从文件中读取单词并在 C 中逐行加密每个单词

Reading words from a file and encrypting each of them line by line in C

我对 C 编程语言还很陌生。我想寻求有关加密文本文件中的单词并显示它的指导。这是我到目前为止所做的。我想提前为格式道歉,因为我是堆栈溢出的新手。

File-reader1.c 是我打算用来读取文本文件的程序。

文件-reader1.c

#include <stdio.h>
#include <string.h>

int main()
{

    FILE *ptr_file;  //declaring a file
    char buf[1000];
    char * hash_type_1 = "$";  // md5 hash
    char * hash_type_2 = "$";  // 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 是一个包含我要加密的单词列表的文件。

Small_wordlist

Andy 

Noel

Liam

Paul

我的输出如下:

Noel

Liam

Andy

Paul

MD5 Result

$$.NeC/EbTUibao2by.HNV01

SHA-512

$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1

如你所见。 SHA 512 和 MD5 只进行了一次哈希运算。但是,我希望所有四个词都用两种方案进行散列处理。您能否指导我完成完成它所需的步骤?提前谢谢你:)

在文件的顶部,您已将 small_wordlist 读入 buf。然后调用 crypt,将 buf 作为数据传递。这就是加密只发生一次的原因:buf 被视为一个大字符串,所有单词都被一起加密。

为了改变这种行为,您需要将 buf 拆分成复合词。一种简单的方法是读取缓冲区,用空终止符替换所有空白字符并保存每个单词开头的索引。然后为遇到的每个单词调用一次 crypt,传递 buf + i,其中 i 是单词开头的索引。

当然,还有其他方法可以将 buf 分开,但重要的是您需要多次调用 crypt 来执行多次加密。

咯咯笑...

如果在

之后添加 { 会发生什么
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);
    }

(注意:不要在代码中使用 幻数,例如 1000 分散在各处,相反,如果你需要一个常量,#define一个(或多个))

检查一下,如果您还有其他问题,请告诉我。