如何用C语言对文件进行加密和解密?

如何用C语言对文件进行加密和解密?,第1张

对于加密要求不高的完全可以自己定义规则来进行加密。这种加密是很简单很自由的,例如你在存文件的时候可以将文件中的每个字符都加上一个数,然后读取该文件的时候再每个字符相应地减去那尺念个数,即可实现就简单的加密,这样你储存的文件看上去就是乱码了。只是这个规则太简单,规则你可以自己定,加密与解密对着来就行了。

下面程序用异或 *** 作对文件进行加密和解密

/******************设计思路******************/

//根据用户输入的加密/机密密码

//每次都拿原文灶困滚件和密码等长度的一个字符串和密码

//对应元素异或进行加密/解密

//另外因为是用异或方法,所以加密和解密就是同一个程序

//即按照同样的加密即是对文件的解密

#include

#include

#include

#include

#include

charfilename[256]//原文件

charpassword[256]//加密/解密密码

constcharfilenametemp[]="temp15435255435325432543.temp"//加密/解密中间文件

voidinputpass(char*pass)//密码输入以"******"显示

voidmain(){

FILE*fp//加密/解密的文件

FILE*fptemp//加密/解密过程临时文件

intpwdlen//密码长度

inti=0//计数器

charch=0//读入的字符

printf("请输入要加密/解密的文件名(全路径名):\n")

gets(filename)

if((fp=fopen(filename,"rb"))==NULL){

printf("找不到文件%s\n",filename)

exit(1)

}//if

printf("请输入要加密/解密的密码:\n")

inputpass(password)

pwdlen=strlen(password)

if(pwdlen==0){

printf("密码不能为空,加密/解密失败\n")

exit(1)

}//if

fptemp=fopen(filenametemp,"wb")//打开中间文件

while(1){

ch=fgetc(fp)//从原文件读入一个字符

if(feof(fp)){//已经读到文件尾

break//退出循环

}

ch^=password[i++]//对原字符和密码进行异或 *** 作

fputc(ch,fptemp)//将异或结果写入中间文件

if(i==pwdlen){//使得原文件每和密码长度相同的固定长度异或加密

i=0

}

}//while

fclose(fp)//关闭打开原文件

fclose(fptemp)//关闭打开中间文件

remove(filename)//删除原文件

rename(filenametemp,filename)//将隐余中间文件重命名为原文件

printf("加密/解密成功\n")//至此加密/解密成功

}

//密码输入以"******"显示

voidinputpass(char*pass){

inti=0

charc

while(isprint(c=getch())){

pass[i++]=c

//printf("*")

}

pass[i]='\0'

printf("\n")

}

我哪岁写的,纯粹为了好玩。

小程序,嫌携加密自然比较简单,程序在win-tc下通过。

/* 数据安全实用程序,加密解密简单程序 */

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

int flag

char encrypt(char ch,int key)/*加密函数,把字符循环移位*/

{

if(ch>='a' &&ch<='z') /* 如果是小写字母 */

{

ch=(ch-'a'+key%26)%26+'a'

}

else if(ch>='A' &&ch<='Z') /* 如果是大写字母 */

{

ch=(ch-'A'+key%26)%26+'A'

}

return ch

}

char decrypt(char ch,int key)/*解密函数,把字符循环移位*/

{

if(ch>='a' &&ch<='z') /* 如果是小写字母 */

{

ch=(ch-'a'+26-key%26)%26+'a'

}

else if(ch>='A' &&ch<='Z') /* 如果是大李者睁写字母 */

{

ch=(ch-'A'+26-key%26)%26+'A'

}

return ch

}

void menu()/*菜单,1.加密,2.解密,3.显示文本文件内容*/

{

clrscr()

printf("\n=======================================================")

printf("\n1.Encrypt the text file")

printf("\n2.Decrypt the text file")

printf("\n3.Display text file contents")

printf("\n4.Quit\n")

printf("=========================================================\n")

printf("Please select a item:")

}

void logo()/*显示程序信息*/

{

printf("\n welcome to encrypt program \n ")

return

}

void encrypt_decrypt_File(char* infile,int key, char* outfile)

{

FILE *in,*out

char ch

clrscr()

if((in=fopen(infile,"r"))==NULL) /* 打开欲加密或解密的文件*/

{

printf("Can not open the infile!\n")

printf("Press any key to exit!\n")

getch()

exit(0)

}

if((out=fopen(outfile,"w"))==NULL) /* 打开文件保存加密或解密后的内容*/

{

printf("Can not open the outfile!\n")

printf("Press any key to exit!\n")

fclose(in)

getch()

exit(0)

}

ch=fgetc(in)/*从文本文件中读入字符*/

while(ch!=EOF)/*加密或解密*/

{

/*如果是英文字符,则进行加密或解密,否则,不进行加密或解密处理*/

if((ch>='a' &&ch<='z' ) || (ch>='A' &&ch<='Z'))

{ if(flag==1)

fputc(encrypt(ch,key),out)

if(flag==2)

fputc(decrypt(ch,key),out)

}

else

fputc(ch,out)

ch=fgetc(in)

}

/*关闭输入及输出文件*/

fclose(in)

fclose(out)

}

void displayFile(char *infile) /*将文本文件的内容显示在屏幕上*/

{

FILE *fp

char string[81]

if((fp=fopen(infile,"r"))==NULL)

{

printf("cann't open file")exit(0)

}

while(fgets(string,81,fp)!=NULL)

fputs(string,stdout)/*把所取字符串送到屏幕显示*/

fclose(fp)

}

int main()

{

int i,n

char ch0,ch1

char infile[40],outfile[40]

textbackground(LIGHTGRAY)/*设置背景颜色*/

textcolor(BLACK)/*设置文字颜色*/

clrscr()/*清除屏幕显示*/

logo()/*显示程序信息*/

sleep(2)

menu()/*显示屏幕菜单*/

ch0=getche()/*等待用户从键盘输入*/

while(ch0!='4')

{

clrscr()

if(ch0=='1') /*选择加密功能*/

{

flag=1

printf("\nPlease input the infile to be encrypted:")

scanf("%s",infile)

printf("Please input the encrypt key:")

scanf("%d",&n)/*输入加密密码*/

printf("Please input the outfile:")

scanf("%s",outfile)/*输入存放加密内容的文件名*/

encrypt_decrypt_File(infile,n,outfile)

printf("\nEncrypt is over!\n")

sleep(1)

}else if(ch0=='2') /*选择解密功能*/

{

flag=2

printf("\nPlease input the infile to be decrypted:")

scanf("%s",infile)

printf("Please input the decrypt key:")

scanf("%d",&n)/*输入解密密码,加密和解密密码应相同*/

printf("Please input the outfile:")

scanf("%s",outfile)/*输入存放解密内容的文件名*/

encrypt_decrypt_File(infile,n,outfile)

printf("\nDecrypt is over!\n")

sleep(1)

}else if(ch0=='3') /*选择显示文本文件功能*/

{

printf("\nPlease input the infile to be displayed:")

scanf("%s",infile)

displayFile(infile)

getch()

}else{ /*不合法输入*/

printf("\nplease input a valid number(1-4)\n")

sleep(1)

}

menu()/*显示程序菜单*/

ch0=getche()/*等待用户下一次的功能选择*/

}

system("cls")/*清除屏幕*/

logo()/*显示程序信息*/

printf("\nGood Bye!\n")

sleep(2)

system("pause")

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12351009.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存