東北大學(xué)信息安全作業(yè)_第1頁
東北大學(xué)信息安全作業(yè)_第2頁
東北大學(xué)信息安全作業(yè)_第3頁
東北大學(xué)信息安全作業(yè)_第4頁
東北大學(xué)信息安全作業(yè)_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上作業(yè)(一)(1).編程實(shí)現(xiàn)雙軌加密#include<iostream>#include<string>開始using namespace std;char ch130;char ch230;從鍵盤讀入明文ch1int i;string shuanggui(char *ch) 去除空格ch2int j=0,k=0;i=0;string str1=""偶數(shù)位字符string str2=""while(chj!='0')否是if(chj=' ');加到str1中加到str2中el

2、se ch2i=chj;i+;j+;while(k<i)輸出str1+str2 str1+=ch2k; k+;結(jié)束 str2+=ch2k; k+;return str1+str2;int main()cout<<"請輸入明文:" gets(ch1); cout<<"雙軌加密后:"<<shuanggui(ch1)<<endl;return 0;(2).編程實(shí)現(xiàn)鑰控算法開始#include<iostream>#include<string>using namespace std;讀

3、入明文c1,密鑰c2 int N;int M;int len(char *ch)計(jì)算明文、密鑰的實(shí)際長度M、N /計(jì)算密鑰長度、明文長度 int i=0;while(chi!='0')i+;生成二維字符數(shù)組return i;/快速排序?qū)γ荑€數(shù)組進(jìn)行一次快速排序ch2void Qsort(char *ch, int low, int high) if(low >= high) c2n=ch2m return; int first = low; int last = high;n=n+1 char key = chfirst; /用字表的第一個(gè)記錄作為樞軸 while(fir

4、st < last)n<N while(first < last && chlast >= key) -last; m=m+1 chfirst = chlast; /將比第一個(gè)小的移到低端n<N while(first < last && chfirst <= key) +first; 輸出密文矩陣 chlast = chfirst; /將比第一個(gè)大的移到高端 結(jié)束 chfirst = key; /樞軸記錄到位 Qsort(ch, low, first-1); Qsort(ch, first+1, high);void

5、yuekong(char *ch0,char *ch2)int i=0,k=0,j=0;char ch31010;char ch410,ch1100;while(ch0j!='0') /除去空格if(ch0j=' ');else ch1i=ch0j;i+;j+;ch1i+1='0'i=0;j=0;while(ch1i!='0') /生成二維字符數(shù)組 ch3i/Ni%N=ch1i; i+; for(int l=0;l<=N;l+)ch4l=ch2l;Qsort(ch2,0,N-1); cout<<"鑰控加

6、密后:"<<endl;for(int m=0;m<=N;m+) for(int n=0;n<N;n+) if(ch4n=ch2m) if(i%N=0) for(int j=0;j<i/N;j+) cout<<ch3jn; else for(int j=0;j<(i/N+1);j+) if(ch3jn>='a'&&ch3jn<='z')|(ch3jn>='A'&&ch3jn<='Z') cout<<ch3jn

7、; else ; cout<<endl; else ; void main()char c1100; /明文 char c220; /密鑰cout<<"請輸入明文:"gets(c1);cout<<"請輸入密鑰:" gets(c2); N=len(c2); M=len(c1);yuekong(c1,c2);作業(yè)(二)(1)編程實(shí)現(xiàn)求乘法逆元;#include <stdio.h> int ExtendedEuclid( int f,int d ,int *result) int x1,x2,x3,y1,y2,y

8、3,t1,t2,t3,q; x1 = y2 = 1; x2 = y1 = 0; x3 = ( f>=d )?f:d;y3 = ( f>=d )?d:f; while( 1 ) if ( y3 = 0 ) *result = x3; / 兩個(gè)數(shù)不互素則result為兩個(gè)數(shù)的最大公約數(shù),此時(shí)返回值為零 return 0; if ( y3 = 1 ) *result = y2; / 兩個(gè)數(shù)互素則resutl為其乘法逆元,此時(shí)返回值為1 return 1; q = x3/y3; t1 = x1 - q*y1; t2 = x2 - q*y2; t3 = x3 - q*y3; x1 = y1;

9、 x2 = y2; x3 = y3; y1 = t1; y2 = t2; y3 = t3; int main() int x,y,z,max,min; z = 0; printf("請輸入兩個(gè)數(shù):n"); scanf("%d%d",&x,&y); max=x>y?x:y;min=x<y?x:y;if(ExtendedEuclid(min,max,&z) if(z>0) printf("%d和%d互素,乘法的逆元是:%dn",x,y,z);else printf("%d和%d互素,乘法

10、逆元是:%dn",x,y,z+max); else printf("%d和%d不互素,最大公約數(shù)為:%dn",x,y,z); return 0; (2)編程實(shí)現(xiàn)換位密碼;#include<iostream>#include<string>using namespace std;char ch30;char ch130;int x;int len(char *ch)int i=0,j=0;char c120;while(chi!='0')if(chi!=' ') c1j=chi;j+;else ;i+;retu

11、rn j;void Lieout(char ch4) /列換位輸出cout<<"列換位加密后:" for(int i1=0;i1<5;i1+) if(chi11>='a'&&chi11<='z')|(chi11>='A'&&chi11<='Z') cout<<chi11;else ; for(int i2=0;i2<5;i2+) if(chi23>='a'&&chi23<=&#

12、39;z')|(chi23>='A'&&chi23<='Z') cout<<chi23;else ; for(int i3=0;i3<5;i3+) if(chi30>='a'&&chi30<='z')|(chi30>='A'&&chi30<='Z') cout<<chi30;else ; for(int i4=0;i4<5;i4+) if(chi42>='a&

13、#39;&&chi42<='z')|(chi42>='A'&&chi42<='Z') cout<<chi42;else ; cout<<endl;void Hangout(char ch4) /行換位輸出cout<<"行換位加密后:" for(int i0=0;i0<4;i0+) if(ch3i0>='a'&&ch3i0<='z')|(ch3i0>='A'

14、&&ch3i0<='Z') cout<<ch3i0; else ;for(int i1=0;i1<4;i1+) if(ch1i1>='a'&&ch1i1<='z')|(ch1i1>='A'&&ch1i1<='Z') cout<<ch1i1; else ;for(int i2=0;i2<4;i2+) if(ch4i2>='a'&&ch4i2<='z

15、9;)|(ch4i2>='A'&&ch4i2<='Z') cout<<ch4i2; else ; for(int i3=0;i3<4;i3+) if(ch0i3>='a'&&ch0i3<='z')|(ch0i3>='A'&&ch0i3<='Z') cout<<ch0i3;else ; for(int i4=0;i4<4;i4+) if(ch2i4>='a'&a

16、mp;&ch2i4<='z')|(ch2i4>='A'&&ch2i4<='Z') cout<<ch2i4;else ; cout<<endl;void Zhouqiout(char *ch) /周期換位輸出char ch05,ch15,ch25,ch35;int i=0,j0=0,j1=0,j2=0,j3=0;while(chi!='0')if(i%4=1)ch0j0=chi;j0+;else if(i%4=3)ch1j1=chi;j1+;else if(i%4=0

17、)ch2j2=chi;j2+;else if(i%4=2)ch3j3=chi;j3+;else ;i+;cout<<"周期換位加密后:" for(int k=0;k<5;k+)if(ch0k>='a'&&ch0k<='z'|ch0k>='A'&&ch0k<='Z') cout<<ch0k; if(ch1k>='a'&&ch1k<='z'|ch1k>='A

18、'&&ch1k<='Z')cout<<ch1k;if(ch2k>='a'&&ch2k<='z'|ch2k>='A'&&ch2k<='Z')cout<<ch2k;if(ch3k>='a'&&ch3k<='z'|ch3k>='A'&&ch3k<='Z')cout<<ch3k;cou

19、t<<endl;void huanwei(char *ch,int x) /換位加密char ch254;int i=0,j=0;while(chi!='0')if(chi!=' ') ch1j=chi;j+;else ;i+;ch1j='0'j=0; while(ch1j!='0')ch2j/4j%4=ch1j;j+;if(x=1|x=2) cout<<"明文矩陣:"<<endl; for(int a=0;a<5;a+) cout<<"t&quo

20、t;for(int b=0;b<4;b+)if(ch2ab>='a'&&ch2ab<='z')|(ch2ab>='A'&&ch2ab<='Z') cout<<ch2ab<<" "else ;cout<<endl; else ;if(x=1) Lieout(ch2);else if(x=2)Hangout(ch2);else if(x=3)Zhouqiout(ch1);else ;int main()cout<<"請輸入明文(最多包含20個(gè)字母,空格不限):"<<endl;gets(ch);while(len(ch)>20)cout<<"明文字母過長,請重新輸入

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論