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

问题描述 投票:1回答:2

我是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 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

$1$$.NeC/EbTUibao2by.HNV01

SHA-512

$6$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1

如你看到的。 SHA 512和MD5只进行了一次哈希实例。但是,我希望所有四个词都可以用两种方案进行调整。你能帮助我完成我需要完成的步骤吗?先感谢您 :)

c encryption sha512
2个回答
4
投票

笑着......

如果你之后添加一个{会发生什么

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一个(或更多))

仔细看看,如果你有其他问题,请告诉我。


1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.