从文件中读取单词,并按行中的每条线加密



我是C编程语言的新手。我想寻求指导,以加密文本文件并显示它。这是我到目前为止所做的。我想提前道歉,因为我对堆栈越过的群体很新。

file-reader1.c是我打算从文本文件中读取的程序。

file-Reader1.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 Resultn");
    printf("%sn",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-512n");
    printf("%sn",result);
    
    fclose(ptr_file); //closing the file
    return 0;
}

small_wordlist是一个文件,包含我想加密的单词列表。

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 Resultn");
    printf("%sn",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-512n");
    printf("%sn",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 ("%sn",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 Resultn");
        printf("%sn",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-512n");
        printf("%sn",result);
    }

注意:不要在代码中使用魔法数字,例如1000散布在整个过程中,如果您需要常量,则#define一个(或更多))

看事物,让我知道您是否还有其他问题。

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

为了改变这种行为,您需要将buf分开成复合词。一种简单的方法是阅读缓冲区,用无效终止器替换所有空格字符,并保存每个单词开始的索引。然后呼叫crypt遇到的每个单词一次,通过buf + i,其中i是单词开头的索引。

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

最新更新