第一篇:c語言課程設(shè)計-文件加密解密(含源代碼)
C 語 言 課 程 設(shè) 計 實 驗 報 告
實驗名稱:文件加密解密 院系:軟件學院
學號:
年9月3日—9月17日 日期:2012
一:設(shè)計題目
1:設(shè)計圖形用戶界面。
2:對文件進行加密并對加密文件進行保存。3:對加密了的文件進行解密。
二:設(shè)計過程
設(shè)計過程中遇到的困難和解決方法: 1:不能很好地理解題意(通過老師的講解)。
2:不知道如何設(shè)計加密解密程序(通過翻閱書籍和上網(wǎng)查找資料)過程:
首先通過學習老師提供的資料了解大致的設(shè)計過程并懂得運用一些以前沒有學習過的c語言。先利用文本文件設(shè)計出加密解密的主要過程并能運行。知道如何運用fopen將原文件打開并用fread將原文件內(nèi)容讀出來,然后進行加密設(shè)計并將加密的數(shù)據(jù)用fwrite寫進指定的文件中并保存。然后讀出加密的文件并解密并保存。最后在寫出的程序中加入圖形用戶界面,運用window,box,gotoxy等進行設(shè)計。
三:源代碼
#include
///////////////////////////////////////////////////////////////加密解密 */ void fun(char *list,char *sd)/*加密過程*/ {
FILE *fp1,*fp2;char buf[1000];/*文件臨時存放處*/ register int ch;fp1=fopen(“e:list.txt”,“r”);/*用可讀方式打開文件*/ fp2=fopen(“e:sd.txt”,“w”);/*用可寫方式創(chuàng)建一個文件*/ if(fp1==NULL){ printf(“cannot open filen”);exit(1);} if(fp2==NULL){ printf(“cannot build filen”);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中存放的數(shù)據(jù)*/ printf(“%s”,buf);/*fclose(fp1);fclose(fp2);*/ }
void man(char *sd,char *ds)/*解密過程*/ { /*int n=0;*/ FILE *fp2,*fp3;register int fh;char buf1[1000];
fp2=fopen(“e:sd.txt”,“rb”);/*用可讀方式打開文件*/ fp3=fopen(“e:ds.txt”,“wb”);/*用可寫方式創(chuàng)建一文件*/ if(fp2==NULL){ printf(“cannot open filen”);exit(1);} if(fp3==NULL){ printf(“cannot build filen”);exit(1);} fh=fgetc(fp2);/*從光標處讀出一個字符*/ while(!feof(fp2))/*當讀出的字符到達最后一個則停止*/ { fh=fh>>1;/*解密方式*/
fputc(fh,fp3);/*解密的字符存放在指定的地方*/ fh=fgetc(fp2);} fread(buf1,sizeof(buf1),1,fp3);/*讀出buf1中所存放的數(shù)據(jù)*/ printf(“%s”,buf1);}
void main(){ int k;char *f[]={“jiami”,“jiemi”};/**界面的形式/ int key,y;int j,q;char list[300];char sd[300];char ds[300];char ch,fh;char buf[1000];char buf1[1000];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();/*清除矩形屏幕上的所有字符*/ 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坐標數(shù)值*/ if(key==key_up)y=y==10? y+4:10;/*當y=10光標向下移動四個位置否則將光標移動到y(tǒng)=10處*/ if(key==key_down)y=y==14? y-4:14;/*當y=14光標向下移動四個位置否則將光標移動到y(tǒng)=14處*/
puttext(10,8,60,16,buf);/*將gettext函數(shù)保存的字符恢復(fù)到屏幕上 */
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(“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();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+l1,5);scanf(“%s”,sd);gotoxy(24,10);printf(“input file name for save:n”);/*對解密的文件系統(tǒng)又可以提供保存路徑 */ 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,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”,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(“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(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+l1,5);scanf(“%s”,sd);gotoxy(24,10);printf(“input file name for save:n”);/*對解密的文件系統(tǒng)又可以提供保存路徑 */ 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,save now”);getche();break;}
}
}
int get_key(){ union REGS rg;rg.h.ah=0;int86(0x16,&rg,&rg);return rg.h.ah;getchar();} void box(int startx,int starty,int high,int width)/*的建立*/ { int i;gotoxy(startx,starty);putch(0xda);for(i=startx+1;i for(i=starty+1;i 屏幕 } gotoxy(startx,starty+high-1);putch(0xc0);gotoxy(startx+1,starty+high-1);for(i=startx+1;i 通過這次的作業(yè)我覺得最大的收獲是不僅把平時學習到的知識理解的更加透徹,而且使知識更加系統(tǒng)化,同時還把有些平時不太注意的小問題發(fā)現(xiàn)了出來,這不但有利于我學習C語言,而且對于我學習任何一門課程都是很有益處的??傊?,做這份作業(yè)對于我們學習C語言有很大的幫助。 在做課程設(shè)計時,由于運用了很多新知識,新的方法,還有題目更加復(fù)雜,應(yīng)用性更強,在編寫過程中遇到了很多困難,從而使自己能夠?qū)W習到更多以前不懂,難懂的東西。 #include #include #include void sub(int a[MAX],int b[MAX] ,int c[MAX]); struct slink { int bignum[MAX];/*bignum[98]用來標記正負號,1正,0負bignum[99]來標記實際長度*/ struct slink *next;}; /*/-------自己建立的大數(shù)運算庫------*/ void print(int a[MAX]) { int i; for(i=0;i printf(“%d”,a[a[99]-i-1]); printf(“nn”); return; } int cmp(int a1[MAX],int a2[MAX]){ int l1, l2;int i;l1=a1[99];l2=a2[99];if(l1>l2) return 1; if(l1 return-1; for(i=(l1-1);i>=0;i--) { if(a1[i]>a2[i]) return 1; if(a1[i] return-1; } return 0;} void mov(int a[MAX],int *b){ int j; for(j=0;j b[j]=a[j]; return;} void mul(int a1[MAX],int a2[MAX],int *c){ int i,j;int y;int x;int z;int w;int l1, l2;l1=a1[MAX-1];l2=a2[MAX-1];if(a1[MAX-2]=='-'&& a2[MAX-2]=='-') c[MAX-2]=0;else if(a1[MAX-2]=='-') c[MAX-2]='-';else if(a2[MAX-2]=='-') c[MAX-2]='-';for(i=0;i for(j=0;j { x=a1[i]*a2[j]; y=x/10; z=x%10; w=i+j; c[w]=c[w]+z; c[w+1]=c[w+1]+y+c[w]/10; c[w]=c[w]%10; } } w=l1+l2;if(c[w-1]==0)w=w-1;c[MAX-1]=w;return;} void add(int a1[MAX],int a2[MAX],int *c){ int i,l1,l2;int len,temp[MAX];int k=0;l1=a1[MAX-1];l2=a2[MAX-1];if((a1[MAX-2]=='-')&&(a2[MAX-2]=='-')){ c[MAX-2]='-';} else if(a1[MAX-2]=='-'){ mov(a1,temp);temp[MAX-2]=0;sub(a2,temp,c);return;} else if(a2[MAX-2]=='-'){ mov(a2,temp);temp[98]=0;sub(a1,temp,c);return;} if(l1 c[i]=(a1[i]+a2[i]+k)%10; k=(a1[i]+a2[i]+k)/10;} if(l1>len){ for(i=len;i { c[i]=(a1[i]+k)%10; k=(a1[i]+k)/10; } if(k!=0) { c[l1]=k; len=l1+1; } else len=l1;} else { for(i=len;i { c[i]=(a2[i]+k)%10; k=(a2[i]+k)/10; } if(k!=0) { c[l2]=k; len=l2+1; } else len=l2;} c[99]=len; return;} void sub(int a1[MAX],int a2[MAX],int *c){ int i,l1,l2;int len,t1[MAX],t2[MAX];int k=0;l1=a1[MAX-1];l2=a2[MAX-1];if((a1[MAX-2]=='-')&&(a2[MAX-2]=='-')){ mov(a1,t1); mov(a2,t2);t1[MAX-2]=0; t2[MAX-2]=0;sub(t2,t1,c);return;} else if(a2[MAX-2]=='-'){ mov(a2,t2);t2[MAX-2]=0;add(a1,t2,c);return;} else if(a1[MAX-2]=='-'){ mov(a2,t2);t2[MAX-2]='-';add(a1,t2,c);return;} if(cmp(a1,a2)==1){ len=l2;for(i=0;i if((a1[i]-k-a2[i])<0){ c[i]=(a1[i]-a2[i]-k+10)%10; k=1;} else { c[i]=(a1[i]-a2[i]-k)%10; k=0; } } for(i=len;i { if((a1[i]-k)<0){ c[i]=(a1[i]-k+10)%10; k=1;} else { c[i]=(a1[i]-k)%10; k=0; } } if(c[l1-1]==0)/*使得數(shù)組C中的前面所以0字符不顯示了,如1000-20=0980--->顯示為980了*/ { len=l1-1; i=2; while(c[l1-i]==0)/*111456-111450=00006,消除0后變成了6;*/ { len=l1-i; i++; } } else { len=l1; } } else if(cmp(a1,a2)==(-1)){ c[MAX-2]='-'; len=l1; for(i=0;i if((a2[i]-k-a1[i])<0){ c[i]=(a2[i]-a1[i]-k+10)%10; k=1;} else { c[i]=(a2[i]-a1[i]-k)%10; k=0; } } for(i=len;i { if((a2[i]-k)<0){ c[i]=(a2[i]-k+10)%10; k=1;} else { c[i]=(a2[i]-k)%10; k=0; } } if(c[l2-1]==0) { len=l2-1; i=2; while(c[l1-i]==0) { len=l1-i; i++; } } else len=l2; } else if(cmp(a1,a2)==0) { len=1; c[len-1]=0; } c[MAX-1]=len;return;} void mod(int a[MAX],int b[MAX],int *c)/*/c=a mod b//注意:經(jīng)檢驗知道此處A和C的數(shù)組都改變了。*/ { int d[MAX];mov(a,d);while(cmp(d,b)!=(-1))/*/c=a-b-b-b-b-b.......until(c sub(d,b,c); mov(c,d);/*/c復(fù)制給a*/ } return;} void divt(int t[MAX],int b[MAX],int *c ,int *w)/*//試商法//調(diào)用以后w為a mod b, C為a div b;*/ { int a1,b1,i,j,m;/*w用于暫時保存數(shù)據(jù)*/ int d[MAX],e[MAX],f[MAX],g[MAX],a[MAX]; mov(t,a); for(i=0;i e[i]=0;for(i=0;i d[i]=0;for(i=0;i b1=b[MAX-1];if(cmp(a,b)==(-1)){ c[0]=0; c[MAX-1]=1; mov(t,w); return;} else if(cmp(a,b)==0){ c[0]=1; c[MAX-1]=1; w[0]=0; w[MAX-1]=1; return;} m=(a1-b1); for(i=m;i>=0;i--)/*341245/3=341245-300000*1--->41245-30000*1--->11245-3000*3--->2245-300*7--->145-30*4=25--->25-3*8=1*/ { for(j=0;j d[j]=0; d[i]=1; d[MAX-1]=i+1; mov(b,g); mul(g,d,e); while(cmp(a,e)!=(-1)) { c[i]++; sub(a,e,f); mov(f,a);/*f復(fù)制給g*/ } for(j=i;j e[j]=0; } mov(a,w);if(c[m]==0)c[MAX-1]=m;else c[MAX-1]=m+1; return;} void mulmod(int a[MAX] ,int b[MAX] ,int n[MAX],int *m)/*解決 了 m=a*b mod n;*/ { int c[MAX],d[MAX];int i;for(i=0;i d[i]=c[i]=0;mul(a,b,c); divt(c,n, d,m); for(i=0;i printf(“%d”,m[m[MAX-1]-i-1]); printf(“nm length is : %d n”,m[MAX-1]);} /*接下來的重點任務(wù)是要著手解決 m=a^p mod n的函數(shù)問題。*/ void expmod(int a[MAX] ,int p[MAX] ,int n[MAX],int *m){ int t[MAX],l[MAX],temp[MAX];/*/t放入2,l放入1;*/ int w[MAX],s[MAX],c[MAX],b[MAX],i;for(i=0;i b[i]=l[i]=t[i]=w[i]=0;t[0]=2;t[MAX-1]=1;l[0]=1;l[MAX-1]=1; mov(l,temp);mov(a,m); mov(p,b); while(cmp(b,l)!=0){ for(i=0;i divt(b,t,w,c);/*// c=p mod 2 w= p /2*/ mov(w,b);/*//p=p/2*/ if(cmp(c,l)==0)/*/余數(shù)c==1*/ { for(i=0;i mul(temp,m,w); mov(w,temp); for(i=0;i divt(temp,n,w,c);/* /c為余c=temp % n,w為商w=temp/n */ mov(c,temp);} for(i=0;i mul(m,m,s);//s=a*a for(i=0;i divt(s,n,w,c);/*/w=s/n;c=s mod n*/ mov(c,m);} for(i=0;i mul(m,temp,s); for(i=0;i divt(s,n,w,c); mov(c,m);/*余數(shù)s給m*/ m[MAX-2]=a[MAX-2];/*為后面的漢字顯示需要,用第99位做為標記*/ return;/*/k=temp*k%n;*/ } int is_prime_san(int p[MAX]){ int i,a[MAX],t[MAX],s[MAX],o[MAX]; for(i=0;i s[i]=o[i]=a[i]=t[i]=0; t[0]=1; t[MAX-1]=1; a[0]=2;// { 2,3,5,7 } a[MAX-1]=1; sub(p,t,s); expmod(a, s, p ,o); if(cmp(o,t)!= 0) { return 0; } a[0]=3; for(i=0;i expmod(a, s, p ,o); if(cmp(o,t)!= 0) { return 0; } a[0]=5; for(i=0;i expmod(a, s, p ,o); if(cmp(o,t)!= 0) { return 0; } a[0]=7; for(i=0;i expmod(a, s, p ,o); if(cmp(o,t)!= 0) { return 0; } return 1;} int coprime(int e[MAX],int s[MAX])/*//// 求兩個大數(shù)之間是否互質(zhì)////*/ { int a[MAX],b[MAX],c[MAX],d[MAX],o[MAX],l[MAX]; int i;for(i=0;i l[i]=o[i]=c[i]=d[i]=0;o[0]=0;o[MAX-1]=1;l[0]=1;l[MAX-1]=1;mov(e,b);mov(s,a);do { if(cmp(b,l)==0){ return 1;} for(i=0;i }while(cmp(c,o)!=0);/* printf(“Ihey are not coprime!n”);*/ return 0;} void prime_random(int *p,int *q){ int i,k;time_t t; p[0]=1; q[0]=3; // p[19]=1;// q[18]=2; p[MAX-1]=10; q[MAX-1]=11; do { t=time(NULL); srand((unsigned long)t);for(i=1;i k=rand()%10;} p[p[MAX-1]-1]=k; }while((is_prime_san(p))!=1); printf(“素數(shù) p 為 : ”); for(i=0;i printf(“nn”); do { t=time(NULL); srand((unsigned long)t);for(i=1;i }while((is_prime_san(q))!=1); printf(“素數(shù) q 為 : ”); for(i=0;i printf(“nn”);return;} void erand(int e[MAX],int m[MAX]){ int i,k;time_t t;e[MAX-1]=5;printf(“隨機產(chǎn)生一個與(p-1)*(q-1)互素的 e :”); do { t=time(NULL); srand((unsigned long)t);for(i=0;i k=rand()%10;e[e[MAX-1]-1]=k;}while(coprime(e, m)!=1); for(i=0;i printf(“nn”);return;} void rsad(int e[MAX],int g[MAX],int *d){ int r[MAX],n1[MAX],n2[MAX],k[MAX],w[MAX];int i,t[MAX],b1[MAX],b2[MAX],temp[MAX];mov(g,n1);mov(e,n2);for(i=0;i k[i]=w[i]=r[i]=temp[i]=b1[i]=b2[i]=t[i]=0;b1[MAX-1]=0;b1[0]=0;/*/b1=0;*/ b2[MAX-1]=1;b2[0]=1;/*/b2=1;*/ while(1){ for(i=0;i k[i]=w[i]=0; divt(n1,n2,k,w);/*/k=n1/n2;*/ for(i=0;i temp[i]=0; mul(k,n2,temp);/*/temp=k*n2;*/ for(i=0;i r[i]=0; sub(n1,temp,r); if((r[MAX-1]==1)&&(r[0]==0))/*/r=0*/ { break; } else { mov(n2,n1);/*/n1=n2;*/ mov(r,n2);/*/n2=r;*/ mov(b2, t);/*/t=b2;*/ for(i=0;i temp[i]=0; mul(k,b2,temp);/*/b2=b1-k*b2;*/ for(i=0;i b2[i]=0; sub(b1,temp,b2); mov(t,b1); } } for(i=0;i t[i]=0; add(b2,g,t); for(i=0;i temp[i]=d[i]=0; divt(t,g,temp,d); printf(“由以上的(p-1)*(q-1)和 e 計算得出的 d : ”); for(i=0;i printf(“nn”);} /*/求解密密鑰d的函數(shù)(根據(jù)Euclid算法)***68000*/ unsigned long rsa(unsigned long p,unsigned long q,unsigned long e)/*/求解密密鑰d的函數(shù)(根據(jù)Euclid算法)*/ { unsigned long g,k,r,n1,n2,t;unsigned long b1=0,b2=1; g=(p-1)*(q-1);n1=g;n2=e; while(1){ k=n1/n2; r=n1-k*n2; if(r!=0) { n1=n2; n2=r; t=b2; b2=b1-k*b2; b1=t; } else { break; } } return(g+b2)%g;} /*/-----------導(dǎo)入導(dǎo)出公鑰和私鑰-----/*/ void loadpkey(int e[MAX],int n[MAX])//導(dǎo)入公鑰 { FILE *fp;char filename[25],str[MAX],ch;int i,k;for(i=0;i e[i]=n[i]=0;while(1){ printf(“n”);printf(“為導(dǎo)入(e,n),請輸入加密密鑰對文件路徑: n”); scanf(“%s”,filename); if((fp=fopen(filename,“r”))==NULL) printf(“輸入的文件不存在,請重新輸入!n”); else break;} k=0; while((ch=fgetc(fp))!=EOF) { if(ch!=' ') { str[k]=ch; k++; } else { for(i=0;i { e[i]=str[k-i-1]-48; } e[MAX-1]=k; k=0; } } for(i=0;i n[i]=str[k-i-1]-48; n[MAX-1]=k; printf(“n加密密鑰 e : ”); for(i=0;i printf(“%d”,e[e[MAX-1]-i-1]); printf(“n”); printf(“n 公鑰 n : ”); for(i=0;i printf(“%d”,n[n[MAX-1]-i-1]); printf(“n”); fclose(fp); printf(“n導(dǎo)入(e,n)成功!n”); getchar();} void loadskey(int d[MAX],int n[MAX])//導(dǎo)入私鑰 { { FILE *fp;char filename[25],str[MAX],ch;int i,k;for(i=0;i d[i]=n[i]=0;while(1){ printf(“為導(dǎo)入(d,n),請輸入解密密鑰對文件的路徑: n”); scanf(“%s”,filename); if((fp=fopen(filename,“r”))==NULL) { printf(“輸入的文件不存在,請重新輸入!n”); } else break;} k=0; while((ch=fgetc(fp))!=EOF) { if(ch!=' ') { str[k]=ch; k++; } else { for(i=0;i { d[i]=str[k-i-1]-48; } d[MAX-1]=k; k=0; } } for(i=0;i n[i]=str[k-i-1]-48; n[MAX-1]=k; printf(“n解密密鑰 d : ”); for(i=0;i printf(“%d”,d[d[MAX-1]-i-1]); printf(“n”); printf(“n 公鑰 n : ”); for(i=0;i printf(“%d”,n[n[MAX-1]-i-1]); printf(“n”); fclose(fp); printf(“n導(dǎo)入(d,n)成功!n”); getchar();} } void savepkey(int e[MAX],int n[MAX])//導(dǎo)出公鑰 { FILE *fp; int i; char savefile[25],ch;printf(“導(dǎo)出加密密鑰(e,n),存放的文件路徑為: ”); scanf(“%s”,savefile);printf(“n”); fp=fopen(savefile,“w”);for(i=0;i ch=e[e[MAX-1]-i-1]+48; fputc(ch,fp);} ch=' ';fputc(ch,fp);for(i=0;i ch=n[n[MAX-1]-i-1]+48; fputc(ch,fp);} fclose(fp);printf(“n保存(e,n)操作完成!n”);} void saveskey(int d[MAX],int n[MAX])//導(dǎo)出私鑰 { FILE *fp; int i; char savefile[25],ch;printf(“導(dǎo)出解密密鑰(d,n),存放的文件路徑為: ”); scanf(“%s”,savefile);printf(“n”); fp=fopen(savefile,“w”);for(i=0;i ch=d[d[MAX-1]-i-1]+48; fputc(ch,fp);} ch=' ';fputc(ch,fp);for(i=0;i ch=n[n[MAX-1]-i-1]+48; fputc(ch,fp);} fclose(fp);printf(“n保存(d,n)操作完成!n”); } /*/-----------加密和解密的塊-----/*/ void printbig(struct slink *h){ struct slink *p; int i; p=(struct slink *)malloc(LEN); p=h; if(h!=NULL)do { for(i=0;i bignum[MAX-1];i++) printf(“%d”,p->bignum[p->bignum[MAX-1]-i-1]); p=p->next;} while(p!=NULL); printf(“nn”); } void tencrypto(int e[MAX], int n[MAX])/*//對有需要的文件進行加密*/ { FILE *fp; int i,k,count,temp,c; char filename[25],ch,encryfile[25]; struct slink *p,*p1,*p2; struct slink *h; h=p=p1=p2=(struct slink *)malloc(LEN); h=NULL; printf(“n輸入需要加密的文件路徑 : ”); scanf(“%s”,filename); if((fp=fopen(filename,“r”))==NULL) { printf(“Cannot open file!n”); exit(0); } printf(“n文件的原文內(nèi)容:nn”); count=0; while((ch=fgetc(fp))!=EOF) { putchar(ch); c=ch; k=0;if(c<0){ c=abs(c);/*/把負數(shù)取正并且做一個標記*/ p1->bignum[MAX-2]='0';} else { p1->bignum[MAX-2]='1';} while(c/10!=0){ temp=c%10; c=c/10; p1->bignum[k]=temp; k++;} p1->bignum[k]=c; p1->bignum[MAX-1]=k+1;count=count+1;if(count==1) h=p1;else p2->next=p1;p2=p1; p1=(struct slink *)malloc(LEN);} p2->next=NULL; printf(“n”); fclose(fp); // printf(“加密后文件的保存路徑 : n”);// scanf(“%s”,encryfile);// fp=fopen(encryfile,“w”); fp=fopen(filename,“w”); p=p1=(struct slink *)malloc(LEN); p=h; printf(“n加密后文件中所形成密文:nn”); if(h!=NULL)do { expmod(p->bignum , e ,n ,p1->bignum); ch=p1->bignum[MAX-2]; printf(“%c”,ch); fputc(ch,fp); if((p1->bignum[MAX-1]/10)==0)/*/判斷p1->bignum[99]的是否大于十;*/ { ch=0+48; printf(“%c”,ch); fputc(ch,fp); ch=p1->bignum[MAX-1]+48; printf(“%c”,ch); fputc(ch,fp); } else { ch=p1->bignum[MAX-1]/10+48; printf(“%c”,ch); fputc(ch,fp); ch=p1->bignum[MAX-1]%10+48; printf(“%c”,ch); fputc(ch,fp); } for(i=0;i bignum[MAX-1];i++) { printf(“%d”,p1->bignum[i]); ch=p1->bignum[i]+48; fputc(ch,fp); } p=p->next; p1=(struct slink *)malloc(LEN);}while(p!=NULL);printf(“nn”); fclose(fp);return;} void tdecrypto(int d[MAX], int n[MAX]){ FILE *fp; struct slink *h,*p1,*p2; char ch,encryfile[25],decryfile[25]; int i,j,k,c,count,temp; printf(“n輸入加密過的文件路徑 : ”); scanf(“%s”,encryfile); if((fp=fopen(encryfile,“r”))==NULL) { printf(“此文件不存在!n”); exit(0); } printf(“n文件中密文內(nèi)容:nn”); i=0; j=3; count=0; h=p1=p2=(struct slink *)malloc(LEN); while((ch=fgetc(fp))!=EOF) { putchar(ch); c=ch; if(j==3) { p1->bignum[MAX-2]=c; j--; } else if(j==2) { temp=c-48; j--; } else if(j==1) { p1->bignum[MAX-1]=temp*10+c-48; j--; } else if(j==0) { p1->bignum[i]=c-48; i++; if(i==p1->bignum[MAX-1]) { i=0; j=3; count++; if(count==1) h=p1; else p2->next=p1; p2=p1; p1=(struct slink *)malloc(LEN); } } } p2->next=NULL; printf(“n”); fclose(fp); // printf(“解密后的明文文件保存路徑 : n”);// scanf(“%s”,decryfile);// fp=fopen(decryfile,“w”); fp=fopen(encryfile,“w”);printf(“n解密密文后文件中的明文:nn”);p2=(struct slink *)malloc(LEN); p1=h;k=0;if(h!=NULL)/*/temp為暫存ASIIC碼的int值*/ do { for(i=0;i p2->bignum[i]=0; expmod(p1->bignum , d ,n ,p2->bignum); temp=p2->bignum[0]+p2->bignum[1]*10+p2->bignum[2]*100; if((p2->bignum[MAX-2])=='0') { temp=0-temp; }/*/轉(zhuǎn)化為正確的ASIIC碼,如-78-96形成漢字 */ ch=temp;/* str[k]--->ch */ printf(“%c”,ch);/* str[k]--->ch */ fputc(ch,fp);/*/寫入文件str[k]--->ch*/ k++; p1=p1->next; p2=(struct slink *)malloc(LEN); }while(p1!=NULL); printf(“nn”); fclose(fp);return;} struct slink *input(void)/*/輸入明文并且返回頭指針,沒有加密時候轉(zhuǎn)化的數(shù)字*/ { struct slink *head; struct slink *p1,*p2; int i,n,c,temp; char ch; n=0;p1=p2=(struct slink *)malloc(LEN);head=NULL;printf(“n請輸入你所要加密的內(nèi)容 : n”);while((ch=getchar())!='n') { i=0;c=ch;if(c<0){ c=abs(c);/*/把負數(shù)取正并且做一個標記*/ p1->bignum[MAX-2]='0';} else { p1->bignum[MAX-2]='1';} while(c/10!=0){ temp=c%10; c=c/10; p1->bignum[i]=temp; i++;} p1->bignum[i]=c; p1->bignum[MAX-1]=i+1;n=n+1;if(n==1) head=p1;else p2->next=p1;p2=p1; p1=(struct slink *)malloc(LEN);} p2->next=NULL; return(head);} struct slink *jiami(int e[MAX],int n[MAX],struct { struct slink *p; struct slink *h; struct slink *p1,*p2; int m=0,i;printf(“n”); printf(“加密后形成的密文內(nèi)容:n”);p1=p2=(struct slink*)malloc(LEN);h=NULL; p=head; if(head!=NULL)do slink *head){ expmod(p->bignum , e ,n ,p1->bignum); for(i=0;i bignum[MAX-1];i++){ printf(“%d”,p1->bignum[p1->bignum[MAX-1]-1-i]);} m=m+1;if(m==1) h=p1;else p2->next=p1;p2=p1; p1=(struct slink *)malloc(LEN); p=p->next;} while(p!=NULL);p2->next=NULL; p=h; printf(“n”); return(h); } void jiemi(int d[MAX],int n[MAX],struct slink *h){ int i,j,temp; struct slink *p,*p1; char ch[65535]; p1=(struct slink*)malloc(LEN); p=h; j=0; if(h!=NULL) do { for(i=0;i p1->bignum[i]=0; expmod(p->bignum , d ,n ,p1->bignum); temp=p1->bignum[0]+p1->bignum[1]*10+p1->bignum[2]*100; if((p1->bignum[MAX-2])=='0') { temp=0-temp; } ch[j]=temp; j++; p=p->next;}while(p!=NULL);printf(“n”);printf(“解密密文后所生成的明文:n”);for(i=0;i printf(“%c”,ch[i]);printf(“n”);return; } void menu(){ printf(“nnn”);printf(“nnn”);printf(“ R--------產(chǎn)生密鑰對 nnn”); printf(“ S--------保存密鑰對 nnn”);printf(“ L--------載入密鑰對 nnn”);printf(“ E--------對文件加密 nnn”);printf(“ D--------對文件解密 nnn”);printf(“ T--------簡單測試 nnn”);printf(“ Q--------退出 nnn”);printf(“請選擇一種操作:”);} /*/------------------主MAIN函數(shù)----/*/ void main(){ int i; char c; int p[MAX],q[MAX],n[MAX],d[MAX],e[MAX],m[MAX],p1[MAX],q1[MAX];struct slink *head,*h1,*h2; for(i=0;i m[i]=p[i]=q[i]=n[i]=d[i]=e[i]=0;/*/簡單初始化一下*/ while(1) { menu(); c=getchar(); getchar();//接受回車符 if((c=='r')||(c=='R'))//操作r產(chǎn)生密鑰對 { for(i=0;i m[i]=p[i]=q[i]=n[i]=d[i]=e[i]=0; printf(“nnnnnnnnn”); printf(“nn隨機密鑰對產(chǎn)生如下:nn”); prime_random(p,q);/*/隨機產(chǎn)生兩個大素數(shù)*/ mul(p,q,n); printf(“由 p、q 得出 n :”); print(n); mov(p,p1); p1[0]--; mov(q,q1); q1[0]--; /*/q-1;*/ mul(p1,q1,m);//m=(p-1)*(q-1) erand(e,m); rsad(e,m,d); printf(“密鑰對產(chǎn)生完成,現(xiàn)在可以直接進行加解密文件!n”); printf(“n按任意鍵回主菜單…………”); getchar();} else if((c=='l')||(c=='L')) { printf(“nn選擇導(dǎo)入密鑰類型:加密密鑰(P)還是解密密鑰(S)?”); c=getchar(); getchar(); if((c=='p')||(c=='P')) loadpkey(e,n); else if((c=='s')||(c=='S')) loadskey(d,n); printf(“n按任意鍵回主菜單…………”); getchar(); } else if((c=='e')||(c=='E')) { tencrypto(e, n); printf(“n加密文件操作完成!n”); printf(“n按任意鍵回主菜單…………”); getchar(); getchar(); } else if((c=='d')||(c=='D')) { tdecrypto(d, n); printf(“n解密文件操作完成!n”); printf(“n按任意鍵回主菜單…………”); getchar(); getchar(); } else if((c=='s')||(c=='S')) { savepkey(e,n); printf(“n”); saveskey(d,n); printf(“n按任意鍵回主菜單…………”); getchar(); getchar(); } else if((c=='T')||(c=='t')) { head=input(); h1=jiami(e, n, head); jiemi(d, n, h1); printf(“nRSA測試工作完成!n”); printf(“n按任意鍵回主菜單…………”); getchar(); } else if((c=='Q')||(c=='q')) break; } } #include //編號// char name[20]; //起點和終點// char time[5]; //出發(fā)時間// int price; //車票價格// int amount; //剩余數(shù)量// struct Node *next;}Node;//創(chuàng)建鏈表并輸入數(shù)據(jù)// struct Node *creat(){ struct Node *head,*r,*s; int i=0; char choice; head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;r=head;do { s=(struct Node *)malloc(sizeof(struct Node));s->next=NULL;printf(“請輸入第%d種火車票的信息:n”,++i);printf(“請輸入火車的編號:”); scanf(“%d”,&s->num); printf(“起點和終點:”);scanf(“%s”,s->name);printf(“出發(fā)時間:”);scanf(“%s”,s->time);printf(“車票價格:”);scanf(“%d”,&s->price);printf(“剩余數(shù)量:”);scanf(“%d”,&s->amount); r->next=s; r=s; printf(“Continue?(Y/N)”);scanf(“%s”,&choice);}while(choice=='Y'||choice=='y'); r->next=NULL;return(head);} //將單鏈表中的信息保存到文件1.txt中// void save(struct Node *h){ struct Node *s;FILE *fp; char filename[10]=“1.txt”; fp=fopen(“1.txt”,“wt”);if(fp==NULL){ printf(“n寫文件出錯,按任意鍵退出!”);getchar();exit(1);} for(s=h->next;s!=NULL;s=s->next) fprintf(fp,“%d %s %s %d %d n”,s->num,s->name,s->time,s->price,s->amount); getchar();fclose(fp);} // 從文件1.txt中讀取信息并存入單鏈表中// struct Node *read(){ struct Node *head,*r,*s;FILE *fp;char filename[10]=“zl.txt”;fp=fopen(“1.txt”,“rt”);if(fp==NULL){ printf(“讀文件錯誤,按任意鍵退出!”);getchar();exit(1);} head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;r=head;while(!feof(fp)){ s=(struct Node *)malloc(sizeof(struct Node));fscanf(fp,“%d %s %s %d %d”,&s->num,s->name,s->time,&s->price,&s->amount); r->next=s;r=s; } r->next=NULL;fclose(fp); return head;} //將鏈表中的數(shù)據(jù)輸出// void print(struct Node *h){ struct Node *s; printf(“n火車票信息如下:n”); printf(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n”);printf(“編號 起點和終點 出發(fā)時間 車票價格 剩余票數(shù):n”); for(s=h->next;s->next!=NULL;s=s->next){ printf(“ %d %10s %5s %10d %6dn”,s->num,s->name,s->time,s->price,s->amount);} } //鏈表查詢// struct Node * find(struct Node *h){ int i,j;char s[20];printf(“tt 查詢方法有以下幾種:n”);printf(“tt 1.火車票編號n”);printf(“tt 2.起點和終點n”);printf(“tt 3.出發(fā)時間n”);printf(“tt 4.車票價格n”);printf(“tt 5.剩余票數(shù)n”);printf(“請輸入您要查詢的方法的序號:”);scanf(“%d”,&i);switch(i){ case 1:printf(“請輸入你要查詢火車票的編號:”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->num==j)return h; } return NULL;break;case 2:printf(“請輸入您要查詢火車票的起點和終點:”);scanf(“%s”,s);while(h->next!=NULL){ h=h->next;if(strcmp(h->name,s)==0) return h; } return NULL;break;case 3:printf(“請輸入您要查詢火車票的時間:”); scanf(“%s”,s); while(h->next!=NULL) { h=h->next; if(strcmp(h->time,s)==0) return h; } return NULL; break;case 4:printf(“請輸入你要查詢火車票的價格 :”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->price==j) return h; } return NULL; break;case 5:printf(“請輸入你要查詢火車票的剩余票數(shù):”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->amount==j) return h; } return NULL; break;} } //修改信息// change(struct Node *h,int k){ int j;struct Node *p;p=find(h);printf(“------------n”);printf(“t 您要修改哪一項?n”);printf(“t 1.火車編號n”);printf(“t 2.起點和終點n”);printf(“t 3.出發(fā)時間n”);printf(“t 4.車票價格n”); printf(“t 5.剩余票數(shù)n”);printf(“t 0.退出系統(tǒng)n”); printf(“------------n”);printf(“請輸入您要修改項的編號:”);scanf(“%d”,&j);switch(j) { case 1: printf(“修改后的火車編號:”); scanf(“%d”,&p->num); break; case 2: printf(“修改后的起點和終點:”); scanf(“%s”,p->name); break; case 3: printf(“修改后的出發(fā)時間:”); scanf(“%s”,p->time); break; case 4: printf(“修改后的車票價格:”); scanf(“%d”,&p->price); break; case 5: printf(“修改后的剩余票數(shù):”); scanf(“%d”,&p->amount); break; case 0:break;} } //刪除信息// delete(struct Node *h){ struct Node *p; int j; printf(“請輸入您要刪除的火車票的編號:”);scanf(“%d”,&j);p=h->next; if(p==NULL) return 0;while(p!=NULL){ if(p->num==j){ h->next=p->next; free(p); return 1;} h=p;p=p->next; } return 0;} //添加信息// void append(){ struct Node *p; FILE *fp; fp=fopen(“1.txt”,“at+”); if(fp==NULL) { printf(“寫文件出錯,按任意鍵返回.n”);getchar();exit(1); } printf(“請輸入要添加的火車票的信息:火車編號,起點和終點,出發(fā)時間,車票價格,剩余票數(shù):n”);scanf(“%d%s%s%d%d”,&p->num,p->name,p->time,&p->price,&p->amount);fprintf(fp,“%d %s %s %d %dn”,p->num,p->name,p->time,p->price,p->amount);getchar();fclose(fp);} //數(shù)據(jù)的統(tǒng)計// void count(struct Node *h){ struct Node *s;s=h;int i,j,k,n=0;printf(“*****************************************************************************n”); printf(“tt 請選擇您要統(tǒng)計項目的序號:n”); printf(“tt 1.車票價格n”); printf(“tt 2.剩余票數(shù)n”);printf(“tt 0.退出界面n”); scanf(“%d”,&i);switch(i) { case 1: printf(“請輸入您要統(tǒng)計車票的價格的標準:”); scanf(“%d”,&j); printf(“tt 請選擇低于或高于標準:n”); printf(“tt 1.價格低于%d的個數(shù)n”,j); printf(“tt 2.價格高于%d的個數(shù)n”,j); scanf(“%d”,&k); if(k==1) { for(s=h->next;s->next!=NULL;s=s->next) if(s->price n++; printf(“車票價格低于%d的個數(shù)有%d個.n”,j,n); } else { for(s=h->next;s->next!=NULL;s=s->next) if(s->price>j) n++; printf(“車票價格低于%d的個數(shù)有%d個.n”,j,n); } break; case 2: printf(“請輸入您要統(tǒng)計剩余票數(shù)的數(shù)量:”); scanf(“%d”,&j); printf(“tt 請選擇低于或高于所輸票數(shù):n”); printf(“tt 1.票數(shù)低于%d的個數(shù)n”,j); printf(“tt 2.票數(shù)高于%d的個數(shù)n”,j); scanf(“%d”,&k); if(k==1) { for(s=h->next;s->next!=NULL;s=s->next) if(s->amount n++; printf(“剩余票數(shù)低于%d的個數(shù)有%d個.n”,j,n); } else { for(s=h->next;s->next!=NULL;s=s->next) if(s->amount>j) n++; printf(“剩余票數(shù)高于%d的個數(shù)有%d個.n”,j,n); } break; case 0:break; } } //保存用戶和密碼到文件2.txt中// void save_user(){ char file[10]=“2.txt”;FILE *fp;char name[20];char pwd[10];fp=fopen(“2.txt”,“at+”);if(fp==NULL){ printf(“n寫文件出錯,按任意鍵退出.n”); getchar();exit(1);} printf(“請輸入用戶名:”); scanf(“%s”,name);printf(“請輸入密碼:”); scanf(“%s”,pwd); fprintf(fp,“%s %sn”,name,pwd); getchar(); fclose(fp); printf(“用戶注冊成功!n”);} //檢驗用戶和密碼是否匹配// int check(char *name,char *pwd){ char name1[20];char pwd1[10];FILE *fp;char file[10]=“2.txt”;if((fp=fopen(“2.txt”,“rt”))==NULL){ printf(“讀文件出錯,按任意鍵退出!n”); getchar(); exit(1);} while(!feof(fp)){ fscanf(fp,“%s %s”,name1,pwd1); if(strcmp(name1,name)==0&&strcmp(pwd1,pwd)==0) return 1;} return 0;} //數(shù)據(jù)排序// void sort(struct Node *h){ struct Node *s,*p,*m,*n;int t,t1,t2,t3;char s1[20];char s2[10]; printf(“車票價格由小到大排序如下:n”);for(s=h->next;s->next!=NULL;s=s->next)for(p=s->next;p->next!=NULL;p=p->next) if(s->price>p->price) { t1=s->num;s->num=p->num;p->num=t1; t2=s->price;s->price=p->price;p->price=t2; t3=s->amount;s->amount=p->amount;p->amount=t3; strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1); strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2); } print(h);printf(“nn剩余車票數(shù)量由多到少排序如下:n”);for(s=h->next;s->next!=NULL;s=s->next) for(p=s->next;p->next!=NULL;p=p->next) if(s->amount amount) { t1=s->num;s->num=p->num;p->num=t1; t2=s->price;s->price=p->price;p->price=t2; t3=s->amount;s->amount=p->amount;p->amount=t3; strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1); strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2); } print(h);} void main(){ struct Node *head,*p;int i,j,k;head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;char name[20];char pwd[10];printf(“n***************歡迎進入火車票管理系統(tǒng)******************n”);printf(“tt 1.用戶登錄n”);printf(“tt 2.用戶注冊n”);printf(“tt 0.退出系統(tǒng) n”);printf(“請輸入所選序號:”);scanf(“%d”,&k); switch(k){ case 1: printf(“請輸入用戶名:”); scanf(“%s”,name); printf(“請輸入密碼:”); scanf(“%s”,pwd); if(check(name,pwd)) { printf(“密碼正確.n”); do { printf(“nntt*********************歡迎進入火車票管理系統(tǒng)***********************n”); printf(“tt 1.錄入火車票信息tt 2.添加火車票信息n”); printf(“tt 3.修改火車票信息tt 4.刪除火車票信息n”); printf(“tt 5.打印火車票信息tt 6.查詢火車票信息n”); printf(“tt 7.統(tǒng)計火車票信息tt 8.火車票銷售排行n”); printf(“tt 0.退出系統(tǒng)n”); printf(“請輸入您要進入菜單的序號(0-8):”); scanf(“%d”,&i); switch(i) { case 1: printf(“請錄入火車票信息nn”); head=creat(); save(head); head=read(); break; case 2: append(); break; case 3: printf(“請輸入您要修改的火車票的編號:”); scanf(“%d”,&j); change(head,j); save(head); break; case 4: head=read(); if(delete(head)) { printf(“已正確刪除!n”); save(head); } else printf(“要刪除的結(jié)點不存在!n”); break; case 5: head=read(); print(head); break; case 6: printf(“請輸入您要查詢火車票的編號(以0結(jié)束):”); scanf(“%d”,&j); { p=find(head); printf(“編號 起點和終點 出發(fā)時間 車票價格 剩余票數(shù):n”); printf(“%d %10s %5s %10d %6dn”,p->num,p->name,p->time,p->price,p->amount); printf(“請繼續(xù)輸入序號(以0結(jié)束):”); scanf(“%d”,&j); } break; case 7: head=read();count(head);break; case 8: sort(head);break; case 0: printf(“************************用!*****************************n”);break; } }while(i!=0); } else printf(“密碼錯誤或用戶名不存在.n”); break;case 2:save_user();break;case 0:break;} 謝 謝 使 #include #include #include //將十進制數(shù)轉(zhuǎn)換成二進制,用于檢驗大素數(shù)p和q int zhuan_huan(int b,int a[],int k) { int t,temp=-1; while(b>0){ t=b%2; temp++; a[temp]=t; b=b/2; } return temp; } //歐幾里得算法,用于判斷加密指數(shù)e是否符合要求 int gcd(int n,int b) { int r1=n,r2=b,r; while(r2>0){ r=r1%r2; r1=r2; r2=r; } return r1; } //擴展歐幾里得算法求乘法逆元,即求解密指數(shù)d int extend(int n,int b) { int q,r,r1=n,r2=b,t,t1=0,t2=1,i=1; while(r2>0) { q=r1/r2; r=r1%r2; r1=r2;r2=r; t=t1-q*t2; t1=t2; t2=t; } if(t1>=0)return t1%n; else{ while((t1+i*n)<0) i++; return t1+i*n; } } //檢驗大素數(shù),符合要求返回1,否則返回0 int Witness(int a,int n) { int d=1,k,r=n-1,i,x,b[1000]; k=zhuan_huan(r,b,1000); for(i=k;i>=0;i--){ x=d; d=(d*d)%n; if((d==1)&&(x!=1)&&(x!=n-1))return 0; if(b[i]==1)d=(d*a)%n; } if(d!=1)return 0; else return 1; } //快速計算模指數(shù) int js_mod(int a,int b,int n) { int x=0,y=1,k,i,s[1000]; k=zhuan_huan(b,s,1000); for(i=k;i>=0;i--){ x=2*x; y=(y*y)%n; if(s[i]==1){ x++; y=(y*a)%n; } } return y; } //主函數(shù)。。。。。。。。。。。。。。。。。。。。。。 void main() { int p,q,e,d,n,yn,m[1000],c[10000];//c[10000]存放加密后的數(shù)字密文,m[1000]存放解密后的數(shù)字明文,即英文明文在zimu_biao[69]中的下標。 int i,j;//i,j用于循環(huán)遍歷數(shù)組 int mi_yue;//用戶輸入的密鑰 int count=1;//統(tǒng)計輸入密鑰的次數(shù),count>3時將不允許用戶再輸入。 char min_wen[1000],re_min_wen[1000];//分別為用戶輸入的明文、密文,解密后的明文。//密鑰生成char zimu_biao[69]=“abcdefghijklmnopqrstuvwxyz,ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789'.?!”; printf(“請輸入您要發(fā)送的明文文件(小寫英文表示):n”); printf(“******************************************************n”); gets(min_wen); printf(“******************************************************n”); printf(“n加密開始,請按要求操作。。nn”); printf(“請輸入第一個大素數(shù)p:n”); while(1){ scanf(“%d”,&p); if(Witness(2,p)==1){ printf(“您輸入的第一個大素數(shù) %d 符合要求n”,p); break; } else printf(“您輸入的 %d 不是素數(shù),請重新輸入:n”,p); } printf(“請輸入第二個大素數(shù)q:n”); while(1){ scanf(“%d”,&q); if(Witness(2,q)){ printf(“您輸入的第二個大素數(shù) %d 符合要求n”,q); break; } else printf(“您輸入的 %d 不是素數(shù),請重新輸入:n”,q); } n=p*q;yn=(p-1)*(q-1); printf(“請輸入加密指數(shù)(整數(shù))e,且0 scanf(“%d”,&e); if(gcd(yn,e)==1){ printf(“您輸入加密指數(shù) %d 與 %d 互素,符合要求n”,e,yn); break; } else printf(“您輸入加密指數(shù) %d 與 %d 不互素,請重新輸入。。n”,e,yn); } d=extend(yn,e);//求解密指數(shù)d printf(“nn請記住您的兩個大素數(shù)分別為p=%d(保密),q=%d(保密),模數(shù)n=%d(公開),歐拉函數(shù)yn=%d(保密),加密指數(shù)e=%d(公鑰,公開),。。解密指數(shù) d=%d(私鑰,保密)nn”,p,q,n,yn,e,d); //明文轉(zhuǎn)換過程 /* scanf(“%s”,min_wen); printf(“%s”,min_wen);*/ for(i=0;i for(j=0;j<68;j++)//for(j=0;j<26;j++) if(min_wen[i]==zimu_biao[j]) m[i]=j;//將字符串明文換成數(shù)字,并存到整型數(shù)組m里面,即明文的另一種表示方法 //加密過程 for(i=0;i c[i]=js_mod(m[i],e,n); printf(“輸出密文:n”); printf(“******************************************************n”); for(i=0;i printf(“%d”,c[i]); printf(“n******************************************************n”); //解密過程 for(i=0;i m[i]=js_mod(c[i],d,n); for(i=0;i re_min_wen[i]=zimu_biao[m[i]]; //提示用戶解密 printf(“nn您有3次輸入密鑰的機會,密鑰正確后將進行解密顯示明文,3次輸入錯誤解密將終止,請注意。。nn”); while(1){ scanf(“%d”,&mi_yue); if(mi_yue==d){ printf(“密鑰輸入正確,您得到的明文為:nn”); for(i=0;i printf(“%c”,re_min_wen[i]); printf(“nn”); break; } else{ }} }}printf(“您第%d次輸入的密鑰錯誤,請重新輸入。。n”,count);count++;if(count>3){printf(“n您已%d次輸入的密鑰錯誤,將不允許繼續(xù)輸入n”,count-1);break; #include //包含access函數(shù)的頭文件 #define N 9999 //定義最多的航班數(shù) #define PRINT “%dtt%stt%stt星期%stt%dn ”,s[i].num,s[i].start,s[i].over,s[i].time,s[i].count //宏定義輸出格式 struct air //定義結(jié)構(gòu)體數(shù)組 { int num; //定義航班號 char start[20];//航班起始站 char over[20];//終點站 char time[10];//飛行時間 int count; //機票數(shù)量 }s[N]; int i,m=0; //定義全局變量 char ii[10]; void add();//函數(shù)聲明增加航班信息函數(shù) void print(); //顯示航班信息 void search();//查找航班信息 void dingpiao();//訂票業(yè)務(wù) void tuipiao();//退票 void read();//讀取文件 void save();//保存文件 void output();//輸出格式 void paixu();//航班排序 void chushihua();//系統(tǒng)初始化 void build();//建立數(shù)據(jù)文件 void paixu1();//按航班號從小到大排序 void paixu2();//從大到小 void main()//主函數(shù) { int j; chushihua();//系統(tǒng)初始化判斷是否存在原始數(shù)據(jù)文件 printf(“ 歡迎使用飛機訂票系統(tǒng)n”);//打印出系統(tǒng)主界面 do { printf(“================================== ”); printf(“1.增加航班信息n” “t2.瀏覽航班信息n” “tt3.查找航班信息(按航班號)tt╮(╯_╰)╭n” “ttt4.航班排序(按航班號)n” “tttt5.訂票業(yè)務(wù)n” “to(︶︿︶)ottt6.退票業(yè)務(wù)n” “tttttt0.退出n”);printf(“================================== ”); printf(“請在0-6中選擇以回車鍵結(jié)束: ”);scanf(“%d”,&j);switch(j){ case 1: add();//調(diào)用增加航班函數(shù) break; case 2:print();//調(diào)用顯示模塊 break; case 3:search();//調(diào)用查找模塊 break; case 4:paixu();//調(diào)用排序函數(shù) break; case 5:dingpiao();//調(diào)用訂票模塊 break; case 6:tuipiao();//調(diào)用退票模塊 break; case 0: //退出系統(tǒng) save(); printf(“謝謝使用,再見!”); break;} }while(j!=0);//判斷是否調(diào)用其他函數(shù) } void chushihua()//定義系統(tǒng)初始化函數(shù) { if(access(“hangban.dat”,0)){ build();} else read();} void build()//定義建立數(shù)據(jù)文件函數(shù) { FILE *fp;//定義文件指針 if((fp=fopen(“hangban.dat”,“wb”))==NULL)//打開文件并判定是否出錯 { printf(“創(chuàng)建文件失敗!”);//打印出錯提示 getchar(); return;} printf(“請依次輸入航班信息(以回車鍵結(jié)束):n”); //打印提示信息 printf(“------------n”);for(i=0;i printf(“請輸入航班號: ”); scanf(“%d”,&s[i].num);//輸入航班號 printf(“請輸入起始站: ”); scanf(“%s”,s[i].start);//輸入起始站 printf(“請輸入終點站: ”); scanf(“%s”,s[i].over);//輸入終點站 printf(“請輸入時間(星期幾): ”); scanf(“%s”,s[i].time);//輸入時間 printf(“請輸入機票數(shù): ”); scanf(“%d”,&s[i].count);//輸入機票數(shù) fwrite(&s[i],sizeof(struct air),1,fp); m++; printf(“添加完畢,是否繼續(xù)添加?請鍵入y或n以回車鍵結(jié)束:”); scanf(“%s”,ii); if(strcmp(ii,“y”)!=0) //判斷是否繼續(xù)添加航班信息 { fclose(fp); //關(guān)閉文件 return; } } } void read() //定義讀取文件函數(shù) { FILE *fp;if((fp=fopen(“hangban.dat”,“r”))==NULL){ printf(“創(chuàng)建文件失敗!”); getchar(); return;} i=0;while(!feof(fp)){ fread(&s[i],sizeof(struct air),1,fp);//逐塊讀取數(shù)據(jù) i++; m++;//計算存在航班數(shù) } m--;fclose(fp);} void save()//定義保存函數(shù) { FILE *fp;if((fp=fopen(“hangban.dat”,“wb”))==NULL) { printf(“創(chuàng)建文件失敗!”); getchar(); return;} for(i=0;i //逐塊保存數(shù)據(jù) fwrite(&s[i],sizeof(struct air),1,fp);fclose(fp);} void add()//定義增加航班信息函數(shù) { do{ printf(“請依次輸入您要增加的航班信息(以回車鍵結(jié)束): n”); //打印提示信息 printf(“------------n”); printf(“請輸入航班號: ”); scanf(“%d”,&s[m].num);//讀取航班號 printf(“請輸入起始站: ”); scanf(“%s”,s[m].start);//讀取起始站 printf(“請輸入終點站: ”); scanf(“%s”,s[m].over);//讀取終點站 printf(“請輸入時間: ”); scanf(“%s”,s[m].time);//讀取時間 printf(“請輸入機票數(shù): ”); scanf(“%d”,&s[m].count);//讀取機票數(shù) m++; printf(“添加完畢,是否繼續(xù)添加?請鍵入y或n以回車鍵結(jié)束:”); scanf(“%s”,ii);}while(!strcmp(ii,“y”));//判斷是否繼續(xù)添加 } void output()//定義輸出格式函數(shù) { printf(“航班號tt起始站tt終點站tt時間tt機票數(shù)n”);//信息標題 for(i=0;i printf(PRINT);//打印出信息 } void print()//定義顯示航班信息函數(shù) { printf(“n目前我們有如下航班:n”);output(); //調(diào)用輸出格式函數(shù) printf(“n請按回車鍵返回上層菜單 ”);getchar();getchar();} void search()//定義查詢函數(shù) { int n; do { printf(“n請輸入航班號: ”); scanf(“%d”,&n);//輸入查詢的航班號 for(i=0;i { if(s[i].num==n)//按航班號判定輸出條件 { printf(“n您所查找的航班信息為:n ”); printf(“航班號tt起始站tt終點站tt時間tt機票數(shù) nn”); printf(PRINT);//顯示信息 printf(“n查詢完畢,按回車鍵繼續(xù)”); getchar(); getchar(); return; } } printf(“n對不起,沒有您需要的信息!n ”);printf(“是否重新查找?請鍵入y或n以回車鍵結(jié)束 ”);scanf(“%s”,ii);}while(!strcmp(ii,“y”));//判定是否重新查找 } void dingpiao()//定義訂票業(yè)務(wù)函數(shù) { int n;char a[10]=“y”;do { search();//調(diào)用查詢模塊 if(!strcmp(ii,“n”)) { printf(“對不起!沒有找到您所需要的航班,所以不能訂票。n”);//未查找到所需航班 printf(“n請按回車鍵返回上層菜單 ”); getchar(); getchar(); strcpy(ii,“n”); break; } do { printf(“請輸入您要訂的機票數(shù)(以回車鍵結(jié)束): ”); scanf(“%d”,&n);//輸入所訂機票數(shù) if(n<=0) //判定機票數(shù)是否出錯 { printf(“輸入錯誤!至少需訂1張機票。n”); } else if(s[i].count==0)//判定機票是否售完 { printf(“對不起,你所選擇的航班的機票已售完!n”); break; } else if(s[i].count!=0&&s[i].count>=n)//判定機票數(shù)是否大于等于訂票數(shù) { s[i].count=s[i].count-n; printf(“訂票成功!”); break; } else if(s[i].count { printf(“對不起,你所選擇的航班只剩 %d張機票n”, s[i].count); printf(“是否需要重新輸入機票數(shù)?請輸入y或n以回車鍵結(jié)束: ”);//判定是否重新輸入訂票數(shù) scanf(“%s”,a); } }while(!strcmp(a,“y”)); printf(“是否需要訂其他航班的機票?請輸入y或n以回車鍵結(jié)束: ”); scanf(“%s”,a);}while(!strcmp(a,“y”));//判定是否繼續(xù)訂票 } void tuipiao()//定義退票函數(shù) { int n;char a[10];do { search();//調(diào)用查詢函數(shù) if(!strcmp(ii,“n”)) { printf(“對不起!沒有找到您所需要的航班,所以不能退票。n”); printf(“n請按回車鍵返回上層菜單 ”); getchar(); getchar(); strcpy(ii,“n”); break; } printf(“請輸入您要退的機票數(shù)目: ”); scanf(“%d”,&n);//輸入所退票數(shù) if(n<=0) //判定票數(shù)是否有效 printf(“輸入錯誤!至少需退1張機票?!?; else { s[i].count=s[i].count+n; printf(“退票成功!”); } printf(“是否繼續(xù)? 請鍵入y或n以回車鍵結(jié)束: ”);//判定是否繼續(xù)退票 scanf(“%s”,a);}while(!strcmp(a,“y”));//判定并跳出循環(huán) } void paixu()//定義排序函數(shù) { int n; printf(“n******************************************************************************** ”); printf(“1.按航班號從小到大排序n” “t2.按航班號從大到小排序n”);printf(“******************************************************************************** ”); printf(“請在1-2中選擇以回車鍵結(jié)束: ”);scanf(“%d”,&n);//輸入排序方式 switch(n){ case 1:paixu1();//調(diào)用從小到大排序函數(shù) break; case 2:paixu2();//調(diào)用從大到小排序函數(shù) break;} printf(“排序后的航班信息為:n”);output(); //顯示排序后航班信息 printf(“n請按回車鍵返回上層菜單 ”); getchar(); getchar();} void paixu1()//定義從小到大排序函數(shù) { int k,j;struct air t;for(i=0;i { k=i; for(j=i+1;j if(s[k].num>s[j].num) k=j; if(i!=k) { t=s[k]; s[k]=s[i]; s[i]=t; } } } void paixu2()//定義從大到小排序函數(shù) { } int k,j;struct air t;for(i=0;i if(s[k].num k=j;if(i!=k){ t=s[k]; s[k]=s[i]; s[i]=t;} }第二篇:RSA加密解密算法C語言代碼
第三篇:C語言課程設(shè)計火車票系統(tǒng)源代碼
第四篇:RSA加密解密算法c語言程序
第五篇:C語言課程設(shè)計——飛機訂票系統(tǒng)源代碼