‘壹’ 如何用C语言对文件进行加密和解密
对于加密要求不高的完全可以自己定义规则来进行加密。这种加密是很简单很自由的,例如你在存文件的时候可以将文件中的每个字符都加上一个数,然后读取该文件的时候再每个字符相应地减去那尺念个数,即可实现就简单的加密,这样你储存的文件看上去就是乱码了。只是这个规则太简单,规则你可以自己定,加密与解密对着来就行了。
下面程序用异或操作对文件进行加密和解密
/******************设计思路******************/
//根据用户输入的加密/机密密码,
//每次都拿原文灶困滚件和密码等长度的一个字符串和密码
//对应元素异或进行加密/解密
//另外因为是用异或方法,所以加密和解密就是同一个程序
//即按照同样的加密即是对文件的解密
#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");
}
‘贰’ 将凯撒密码X的加密、解密过程用C语言编程实现
1、在密码学中,恺撒密码(或称恺撒加密、恺撒变换、变换加密)是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。恺撒密码通常被作为其他更复杂的加密方法中的一个步骤,例如维吉尼尔密码。恺撒密码还在现代的ROT13系统中被应用。但是和所有的利用字母表进行替换的加密技术一样,恺撒密码非常容易被破解,而且在实际应用中也无法保证通信安全。例子恺撒密码的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左或向右移动一个固定数目的位置。
2、kaiser加密算法具体程序:
#include<stdio.h>
#include<conio.h>
charencrypt(charch,intn)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
return('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return('a'+(ch-'a'+n)%26);
}
returnch;
}
voidmenu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf(" =========================================================");
printf(" 1.Encryptthefile");
printf(" 2.Decryptthefile");
printf(" 3.Forcedecryptfile");
printf(" 4.Quit ");
printf("========================================================= ");
printf("Pleaseselectaitem:");
return;
}
main()
{
inti,n;
charch0,ch1;
FILE*in,*out;
charinfile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*输入加密密码*/
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Encryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/
n=26-n;
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Decryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("========================================================== ");
printf("Theoutfileis: ");
printf("========================================================== ");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf(" ======================================================== ");
printf("Thecurrentkeyis:%d ",i);/*显示当前破解所用密码*/
printf("Press'Q'toquitandotherkeytocontinue...... ");
printf("========================================================== ");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
{
clrscr();
printf(" GoodBye! ");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf(" Forcedecryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf(" GoodBye! ");
sleep(3);
}
‘叁’ C语言程序:数据的简单加密和解密:对于重要的信息,为了在传输或存储时避免泄露,可以在传输或存储前进行加
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int a,i;
char word[100];
printf("please input the array:");
gets(word);
printf("\nplease input the password:");
scanf("%d",&a);
for(i=0;i<strlen(word);i++)
word[i]+=(a+i);
printf("\n%s",word);
return 0;
}