1、C语言课程设计实验报告实验名称:文件加密解密院系:软件学院 学号:日期:2012年9月3日9月17日 一:设计题目1:设计图形用户界面。2:对文件进行加密并对加密文件进行保存。3:对加密了的文件进行解密。 二:设计过程设计过程中遇到的困难和解决方法:1:不能很好地理解题意(通过老师的讲解)。2:不知道如何设计加密解密程序(通过翻阅书籍和上网查找资料)过程:首先通过学习老师提供的资料了解大致的设计过程并懂得运用一些以前没有学习过的c语言。先利用文本文件设计出加密解密的主要过程并能运行。知道如何运用fopen将原文件打开并用fread将原文件内容读出来,然后进行加密设计并将加密的数据用fwrite
2、写进指定的文件中并保存。然后读出加密的文件并解密并保存。最后在写出的程序中加入图形用户界面,运用window,box,gotoxy等进行设计。 三:源代码#include /* 标准输入、输出函数 */#include /* 标准库函数 */#include /*字符串处理函数 */#include /* 字符操作函数 */#include #include #define key_down 80 #define key_up 72 #define key_esc 1 #define key_enter 28#define SIZE 1void box(int startx,int start
3、y,int high,int width);int get_key();char buf20*20*4;/*/加密解密 */void fun(char *list,char *sd) /*加密过程*/ FILE *fp1,*fp2; char buf1000; /*文件临时存放处*/ register int ch; fp1=fopen(e:list.txt,r); /*用可读方式打开文件*/ fp2=fopen(e:sd.txt,w); /*用可写方式创建一个文件*/ if(fp1=NULL) printf(cannot open filen); exit(1); if(fp2=NULL)
4、printf(cannot build filen); exit(1); ch=fgetc(fp1); /*读出打开文件的光标处的一个字符*/ while(!feof(fp1) /*读出的字符不是最后的字符*/ ch=ch1; /*解密方式*/ fputc(fh,fp3); /*解密的字符存放在指定的地方*/ fh=fgetc(fp2); fread(buf1,sizeof(buf1),1,fp3); /*读出buf1中所存放的数据*/ printf(%s,buf1); void main() int k; char *f=jiami,jiemi; /*界面的形式/ int key,y; in
5、t j,q; char list300; char sd300; char ds300; char ch,fh; char buf1000; char buf11000; FILE *fp1; FILE *fp2; int l1,l2; window(1,1,80,25); /*left,top,right,bottom,相对于屏幕的字符坐标,屏幕原点在左上角*/ gettext( 20,10,40,14,buf); /*保存矩形屏幕上的字符*/ textbackground(7); /*背景颜色*/ textcolor(0); /*字体颜色*/ clrscr(); /*清除矩形屏幕上的所有字
6、符*/ gotoxy(24,10); /*将当前字符屏幕的光标位置移动到x,y的坐标位子*/ printf(%s,f0); gotoxy(24,14); printf(%s,f1); 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_
7、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(%
8、s,f0); else textbackground(7); textcolor(0); box(22,13,3,30); textbackground(3); textcolor(15); gotoxy(24,y); cprintf(%s,f1); if(key=key_enter&y=10)且光标在y=10处 /*当按下enter键且光标在y=10处进行下步*/ clrscr(); textbackground(3); textcolor(15); /*clrscr(); */ gotoxy(24,5); printf(input the file name for jiamin);/*用
9、户给需要加密的文件加密 */ l1=strlen(input the file name for jiami:); /*待求长度的字符串指针*/ gotoxy(24+l1,5); scanf(%s,list); gotoxy(24,10); printf(input file name for saven);/*给加密后的文件命名,并保存*/ l2=strlen(input file name for save:); gotoxy(24+l2,10); scanf(%s,sd); fun(list,sd); fp1=fopen(e:sd.txt,rb); fread(buf1,sizeof(b
10、uf1),1,fp1); gotoxy(10,15); printf(%sn,buf1); getch(); printf(file haven jiami ,save now); getche(); break; if(key=key_enter&y=14) clrscr(); textbackground(3); textcolor(15); gotoxy(24,5); printf(input the file name for jiemi n);/*用户给需要解密的文件解密 */ l1=strlen(input the file name for jiemi: ); gotoxy(24
11、+l1,5); scanf(%s,sd); gotoxy(24,10); printf(input file name for save:n);/*对解密的文件系统又可以提供保存路径 */ l2=strlen(input file name for save: ); 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(%sn,buf1); getch(); printf(file haven jiemi,
12、save now); getche(); break; window(1,1,80,25); gettext( 20,10,40,14,buf); textbackground(7); textcolor(0); clrscr(); gotoxy(24,10); printf(%s,f0); gotoxy(24,14); printf(%s,f1); 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=
13、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,f0); else textbackground(7); textcolor(0); box(22,13,3,3
14、0); textbackground(3); textcolor(15); gotoxy(24,y); cprintf(%s,f1); if(key=key_enter&y=10) clrscr(); textbackground(3); textcolor(15); /*clrscr(); */ gotoxy(24,5); printf(input the file name for jiamin);/*用户给需要加密的文件加密 */ l1=strlen(input the file name for jiami:); gotoxy(24+l1,5); scanf(%s,list); gotoxy(24,10); printf(input file name for saven);/*给加密后的文件命名,并保存*/ l2=strlen(input file name for save:); 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(%sn,buf1); getch(); printf(file haven jiami ,save now); getche(); if
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2