c语言课程设计文件加密解密含源代码.docx
《c语言课程设计文件加密解密含源代码.docx》由会员分享,可在线阅读,更多相关《c语言课程设计文件加密解密含源代码.docx(11页珍藏版)》请在冰点文库上搜索。
C
语
言
课
程
设
计
实
验
报
告
实验名称:
文件加密解密
院系:
软件学院
学号:
日期:
2012年9月3日—9月17日
一:
设计题目
1:
设计图形用户界面。
2:
对文件进行加密并对加密文件进行保存。
3:
对加密了的文件进行解密。
二:
设计过程
设计过程中遇到的困难和解决方法:
1:
不能很好地理解题意(通过老师的讲解)。
2:
不知道如何设计加密解密程序(通过翻阅书籍和上网查找资料)
过程:
首先通过学习老师提供的资料了解大致的设计过程并懂得运用一些以前没有学习过的c语言。
先利用文本文件设计出加密解密的主要过程并能运行。
知道如何运用fopen将原文件打开并用fread将原文件内容读出来,然后进行加密设计并将加密的数据用fwrite写进指定的文件中并保存。
然后读出加密的文件并解密并保存。
最后在写出的程序中加入图形用户界面,运用window,box,gotoxy等进行设计。
三:
源代码
#include/*标准输入、输出函数*/
#include/*标准库函数*/
#include//*字符串处理函数*/
#include/*字符操作函数*/
#include
#include
#definekey_down80
#definekey_up72
#definekey_esc1
#definekey_enter28
#defineSIZE1
voidbox(intstartx,intstarty,inthigh,intwidth);
intget_key();
charbuf[20*20*4];
/*/////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////加密解密*/
voidfun(char*list,char*sd)/*加密过程*/
{
FILE*fp1,*fp2;
charbuf[1000];/*文件临时存放处*/
registerintch;
fp1=fopen("e:
\list.txt","r");/*用可读方式打开文件*/
fp2=fopen("e:
\sd.txt","w");/*用可写方式创建一个文件*/
if(fp1==NULL)
{
printf("cannotopenfile\n");
exit
(1);
}
if(fp2==NULL)
{
printf("cannotbuildfile\n");
exit
(1);
}
ch=fgetc(fp1);/*读出打开文件的光标处的一个字符*/
while(!
feof(fp1))/*读出的字符不是最后的字符*/
{
ch=ch<<1;/*加密方法*/
fputc(ch,fp2);/*加密的字符存放在指定的地方*/
ch=fgetc(fp1);
}
rewind(fp2);/*将光标移动到第一个字符前面*/
fread(buf,sizeof(buf),1,fp2);/*从文件的当前位置开始中读取buf中存放的数据*/
printf("%s",buf);
/*fclose(fp1);
fclose(fp2);*/
}
voidman(char*sd,char*ds)/*解密过程*/
{
/*intn=0;*/
FILE*fp2,*fp3;
registerintfh;
charbuf1[1000];
fp2=fopen("e:
\sd.txt","rb");/*用可读方式打开文件*/
fp3=fopen("e:
\ds.txt","wb");/*用可写方式创建一文件*/
if(fp2==NULL)
{
printf("cannotopenfile\n");
exit
(1);
}
if(fp3==NULL)
{
printf("cannotbuildfile\n");
exit
(1);
}
fh=fgetc(fp2);/*从光标处读出一个字符*/
while(!
feof(fp2))/*当读出的字符到达最后一个则停止*/
{
fh=fh>>1;/*解密方式*/
fputc(fh,fp3);/*解密的字符存放在指定的地方*/
fh=fgetc(fp2);
}
fread(buf1,sizeof(buf1),1,fp3);/*读出buf1中所存放的数据*/
printf("%s",buf1);
}
voidmain()
{intk;
char*f[]={"jiami","jiemi"};/**界面的形式/
intkey,y;
intj,q;
charlist[300];
charsd[300];
chards[300];
charch,fh;
charbuf[1000];
charbuf1[1000];
FILE*fp1;
FILE*fp2;
intl1,l2;
window(1,1,80,25);/*left,top,right,bottom,相对于屏幕的字符坐标,屏幕原点在左上角*/
gettext(20,10,40,14,buf);/*保存矩形屏幕上的字符*/
textbackground(7);/*背景颜色*/
textcolor(0);/*字体颜色*/
clrscr();/*清除矩形屏幕上的所有字符*/
gotoxy(24,10);/*将当前字符屏幕的光标位置移动到x,y的坐标位子*/
printf("%s",f[0]);
gotoxy(24,14);
printf("%s",f[1]);
gettext(10,8,60,16,buf);
box(22,9,3,30);/*建立一个小窗口*/
key=0;
while
(1)
{while(bioskey
(1)==0);/*读取键盘值查询键盘是否按下*/
key=get_key();/*按下了什么键盘*/
if(key==key_up||key==key_down)
{
y=wherey();/*得到字符模式下窗口光标的x坐标数值*/
if(key==key_up)
y=y==10?
y+4:
10;/*当y=10光标向下移动四个位置否则将光标移动到y=10处*/
if(key==key_down)
y=y==14?
y-4:
14;/*当y=14光标向下移动四个位置否则将光标移动到y=14处*/
puttext(10,8,60,16,buf);/*将gettext函数保存的字符恢复到屏幕上*/
gotoxy(24,y);
if(y==10)
{textbackground(7);
textcolor(0);
box(22,9,3,30);
textbackground(3);
textcolor(15);
gotoxy(24,y);
cprintf("%s",f[0]);
}
else
{textbackground(7);
textcolor(0);
box(22,13,3,30);
textbackground(3);
textcolor(15);
gotoxy(24,y);
cprintf("%s",f[1]);
}
}
if(key==key_enter&&y==10)且光标在y=10处/*当按下enter键且光标在y=10处进行下步*/
{
clrscr();
textbackground(3);
textcolor(15);
/*clrscr();*/
gotoxy(24,5);
printf("inputthefilenameforjiami\n");/*用户给需要加密的文件加密*/
l1=strlen("inputthefilenameforjiami:
");/*待求长度的字符串指针*/
gotoxy(24+l1,5);
scanf("%s",list);
gotoxy(24,10);
printf("inputfilenameforsave\n");/*给加密后的文件命名,并保存*/
l2=strlen("inputfilenameforsave:
");
gotoxy(24+l2,10);
scanf("%s",sd);
fun(list,sd);
fp1=fopen("e:
\sd.txt","rb");
fread(buf1,sizeof(buf1),1,fp1);
gotoxy(10,15);
printf("%s\n",buf1);
getch();
printf("filehavenjiami,savenow");
getche();break;
}
if(key==key_enter&&y==14)
{
clrscr();
textbackground(3);
textcolor(15);
gotoxy(24,5);
printf("inputthefilenameforjiemi\n");/*用户给需要解密的文件解密*/
l1=strlen("inputthefilenameforjiemi:
");
gotoxy(24+l1,5);
scanf("%s",sd);
gotoxy(24,10);
printf("inputfilenameforsave:
\n");/*对解密的文件系统又可以提供保存路径*/
l2=strlen("inputfilenameforsave:
");
gotoxy(24+l2,10);
scanf("%s",ds);
man(sd,ds);
fp2=fopen("e:
\ds.txt","rb");
fread(buf1,sizeof(buf1),1,fp2);
gotoxy(10,15);
printf("%s\n",buf1);
getch();
printf("filehavenjiemi,savenow");
getche();
break;
}
}
window(1,1,80,25);
gettext(20,10,40,14,buf);
textbackground(7);
textcolor(0);
clrscr();
gotoxy(24,10);
printf("%s",f[0]);
gotoxy(24,14);
printf("%s",f[1]);
gettext(10,8,60,16,buf);
box(22,9,3,30);
key=0;
while
(1)
{while(bioskey
(1)==0);
key=get_key();
if(key==key_up||key==key_down)
{
y=wherey();
if(key==key_up)
y=y==10?
y+4:
10;
if(key==key_down)
y=y==14?
y-4:
14;
puttext(10,8,60,16,buf);
gotoxy(24,y);
if(y==10)/*光标在10处的窗口*/
{textbackground(7);
textcolor(0);
box(22,9,3,30);
textbackground(3);
textcolor(15);
gotoxy(24,y);
cprintf("%s",f[0]);
}
else
{textbackground(7);
textcolor(0);
box(22,13,3,30);
textbackground(3);
textcolor(15);
gotoxy(24,y);
cprintf("%s",f[1]);
}
}
if(key==key_enter&&y==10)
{
clrscr();
textbackground(3);
textcolor(15);
/*clrscr();*/
gotoxy(24,5);
printf("inputthefilenameforjiami\n");/*用户给需要加密的文件加密*/
l1=strlen("inputthefilenameforjiami:
");
gotoxy(24+l1,5);
scanf("%s",list);
gotoxy(24,10);
printf("inputfilenameforsave\n");/*给加密后的文件命名,并保存*/
l2=strlen("inputfilenameforsave:
");
gotoxy(24+l2,10);
scanf("%s",sd);
fun(list,sd);
fp1=fopen("e:
\sd.txt","rb");
fread(buf1,sizeof(buf1),1,fp1);
gotoxy(10,15);
printf("%s\n",buf1);
getch();
printf("filehavenjiami,savenow");
getche();
}
if