繁体   English   中英

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

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

我对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);
    }

注意:请勿在代码中使用幻数 ,例如,在整个数字中散布1000 ,而如果需要一个常数,则#define一个(或多个))

查看情况,如果您还有其他问题,请告诉我。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM