Linux 在 c 中加密和解密文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6858073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encryption & Decryption a File in c
提问by Hit's
Here I have a simple code for Encryption & Decryption a File in C as below,
在这里,我有一个简单的代码,用于加密和解密 C 中的文件,如下所示,
#include <stdio.h>
int Encrypt(char * FILENAME, char * NEW_FILENAME)
{
FILE *inFile; //Declare inFile
FILE *outFile; //Declare outFile
char Byte;
char newByte;
int n;
int i=0;
inFile = fopen(FILENAME,"rb");
outFile = fopen(NEW_FILENAME, "w");
if(inFile == NULL || outfile == NULL){
printf("Error in opening file");
return 1;
} else {
printf("\nFile Opened, Encrypting");
while(1){
while ( !feof( inFile ) ){
Byte=fgetc(inFile);
newByte=Byte+25;
fputc(newByte,outFile);
}
printf("End of File");
break;
}
fclose(inFile);
fclose(outFile);
}
}
int Decrypt (char *FILENAME, char *NEW_FILENAME)
{
FILE *inFile; //Declare inFile
FILE *outFile; //Declare outFile
char Byte;
char newByte;
int i=0;
inFile = fopen(FILENAME,"rb");
outFile = fopen(NEW_FILENAME, "w");
if(inFile == NULL || outfile == NULL){
printf("Error in opening file");
return 1;
} else {
printf("File Opened, Decrypting");
while(1){
printf(".");
while ( !feof( inFile ) ){
Byte=fgetc(inFile);
newByte=Byte-25;
fputc(newByte,outFile);
}
printf("End of File");
break;
}
fclose(inFile);
fclose(outFile);
}
}
int main()
{
char encFile[200];
char newencFile[200];
char decFile[200];
char newdecFile[200];
int choice;
printf("Enter 1 to Encrypt / 2 to Decrypt");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the Source Filename: ");
scanf("%s",encFile);
printf("Enter the Destination Filename: ");
scanf("%s",newencFile);
Encrypt(encFile, newencFile);
break;
case 2:
printf("Enter the Source Filename: ");
scanf("%s",decFile);
printf("Enter the Destination Filename: ");
scanf("%s",newdecFile);
Decrypt(decFile, newdecFile);
break;
}
return 0;
}
This code works but at the end of file it also contain "??"
such characters so it can't open in default text-editor in linux & becomes useless to store private details. I want to get same to same stuff back in the discript file.
Please help for the same, Thanks in advance.
此代码有效,但在文件末尾它还包含"??"
此类字符,因此无法在 linux 的默认文本编辑器中打开,并且无法存储私人详细信息。我想在描述文件中恢复相同的内容。请帮忙,提前致谢。
采纳答案by niko
Before I explain the solution to the above code, Let me tell you these things first. Beaware of unneccessary defination of variables in your program. I see lots of memory wastage in your code.Everyone can write a program but the one makes it more simpler gets the crown.Remember that when you work on programming.
在我解释上面代码的解决方案之前,让我先告诉你这些事情。注意程序中不必要的变量定义。我看到您的代码中存在大量内存浪费。每个人都可以编写程序,但程序越简单越好。请记住,当您进行编程时。
I see Unneccessary variable i,defination in your Encrypt and Decrypt functions. and There is no need to go for a new byte char defination You can sort off make it as
我在您的 Encrypt 和 Decrypt 函数中看到了不必要的变量 i,定义。并且无需进行新的字节字符定义您可以将其整理为
byte=byte+25 \ even that makes your work no need of Newbyte char variable.
Erase the int n,int i definations to make your code better.And the most important thing you really got to know is
擦除 int n,int i 定义以使您的代码更好。您真正了解的最重要的事情是
you cannot use char a;a=getc(Filepointer);
remember the EOF returns -1 when it reaches to the end of file and if your using a char .It doesnot work. Char data type can only handle positive data .Even if you assign -1 to char, it internally saves it as positive number you can check that thing on google.
请记住,EOF 在到达文件末尾时返回 -1,如果您使用的是 char 。它不起作用。Char 数据类型只能处理正数据。即使您将 -1 分配给 char,它也会在内部将其保存为正数,您可以在 google 上查看该内容。
so never use char data type for checkin EOF use int datatype
and as Ronald mentioned you make it as
正如罗纳德所说,你把它做成
while((Byte=getc(Filepointer))!=EOF)
Thats the error you made.Make it as int datatype.And please check the previous questions.Becuase its actually a duplicate question.Some people can Treat as downvote.
这就是你犯的错误。将其设为 int 数据类型。请检查前面的问题。因为它实际上是一个重复的问题。有些人可以将其视为否决票。
The better style for encrypting and decrypting is using bitwise operations rather than addition
更好的加密和解密风格是使用按位运算而不是加法
Byte=Byte^200; To encrypt \ just a sample
Byte=Byte^200; To decrypt \ you can make more complicated
That how you gotta do. It gives you more security compared to adding 25 to your ACSII char.YOu can even encrypt it giving some predefined rules to it.Hope you got the answer and solved you problem.Thank you
那你得怎么做。与向 ACSII 字符添加 25 相比,它为您提供了更高的安全性。您甚至可以对其进行加密,并为其提供一些预定义的规则。希望您得到答案并解决了您的问题。谢谢
回答by Roland Illig
Your code is not correct. You probably expect that the encrypted file has the same size as the original file, but this is not the case. The faulty code is:
您的代码不正确。您可能期望加密文件与原始文件具有相同的大小,但事实并非如此。错误的代码是:
while(1){
printf(".");
while ( !feof( inFile ) ){
Byte=fgetc(inFile);
newByte=Byte+25;
fputc(newByte,outFile);
}
printf("End of File");
break;
}
It must instead be:
它必须是:
while ((Byte = fgetc(inFile)) != EOF) {
newByte = Byte + 25;
if (fputc(newByte, outFile) == EOF) {
/* ERROR */
}
}
printf("End of File\n");
Note that the printf
statement ends with a newline character (\n
). This is a style which you should follow. Otherwise the output may not show up immediately because of buffering.
请注意,该printf
语句以换行符 ( \n
)结尾。这是您应该遵循的风格。否则由于缓冲,输出可能不会立即显示。
Some more details: the feof
function only checks if the end of file marker has been set. But when the fgetc
function notices that it has reached the end of file, this marker has not been set yet. Instead, the fgetc
function returns EOF
in that case.
更多细节:该feof
函数仅检查是否已设置文件结束标记。但是当fgetc
函数注意到它已经到达文件末尾时,这个标记还没有被设置。相反,该fgetc
函数EOF
在这种情况下返回。
Oh, and please change the types of your variables.
哦,请更改变量的类型。
char Byte; /* this is wrong. */
int Byte; /* this is correct. */
See any goodintroductory book on Programming in C for the details.
有关详细信息,请参阅有关用 C 编程的任何优秀介绍性书籍。
回答by R Madhavan
What about this new program that uses a different logic:
这个使用不同逻辑的新程序怎么样:
#include <stdio.h>
int main()
{
char src[50], tgt[53], ch;
printf ("Enter file name: ");
gets (src);
fs = fopen (src, "r");
if (fs == NULL)
{
printf ("Error while opening file!\n");
return 1;
}
sprintf (tgt, "%s.ed", src);
ft = fopen (tgt, "w");
while ((ch = fgetc (fs)) != EOF)
fputc (~ch, ft);
fclose (fs);
fclose (ft);
remove (src);
rename (tgt, src);
return 0;
}
There is no need to write separate functions/programs for both the purposes. Also, the same file will be encrypted by printing the one's complement of a file to another file and renaming the new one to the older one after deleting the older one.
Overall: This is very simpleutility!
无需为这两个目的编写单独的函数/程序。此外,同一个文件将通过将一个文件的补码打印到另一个文件并在删除旧文件后将新文件重命名为旧文件来加密。
总体:这是一个非常简单的实用程序!