信息安全學(xué)實驗一_第1頁
信息安全學(xué)實驗一_第2頁
信息安全學(xué)實驗一_第3頁
信息安全學(xué)實驗一_第4頁
信息安全學(xué)實驗一_第5頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、信息安全學(xué)實驗云南大學(xué)軟件學(xué)院實 驗 報 告課程: 信息安全學(xué)實驗 任課教師: 專業(yè): 學(xué)號: 姓名: 成績: 實驗1 古典密碼算法一、 實驗?zāi)康耐ㄟ^編程實現(xiàn)替代密碼算法和置換密碼算法,加深對古典密碼體系的了解,為以后深入學(xué)習(xí)密碼學(xué)奠定基礎(chǔ)。二、 實驗原理古典密碼算法曾被廣泛應(yīng)用,大都比較簡單。它的主要應(yīng)用對象是文字信息,利用密碼算法實現(xiàn)文字信息的加密和解密。其中替代密碼和置換密碼是具有代表性的兩種古典密碼算法。三、 實驗環(huán)境PC機,C或C+編譯環(huán)境四、 實驗內(nèi)容和步驟1、 根據(jù)實驗原理部分對替代密碼算法的介紹,同時查找和學(xué)習(xí)相關(guān)知識,自己創(chuàng)建明文信息,并選擇一個密鑰,編寫替代密碼算法的實現(xiàn)程

2、序,實現(xiàn)加密和解密操作。對于替換密碼,在本實驗分別實現(xiàn)了移位密碼和維吉尼亞密碼,其中維吉尼亞算法中能夠?qū)ξ谋疚募M行加密和解密操作。移位密碼程序流程圖:開始2輸入密文c輸入1或21輸入密鑰k輸入明文m輸入密鑰kj=0cj=(cj-k-a+26)%26+aj+i=0mi=(mi+k-a)%26+ai+cj=0否否mi=00是是輸出明文m輸出密文c結(jié)束算法設(shè)計:實現(xiàn)加密的過程中,先輸入明文m,明文以字符串的形式儲存在數(shù)組中,再輸入密鑰k,密鑰范圍為1-25,加密函數(shù)encrypt對明文m中的字符進行逐個加密E(m)=(m+k)mod26。解密時先輸入需要解密的密文c,密文以字符串的形式儲存在數(shù)組中

3、,再輸入相對應(yīng)的密鑰k,解密函數(shù)decrypt對密文c中的字符進行逐個解密D(c)=(c-k)mod26。該算法中時間復(fù)雜度為O(n)。測試結(jié)果截圖: 維吉尼亞密碼 程序流程圖:開始2輸入密文c輸入1或21輸入密鑰k輸入明文m輸入密鑰ki=0l=k的長度i=0j=0l=k的長度ci=(ci-kj-a+26)%26+a,i+,j+j=0mi=(mi+kj-a)%26+a,i+,j+否j<0是否j<l否ci=0是否mi=00是是輸出明文m輸出密文c結(jié)束算法設(shè)計:加密過程中,先輸入明文m,明文以字符串的形式儲存在數(shù)組中,再輸入密鑰k,維吉尼亞密碼在移位密碼的基礎(chǔ)上增大了密鑰空間,密鑰以字

4、符串的形式儲存于數(shù)組中,加密函數(shù)encrypt按照密鑰的字符循環(huán)對明文m中的字符進行逐個加密。解密時先輸入需要解密的密文c,密文以字符串的形式儲存在數(shù)組中,再輸入相對應(yīng)的密鑰k并儲存在數(shù)組中,解密函數(shù)decrypt對密文c中的字符進行逐個解密。該算法中時間復(fù)雜度為O(n)。測試結(jié)果截圖:對字符串加密:對文本文件加密: 2、 根據(jù)實驗原理部分對置換密碼算法的介紹,同時查找和學(xué)習(xí)相關(guān)知識,自己創(chuàng)建明文信息,并選擇一個密鑰,編寫置換密碼算法的實現(xiàn)程序,實現(xiàn)加密和解密操作。 該置換算法可以對文本文件進行加密解密操作。程序流程圖:開始2輸入密文c輸入1或21輸入密鑰k輸入明文m算出明文的行數(shù)列數(shù)并存入二

5、維數(shù)組輸入密鑰k算出明文的行數(shù)列數(shù)并存入二維數(shù)組算出反向置換函數(shù)對二維數(shù)組中的密文進行兩次列置換算出置換函數(shù)將二維數(shù)組中的字符串放入一維數(shù)組對二維數(shù)組中的明文進行兩次列置換將二維數(shù)組中的字符串放入一維數(shù)組輸出明文m輸出密文c結(jié)束算法設(shè)計:實現(xiàn)置換密碼加密過程,先輸入明文m,明文以字符串的形式儲存在數(shù)組中,再輸入密鑰k,密鑰以字符串的形式儲存于數(shù)組中,若明文不能被密鑰整除,則在明文后隨機添加字母使其能夠被密鑰整除。對密鑰字符串進行冒泡排序,根據(jù)密鑰中字母得出置換函數(shù)f,將明文m存入二維數(shù)組,可以選擇需要進行置換的次數(shù),進行置換得到密文。解密時先輸入需要解密的密文c,密文以字符串的形式儲存在數(shù)組中

6、,再輸入相對應(yīng)的密鑰k并儲存在數(shù)組中,以加密相同的方式進行逆操作得到明文。測試結(jié)果截圖:對字符串加密:對文本文件加密:明文末尾為加密時隨機添加的字母。五、 問題及總結(jié)1、 回答問題:試分析兩種密碼算法的安全性,以及如何改進。答:實驗指導(dǎo)書中的替換密碼為移位密碼,移位密碼的安全性較低,產(chǎn)生移位的可能性只有25種,可以通過窮舉的方式破譯密碼。在移位密碼的基礎(chǔ)上改進的替換密碼,將26個字母都由其他的字母代替,一一對應(yīng)產(chǎn)生密碼表,這種替換密碼有26!種可能的替換方式,用窮舉法無法破譯,但是根據(jù)語言學(xué)各個英文字母出現(xiàn)的頻率可以破譯。在該實驗中實現(xiàn)的第二種替換密碼是維吉尼亞密碼,安全性有較大的提升,其密鑰

7、空間增大,每個字母移位的長度不同,也使字母出現(xiàn)頻率趨于平衡,密鑰長度越長則密碼越安全,破解該密碼的要點是知道密鑰的長度。破譯該實驗中置換密碼的切入點是明文的長度,可以根據(jù)明文的長度猜測密鑰長度,密鑰長度應(yīng)為明文長度的約數(shù),再對序列采用窮舉即可破譯,加強置換密碼安全性可對明文多次置換,或想替換加密后置換加密。2、 本次實驗的體會總結(jié)。1、 通過本次實驗,對古典密碼的兩種基本加密方式有了比較深入的了解。2、 對加密和密碼分析有了一定的概念。3、 了解了密碼學(xué)在信息安全中的重要性。六、 附件移位密碼:#include <stdio.h>#include <string.h>v

8、oid enc(char a, int key)/*加密*/int i;for (i = 0; ai != '0' i+)ai = (ai - 'a' + key) % 26 + 'a'void encrypt()char plain100;int enkey;printf("* encrypt *nn");printf("Please input the plaintext: ");getchar();gets_s(plain, 100);printf("Please input the key

9、 use to encrypt the plaintext: ");scanf_s("%d", &enkey);enc(plain, enkey);printf("The ciphertext is : %sn", plain);printf("n*n");void dec(char a, int key)/*解密*/int i;for (i = 0; ai != '0' i+)ai = (ai - 'a' - key + 26) % 26 + 'a'void decr

10、ypt()char cipher100;int dekey;printf("* decrypt *nn");printf("Please input the ciphertext: ");getchar();gets_s(cipher, 100);printf("Please input the key use to decrypt the ciphertext: ");scanf_s("%d", &dekey);dec(cipher, dekey);printf("The plaintext i

11、s : %sn", cipher);printf("n*n");void main()int num;printf("* substitution *nn");printf("*n");while (1)printf("Press 1 or 2 to choose encrypt or decrypt:n1.encryptn2.decryptn");scanf_s("%d", &num);switch (num)case 1:encrypt();break;case 2:dec

12、rypt();break;default:break;維吉尼亞密碼:#include <stdio.h>#include <string.h>#include <stdlib.h>void enc(char arr, char k)/*加密*/int len = strlen(k);int key, i, j = 0;for (i = 0; arri != '0' i+)if (arri != ' ')if (j > len - 1)j = 0;key = kj - 'a'arri = (arri + k

13、ey - 'a') % 26 + 'a'j+;void encrypt()char plain100, enkey20, ch;printf("* encrypt *nn");loop:printf("Choose f to encrypt a file or s to encrypt a string:");getchar();ch = getchar();if (ch = 'f')/*文件加密*/FILE *fp;char filename20;input:printf("Please inp

14、ut the file u wannna encrypt: ");getchar();gets_s(filename, 20);errno_t err;if (err = fopen_s(&fp, filename, "r+") != 0)printf("Cannot open the file!n");goto input;fgets(plain, 100, fp);printf("Please input the key use to encrypt the file: ");gets_s(enkey, 20);

15、enc(plain, enkey);rewind(fp);fputs(plain, fp);fclose(fp);printf("nThe encryption has done,please check the file!n");else if (ch = 's')/*字符串加密*/printf("Please input the plaintext: ");getchar();gets_s(plain, 100);printf("Please input the key use to encrypt the plaintex

16、t: ");gets_s(enkey, 20);enc(plain, enkey);printf("nThe ciphertext is : %sn", plain);elsegoto loop;printf("n*n");void dec(char arr, char k)/*解密*/int len = strlen(k);int key, i, j = 0;for (i = 0; arri != '0' i+)if (arri != ' ')if (j > len - 1)j = 0;key = kj

17、- 'a'arri = (arri - key - 'a' + 26) % 26 + 'a'j+;void decrypt()char cipher100, dekey20, ch;printf("* decrypt *nn");loop:printf("Choose f to decrypt a file or s to decrypt a string:");getchar();ch = getchar();if (ch = 'f')/*文件解密*/FILE *fp;char filen

18、ame20;input:printf("Please input the file u wannna decrypt: ");getchar();gets_s(filename, 20);errno_t err;if (err = fopen_s(&fp, filename, "r+") != 0)printf("Cannot open the file!n");goto input;fgets(cipher, 100, fp);printf("Please input the key use to encrypt

19、the file: ");gets_s(dekey, 20);dec(cipher, dekey);rewind(fp);fputs(cipher, fp);fclose(fp);printf("nThe decryption has done,please check the file!n");else if (ch = 's')/*字符串解密*/printf("Please input the ciphertext: ");getchar();gets_s(cipher, 100);printf("Please i

20、nput the key use to decrypt the ciphertext: ");gets_s(dekey, 20);dec(cipher, dekey);printf("nThe plaintext is : %sn", cipher);elsegoto loop;printf("n*n");void main()int num;printf("* vigenere *nn");printf("*n");while (1)printf("Press 1 or 2 to choose

21、 encrypt or decrypt:n1.encryptn2.decryptn");scanf_s("%d", &num);switch(num)case 1:encrypt();break;case 2:decrypt();break;default:exit(0);break;置換密碼:#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>void swap(char key, int i, int j)int

22、 t = keyi;keyi = keyj;keyj = t;void bubble(char key, int col)/*冒泡排序*/int i, j;for (i = 0; i < col; i+)for (j = col - 1; j > i; j-)if (keyi > keyj)swap(key, i, j);void match(char key, char k, int f, int col)/*計算置換函數(shù)*/int i, j;for (i = 0; i < col; i+)for (j = 0; j < col; j+)if (keyi = k

23、j)fi = j;void trans(char mid20, char a20, int f, int row, int col)/*列置換*/int i, j;for (i = 0; i < row; i+)for (j = 0; j < col; j+)midij = aifj;a = mid;void enc(char arr, char key)/*加密*/char a2020, mid2020, enk20;int i, j, cnt, m = 0, n = 0, k = 0, f20;int col = strlen(key);int len = strlen(arr

24、);if (len % col != 0)k = col - (len % col);srand(unsigned)time(0);j = len;for (i = 0; i < k; i+)arrj = (rand() % 26) + 'a'j+;arrj = '0'int row = (len + k) / col;for (i = 0; i < col; i+)enki = keyi;for (i = 0; i < row; i+)for (j = 0; j < col; j+)aij = arrm;m+;bubble(key, c

25、ol);match(key, enk, f, col);printf("Please input the count of exchange u wanna make: ");scanf_s("%d", &cnt, 1);for (i = 0; i < cnt; i+)trans(mid, a, f, row, col);for (j = 0; j < col; j+)for (i = 0; i < row; i+)arrn = aij;n+;void encrypt()char plain100, enkey20, ch;pr

26、intf("* encrypt *nn");loop:printf("Choose f to decrypt a file or s to decrypt a string:");getchar();ch = getchar();if (ch = 'f')/*文件加密*/FILE *fp;char filename20;input:printf("Please input the file u wanna decrypt: ");getchar();gets_s(filename, 20);errno_t err;if

27、 (err = fopen_s(&fp, filename, "r+") != 0)printf("Cannot open the file!");goto input;fgets(plain, 100, fp);printf("Please input the key use to encrypt the file: ");gets_s(enkey, 20);enc(plain, enkey);rewind(fp);fputs(plain, fp);fclose(fp);printf("nThe decryptio

28、n has done,please check the file!n");else if (ch = 's')/*字符串加密*/printf("Please input the plaintext: ");getchar();gets_s(plain, 100);printf("Please input the key use to decrypt the ciphertext: ");gets_s(enkey, 20);enc(plain, enkey);printf("nThe plaintext is : %sn

29、", plain);elsegoto loop;printf("n*n");void dec(char arr, char key)/*解密*/char a2020, mid2020, dek20;int i, j, cnt, m = 0, n = 0, f20;int col = strlen(key);int row = strlen(arr) / col;for (i = 0; i < col; i+)deki = keyi;for (j = 0; j < col; j+)for ( i = 0; i < row; i+)aij = arr

30、m;m+;bubble(key, col);match(dek, key, f, col);printf("Please input the count of exchange u wanna make: ");scanf_s("%d", &cnt, 1);for (i = 0; i < cnt; i+)trans(mid, a, f, row, col);for (i = 0; i < row; i+)for (j = 0; j < col; j+)arrn = aij;n+;void decrypt()char cipher100, dekey20, ch;printf("* decrypt *nn");loop:printf("Choose f to decrypt a file or s to decrypt a string:");getchar();ch = getchar();if (ch = 'f')/*文件解密*/FILE *fp;char filename20;input:printf("Please input the

溫馨提示

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

評論

0/150

提交評論