[英]Reading words from a file and encrypting each of them line by line in C
I am fairly new to the C programming language. 我对C编程语言还很陌生。 I would like to seek guidance in encrypting words from a text file and displaying it.
我想寻求指导,以加密文本文件中的单词并显示它。 Here's what I have done so far.
到目前为止,这是我所做的。 I would like to apologise in advance for the formatting as I'm rather new to stack-overflow.
我想对这种格式表示歉意,因为我对堆栈溢出不熟悉。
File-reader1.c is a program I intend to use to read from the text file. 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 is a file containing a list of words I would like to encrypt. Small_Wordlist是一个文件,其中包含我想加密的单词列表。
Andy
Noel
Liam
Paul
My output is as follows : 我的输出如下:
Noel
Liam
Andy
Paul
MD5 Result
$1$$.NeC/EbTUibao2by.HNV01
SHA-512
$6$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1
As you can see. 如你看到的。 Only one instance of hashing has been done for both SHA 512 and MD5.
SHA 512和MD5仅完成了一个哈希实例。 However, I would like all four words to be hashed with both schemes.
但是,我希望对这四个方案的所有四个单词都进行哈希处理。 Would you be so kind as to walk me through the steps I need to do to accomplish it ?
您是否乐于助我一步步完成任务? Thank you in advance :)
先感谢您 :)
Chuckling... 咯咯笑...
What happens if you add a {
after 如果在之后添加
{
while (fgets(buf,1000, ptr_file)!=NULL)
and a }
before 和
}
之前
fclose(ptr_file);//closing the file
You are simply reading and outputting buf
and only encrypting the final word in the word-list :)
您只是在读取和输出
buf
并仅加密单词列表中的最后一个单词:)
If it is unclear, you look like you intended to do the following: 如果不清楚,您似乎打算执行以下操作:
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);
}
To avoid including the trailing '\\n'
(read and included by fgets
) in your encryption, I would recommend removing it before calling crypt
using something similar to the following: 为了避免在加密中包含尾随的
'\\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);
}
( note: don't use magic numbers in your code, eg 1000
scattered throughout, instead, if you need a constant, #define
one (or more)) ( 注意:请勿在代码中使用幻数 ,例如,在整个数字中散布
1000
,而如果需要一个常数,则#define
一个(或多个))
look things over and let me know if you have further questions. 查看情况,如果您还有其他问题,请告诉我。
At the top of your file, you've read small_wordlist into buf
. 在文件的顶部,您已经将small_wordlist读入了
buf
。 Then you call crypt
, passing buf
as the data. 然后调用
crypt
,将buf
作为数据传递。 This is why the encryption only happens once: buf
is being treated as one big string, and all the words are being encrypted together. 这就是加密仅发生一次的原因:
buf
被视为一个大字符串,并且所有单词都一起被加密。
In order to change this behavior, you'll need to break buf
apart into its composite words. 为了更改此行为,您需要将
buf
分解成其复合词。 A simple way to do this is to read through the buffer, replacing all whitespace characters with null terminators and saving the index of the start of each word. 一种简单的方法是读取缓冲区,用空终止符替换所有空白字符,并保存每个单词开头的索引。 Then call
crypt
once for each word encountered, passing buf + i
where i
is the index of the beginning of the word. 然后为每个遇到的单词调用一次
crypt
,并传递buf + i
,其中i
是单词开头的索引。
There are, of course, other ways to break buf
apart, but the important thing is that you'll need multiple calls to crypt
to perform multiple encryptions. 当然,还有其他方法可以使
buf
分开,但重要的是,您将需要多次调用crypt
来执行多种加密。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.