編程實(shí)現(xiàn)AES算法的加密解密過程_第1頁
編程實(shí)現(xiàn)AES算法的加密解密過程_第2頁
編程實(shí)現(xiàn)AES算法的加密解密過程_第3頁
編程實(shí)現(xiàn)AES算法的加密解密過程_第4頁
編程實(shí)現(xiàn)AES算法的加密解密過程_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、信息安全工程課程實(shí)驗(yàn)報(bào)告編程實(shí)現(xiàn)aes算法的加密解密過程課程名稱:信息安全工程學(xué)生姓名:學(xué)生學(xué)號(hào):專業(yè)班級(jí):任課教師:蔡忠閩 目錄1 背景與意義32 實(shí)驗(yàn)要求33.系統(tǒng)設(shè)計(jì)43.1系統(tǒng)主要目標(biāo)43.2主要軟件需求(運(yùn)行環(huán)境)44 aes算法加密和解密原理分析44.1加密過程基本變換64.2解密過程基本變換64.3密鑰擴(kuò)展算法75.系統(tǒng)功能程序設(shè)計(jì)76. 測(cè)試報(bào)告176.1 加密176.2 解密197 實(shí)驗(yàn)總結(jié)211 背景與意義科技的發(fā)展使計(jì)算機(jī)深入到了我們生活的的方方面面,計(jì)算機(jī)在帶來方便和提高了工作效率的同時(shí)卻也帶來了各種各樣的新問題,其中信息安全問題最為突出,隨著計(jì)算機(jī)信息安全要求的不斷提

2、高, 計(jì)算機(jī)保密系統(tǒng)已變得越來越重要,所以我們需要一種較為可靠的加密算法來保護(hù)我們信息的安全。由于des已經(jīng)無法滿足高保密性的要求,美國(guó)于1997年1月開始征集新一代數(shù)據(jù)加密標(biāo)準(zhǔn)(即高級(jí)數(shù)據(jù)加密標(biāo)準(zhǔn),advanced encryption standard,aes)。2000年10月2日,正式宣布選擇比利時(shí)密碼學(xué)家所開發(fā)的rijndael算法成為aes的最終算法。aes(the advanced encryption standard)是美國(guó)國(guó)家標(biāo)準(zhǔn)與技術(shù)研究所用于加密電子數(shù)據(jù)的規(guī)范。它被預(yù)期能成為人們公認(rèn)的加密包括金融、電信和政府?dāng)?shù)字信息的方法。aes 是一個(gè)新的可以用于保護(hù)電子數(shù)據(jù)的加密算

3、法。明確地說,aes 是一個(gè)迭代的、對(duì)稱密鑰分組的密碼,它可以使用128、192 和 256 位密鑰,并且用 128 位(16字節(jié))分組加密和解密數(shù)據(jù)。與公共密鑰密碼使用密鑰對(duì)不同,對(duì)稱密鑰密碼使用相同的密鑰加密和解密數(shù)據(jù)。通過分組密碼返回的加密數(shù)據(jù)的位數(shù)與輸入 數(shù)據(jù)相同。迭代加密使用一個(gè)循環(huán)結(jié)構(gòu),在該循環(huán)中重復(fù)置換和替換輸入數(shù)據(jù)。該算法為比利時(shí)密碼學(xué)家joan daemen和vincent rijmen所設(shè)計(jì),結(jié)合兩位作者的名字,以rijndael之命名之,投稿高級(jí)加密標(biāo)準(zhǔn)的甄選流程。(rijdael的發(fā)音近于 rhine doll。)aes在軟體及硬件上都能快速地加解密,相對(duì)來說較易于實(shí)作

4、,且只需要很少的記憶體。作為一個(gè)新的加密標(biāo)準(zhǔn),目前正被部署應(yīng)用到更廣大的范圍.2 實(shí)驗(yàn)要求(1)一人一組 (2)aes算法加密和解密原理分析 (3)提交完整的源程序(使用網(wǎng)絡(luò)上的現(xiàn)成程序一定要注明出處) (4)提交程序的設(shè)計(jì),說明文檔 (5)用ecb和分組鏈模式cbc,對(duì)給定內(nèi)容進(jìn)行加密和解密,提交解密結(jié)果,并提交實(shí)驗(yàn)心得,著重闡述對(duì)實(shí)驗(yàn)的理解(6)程序調(diào)試成功后,處理以下內(nèi)容:a、aes 加密明文:各自的姓名+郵箱秘鑰:各自的手機(jī)號(hào)b、aes解密密文:郵箱附件給出秘鑰:(給出秘鑰為ascii碼形式,請(qǐng)將ascii碼轉(zhuǎn)為二進(jìn)制,不滿128位在其后補(bǔ)零):130729810633.系統(tǒng)設(shè)計(jì)3.1

5、系統(tǒng)主要目標(biāo)1在深入理解aes加密/解密算法理論的基礎(chǔ)上,設(shè)計(jì)一個(gè)aes加密/解密軟件系統(tǒng);2完成一個(gè)明文分組的加解密,明文和密鑰是十六進(jìn)制,輸入明文和密鑰,輸出密文,進(jìn)行加密后,能夠進(jìn)行正確的解密;3. 按要求提供所設(shè)計(jì)系統(tǒng)的報(bào)告及完整的軟件。3.2主要軟件需求(運(yùn)行環(huán)境)本軟件適用vc語言編寫,編譯成功后的exe文件可以在裝有windows系統(tǒng)的任何計(jì)算機(jī)上使用。測(cè)試平臺(tái):windows 8使用軟件:visual studio 2012/c+4 aes算法加密和解密原理分析aes是一個(gè)迭代的、對(duì)稱密鑰分組的密碼,它可以使用128、192和256位密鑰長(zhǎng)度,并且用128位分組長(zhǎng)度加密和解密數(shù)

6、據(jù)2。該算法輸入分組、輸出分組、狀態(tài)長(zhǎng)度均為128比特。對(duì)于aes算法的運(yùn)算是在一個(gè)稱為狀態(tài)的二維字節(jié)數(shù)組上進(jìn)行。一個(gè)狀態(tài)由四行組成,每一行包括nb個(gè)字節(jié),nb等于分組長(zhǎng)度除以32,aes分組長(zhǎng)度為128位,因此,nb=4,該值反應(yīng)了狀態(tài)中32-bit字的個(gè)數(shù)(列數(shù));密鑰長(zhǎng)度128、192和256位可分別表示為nk=4、6或8,反應(yīng)了密鑰中32-bit字的個(gè)數(shù)(列數(shù))。而aes算法的輪數(shù)nr僅依賴于密鑰長(zhǎng)度nk,輪數(shù)和密鑰長(zhǎng)度的關(guān)系可以表示為:nr=6+nk。密鑰長(zhǎng)度分組長(zhǎng)度輪數(shù)的關(guān)系如表1所示。表1 key-block-round關(guān)系密鑰長(zhǎng)度(nk words)分組長(zhǎng)度(nb words)

7、輪數(shù)(nr)441064128414對(duì)于加密和解密變換,aes算法使用的輪函數(shù)由4個(gè)不同的以字節(jié)為基本單位的變換復(fù)合而成,該過程由四個(gè)不同的階段組成3:(1)s盒變換,用一個(gè)s盒完成分組中的按字節(jié)代替;(2)行移位變換,一個(gè)簡(jiǎn)單的置換;(3)列混淆變換,一個(gè)利用在域gf(28) 上的算術(shù)性的代替;(4)輪密鑰加變換,一個(gè)利用當(dāng)前分組和擴(kuò)展密鑰的一個(gè)部分進(jìn)行按位異或。aes對(duì)數(shù)據(jù)的加密過程是通過把輸入的明文和密鑰由輪函數(shù)經(jīng)nr輪迭代來實(shí)現(xiàn)的,結(jié)尾輪與前nr-1輪不同。前nr-1輪依次進(jìn)行s盒變換、行移位變換、列混淆變換和輪密鑰加變換;結(jié)尾輪與前nr-1輪相比去掉了列混淆變換。而解密過程與加密過程

8、相反,通過把輸入的密文和密鑰由輪函數(shù)經(jīng)nr輪迭代來實(shí)現(xiàn)的,結(jié)尾輪與前nr-1輪不同。前nr-1輪依次進(jìn)行逆行移位變換、逆s盒變換、輪密鑰加變換和逆列混淆變換;結(jié)尾輪與前nr-1輪相比去掉了逆列混淆變換。aes算法的加密解密過程如圖1所示:圖1 aes加密解密流程4.1加密過程基本變換(1) s盒變換s盒變換是一個(gè)基于s盒的非線性置換,它用于輸入或中間態(tài)的每一個(gè)字節(jié)通過一個(gè)簡(jiǎn)單的查表操作,將其映射為另一個(gè)字節(jié)。映射方法是:把輸入字節(jié)的高4位作為s盒的行值,低4位作為列值,然后取出s盒中對(duì)應(yīng)行和列的元素作為輸出。例如,輸入為“89”(十六進(jìn)制)的值所對(duì)應(yīng)的s盒的行值為“8”,列值為“9”,s盒中相

9、應(yīng)位置的值為“a7”,就說明“89”被映射為“87”。 (2) 列混淆變換列混淆變換實(shí)現(xiàn)逐列混淆,其方法是: 其中,內(nèi)的數(shù)表示是字節(jié)。用矩陣表示為(3) 行移位變換行移位變換完成基于行的循環(huán)移位操作,即行移位變換的作用在中間態(tài)的行上,第0行不動(dòng),第1行循環(huán)左移1個(gè)字節(jié),第2行循環(huán)左移2個(gè)字節(jié),第3行循環(huán)左移3個(gè)字節(jié)。(4) 輪密鑰加變換輪密鑰加變換用于將輸入或中間態(tài)s的每一列與一個(gè)密鑰字wi進(jìn)行按位異或,其中, 由原始密鑰通過密鑰擴(kuò)展算法產(chǎn)生。4.2解密過程基本變換解密過程是加密的逆過程,s盒變換、行移位變換、列混淆變換都要進(jìn)行求逆變換,即逆s盒變換、逆行移位變換、逆列混淆變換。而輪密鑰加變換

10、與加密過程相同。(1) 逆s盒變換與s盒變換類似,逆s盒變換是基于逆s盒實(shí)現(xiàn)的。(2) 逆行移位變換與行移位變換相反,逆行移位變換將態(tài)state的后三行按相反的方向進(jìn)行移位操作,即第0行保持不變,第1行向右移1個(gè)字節(jié),第2行向右移2個(gè)字節(jié),第3行向右移3個(gè)字節(jié)。(3) 逆列混淆變換逆列混淆變換的處理方法與列混淆變換類似,每一列都通過與一個(gè)固定的多項(xiàng)相乘進(jìn)行變換。寫為矩陣乘法的形式,逆列混淆變換按以下方式對(duì)列進(jìn)行變換:4.3密鑰擴(kuò)展算法aes加密解密過程中,每一輪都需要一個(gè)與輸入分組具有相同長(zhǎng)度的擴(kuò)展密鑰wi的參與。由于外部輸入的加密密鑰長(zhǎng)度有限,所以在算法中要用一個(gè)密鑰擴(kuò)展程序把外部密鑰擴(kuò)展成

11、更長(zhǎng)的比特串,以生成各輪的加密和解密密鑰。通過生成器產(chǎn)生nr+1輪輪密鑰,每個(gè)輪密鑰由nb個(gè)字組成,共有nb(nr+1)個(gè)字wi,i=0,1,nb(nr+1)-1。5.系統(tǒng)功能程序設(shè)計(jì)(代碼參考自csdn論壇)#include stdafx.h#include (aes加解密).h#include#include#includebyte in4nb; /in數(shù)組用于讀入需要或解密的數(shù)據(jù)byte key4nk; /key數(shù)組用于讀入初始密鑰 byte wnr+1nk4; /w數(shù)組用于保存所有擴(kuò)展后的密鑰 /s盒byte s_box256=0x63,0x7c,0x77,0x7b,0xf2,0x6b

12、,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,0x09,0x83

13、,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff

14、,0xf3,0xd2,0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56

15、,0xf4,0xea,0x65,0x7a,0xae,0x08,0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,0x8c,0xa1,0x89,0x0d,0xbf,0xe6

16、,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16;/逆s盒byte invs_box256 =0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0x

17、fa,0xc3,0x4e,0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0x

18、e4,0x58,0x05,0xb8,0xb3,0x45,0x06,0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,0x47,0xf1,0x1a,0x71,0x1d,0x

19、29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,0xa0,0x

20、e0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d;/表alog256是生成元表示到多項(xiàng)式的映射表。使用的生成元為x+1byte alog256 =0x01,0x03,0x05,0x0f,0x11,0x33,0x55,0xff,0x1a,0x2e,0x72,0x96,0xa1,0xf8,0x13,0x35,0x5f,0xe1,0x38,0x48

21、,0xd8,0x73,0x95,0xa4,0xf7,0x02,0x06,0x0a,0x1e,0x22,0x66,0xaa,0xe5,0x34,0x5c,0xe4,0x37,0x59,0xeb,0x26,0x6a,0xbe,0xd9,0x70,0x90,0xab,0xe6,0x31,0x53,0xf5,0x04,0x0c,0x14,0x3c,0x44,0xcc,0x4f,0xd1,0x68,0xb8,0xd3,0x6e,0xb2,0xcd,0x4c,0xd4,0x67,0xa9,0xe0,0x3b,0x4d,0xd7,0x62,0xa6,0xf1,0x08,0x18,0x28,0x78,0x88

22、,0x83,0x9e,0xb9,0xd0,0x6b,0xbd,0xdc,0x7f,0x81,0x98,0xb3,0xce,0x49,0xdb,0x76,0x9a,0xb5,0xc4,0x57,0xf9,0x10,0x30,0x50,0xf0,0x0b,0x1d,0x27,0x69,0xbb,0xd6,0x61,0xa3,0xfe,0x19,0x2b,0x7d,0x87,0x92,0xad,0xec,0x2f,0x71,0x93,0xae,0xe9,0x20,0x60,0xa0,0xfb,0x16,0x3a,0x4e,0xd2,0x6d,0xb7,0xc2,0x5d,0xe7,0x32,0x56

23、,0xfa,0x15,0x3f,0x41,0xc3,0x5e,0xe2,0x3d,0x47,0xc9,0x40,0xc0,0x5b,0xed,0x2c,0x74,0x9c,0xbf,0xda,0x75,0x9f,0xba,0xd5,0x64,0xac,0xef,0x2a,0x7e,0x82,0x9d,0xbc,0xdf,0x7a,0x8e,0x89,0x80,0x9b,0xb6,0xc1,0x58,0xe8,0x23,0x65,0xaf,0xea,0x25,0x6f,0xb1,0xc8,0x43,0xc5,0x54,0xfc,0x1f,0x21,0x63,0xa5,0xf4,0x07,0x09

24、,0x1b,0x2d,0x77,0x99,0xb0,0xcb,0x46,0xca,0x45,0xcf,0x4a,0xde,0x79,0x8b,0x86,0x91,0xa8,0xe3,0x3e,0x42,0xc6,0x51,0xf3,0x0e,0x12,0x36,0x5a,0xee,0x29,0x7b,0x8d,0x8c,0x8f,0x8a,0x85,0x94,0xa7,0xf2,0x0d,0x17,0x39,0x4b,0xdd,0x7c,0x84,0x97,0xa2,0xfd,0x1c,0x24,0x6c,0xb4,0xc7,0x52,0xf6,0x01;/log256是多項(xiàng)式表示到生成元的映

25、射表。使用的生成元為x+1byte log256 = 0x00,0x00,0x19,0x01,0x32,0x02,0x1a,0xc6,0x4b,0xc7,0x1b,0x68,0x33,0xee,0xdf,0x03,0x64,0x04,0xe0,0x0e,0x34,0x8d,0x81,0xef,0x4c,0x71,0x08,0xc8,0xf8,0x69,0x1c,0xc1,0x7d,0xc2,0x1d,0xb5,0xf9,0xb9,0x27,0x6a,0x4d,0xe4,0xa6,0x72,0x9a,0xc9,0x09,0x78,0x65,0x2f,0x8a,0x05,0x21,0x0f,0xe

26、1,0x24,0x12,0xf0,0x82,0x45,0x35,0x93,0xda,0x8e,0x96,0x8f,0xdb,0xbd,0x36,0xd0,0xce,0x94,0x13,0x5c,0xd2,0xf1,0x40,0x46,0x83,0x38,0x66,0xdd,0xfd,0x30,0xbf,0x06,0x8b,0x62,0xb3,0x25,0xe2,0x98,0x22,0x88,0x91,0x10,0x7e,0x6e,0x48,0xc3,0xa3,0xb6,0x1e,0x42,0x3a,0x6b,0x28,0x54,0xfa,0x85,0x3d,0xba,0x2b,0x79,0x0

27、a,0x15,0x9b,0x9f,0x5e,0xca,0x4e,0xd4,0xac,0xe5,0xf3,0x73,0xa7,0x57,0xaf,0x58,0xa8,0x50,0xf4,0xea,0xd6,0x74,0x4f,0xae,0xe9,0xd5,0xe7,0xe6,0xad,0xe8,0x2c,0xd7,0x75,0x7a,0xeb,0x16,0x0b,0xf5,0x59,0xcb,0x5f,0xb0,0x9c,0xa9,0x51,0xa0,0x7f,0x0c,0xf6,0x6f,0x17,0xc4,0x49,0xec,0xd8,0x43,0x1f,0x2d,0xa4,0x76,0x7

28、b,0xb7,0xcc,0xbb,0x3e,0x5a,0xfb,0x60,0xb1,0x86,0x3b,0x52,0xa1,0x6c,0xaa,0x55,0x29,0x9d,0x97,0xb2,0x87,0x90,0x61,0xbe,0xdc,0xfc,0xbc,0x95,0xcf,0xcd,0x37,0x3f,0x5b,0xd1,0x53,0x39,0x84,0x3c,0x41,0xa2,0x6d,0x47,0x14,0x2a,0x9e,0x5d,0x56,0xf2,0xd3,0xab,0x44,0x11,0x92,0xd9,0x23,0x20,0x2e,0x89,0xb4,0x7c,0xb

29、8,0x26,0x77,0x99,0xe3,0xa5,0x67,0x4a,0xed,0xde,0xc5,0x31,0xfe,0x18,0x0d,0x63,0x8c,0x80,0xc0,0xf7,0x70,0x07;/密鑰擴(kuò)展中使用的異或輪常數(shù)byte rcon104=0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x1b,0

30、x00,0x00,0x00,0x36,0x00,0x00,0x00;/輪密鑰加void addroundkey(byte state4nb,byte p4nb)int i, j;for(i = 0; i 4; i+)for(j = 0; j nb; j+)stateij = stateijpij;/狀態(tài)數(shù)組與每一輪子密鑰異或/字節(jié)替換void subbytes(byte state4nb)int i, j;for(i = 0; i 4; i+)for(j = 0; j nb; j+)stateij = s_boxstateij;/行移位void shiftrows(byte state4nb)

31、 /void memcpy(void *destin,void *source,unsigned n) 操作為從source復(fù)制一個(gè)n個(gè)字節(jié)的塊到destinbyte temp4;int time; /向左循環(huán)移位的位數(shù)for(time = 1; time 4; time+) memcpy(temp, statetime, time);/先存儲(chǔ)需要左移的位到temp中memcpy(statetime, statetime+time, 4-time);/將余下的塊左移memcpy(statetime+4-time, temp, time);/將事先存于temp中的位放到狀態(tài)數(shù)組中 /列混合中使用

32、的mul函數(shù)byte mul(byte a, byte b)if(a != 0 & b != 0)return alog(loga + logb) % 255;/參考代碼,不太懂else return 0;/列混合void mixcolumns(byte state4nb)byte temp4nb;int i, j;/將state函數(shù)存入temp中for(i = 0; i 4; i+)for(j = 0; j nb; j+)tempij = stateij;/按列做調(diào)用mul函數(shù)做列混合,以狀態(tài)的每一列作為輸入與混合每一列的數(shù)據(jù)得到新的列for(i = 0; i 4; i+)state0i =

33、 mul(0x02, temp0i) mul(0x03, temp1i) temp2i temp3i;state1i = mul(0x02, temp1i) mul(0x03, temp2i) temp0i temp3i; state2i = mul(0x02, temp2i) mul(0x03, temp3i) temp1i temp0i;state3i = mul(0x02, temp3i) mul(0x03, temp0i) temp2i temp1i;/字節(jié)變換 密鑰擴(kuò)展中使用void subwords(byte ank)int i;for(i = 0; i nk; i+)ai = s

34、_boxai;/左循環(huán)移位(只移一位) 密鑰擴(kuò)展中使用void rotword(byte tempnb)byte temp1;int i;temp1 = temp0;for(i = 1; i nb; i+)tempi-1 = tempi;tempnb-1 = temp1;/密鑰擴(kuò)展void keyexpansion(byte key4nk,byte wnr+1nk4)byte knb * (nr + 1)4; /計(jì)算是用二維數(shù)組(便于計(jì)算),最后存于三維數(shù)組w中byte tempnk;byte b 44;int i,j,l; /存儲(chǔ)初始密鑰for(i = 0; i 4; i+)for(j =

35、0; j nk; j+)kij = keyij;/計(jì)算擴(kuò)展密鑰for(i = nk; i nb * (nr + 1); i+)for(j = 0; j 4; j+)tempj = ki - 1j;if(i%nk = 0)rotword(temp);subwords(temp);for(j = 0; j 4 ; j+)tempj = tempjrconi/nk-1j;for(j = 0; j 4; j+) kij = ki-nkjtempj;printf(%x , kij);printf(n);for (i = 0; i nr + 1; i+)for (j = 0; j nk; j+)for (

36、l = 0; l 4; l+)wijl = k4 * i + jl;for (j = 0; j nk; j+)for (l = 0; l 4; l+)bjl = wilj;for (j = 0; j nk; j+)for (l = 0; l 4; l+)wijl = bjl;/加密模塊void cipher(char file164, char file264)byte state4nb;int i, j, k;int round = 0;byte wnr+1nk4;byte out4nb;file *fp;openfile(file1, file2); /從文件中讀取初始密鑰和明文 keye

37、xpansion(key,w); /調(diào)用密鑰擴(kuò)展函數(shù)將所有的擴(kuò)展密鑰存于w數(shù)組中/將讀入的明文進(jìn)行轉(zhuǎn)秩for(i = 0; i 4; i+)for(j = 0; j nb; j+)stateji = inij; /將初始明文和初始密鑰存于文件中并在程序中輸出 if (fp = fopen(加密要求輸出結(jié)果.txt, w) = null)afxmessagebox(打開文件錯(cuò)誤!);exit(0);fprintf(fp,初始明文:n);for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j nb; j+)fprintf(fp,%0x ,stateij);fpr

38、intf(fp, nn初始密鑰:n);for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j nb; j+)fprintf(fp,%0x ,w0ji);addroundkey(state,wround); /初始輪密鑰加 /開始循環(huán)nr輪 for(round = 1; round nr; round+)subbytes(state);shiftrows(state);mixcolumns(state);addroundkey(state, wround);if (round = 8)fprintf(fp, nn第九輪輪密鑰:n);for (j = 0; j

39、4; j+)fprintf(fp, nw%d= , (round+1)*4+j);for (k = 0; k 4; k+)fprintf(fp, %x , wround+1kj);fprintf(fp, nn明文加密到第九輪:n);for (j = 0; j 4; j+)fprintf(fp, n);for (k = 0; k 4; k+)fprintf(fp, %x , statejk); subbytes(state); shiftrows(state); addroundkey(state, wround); fprintf(fp, nn第十輪輪密鑰:n);for (j = 0; j 4

40、; j+) fprintf(fp, nw%d= ,round*4+j);for (k = 0; k 4; k+)fprintf(fp, %x , wroundkj);fprintf(fp, nn加密完成后輸出:n);for (j = 0; j 4; j+)fprintf(fp, n);for (k = 0; k 4; k+)fprintf(fp, %x , statejk);fclose(fp);if (fp = fopen(ciphermessage.txt, w) = null)afxmessagebox(打開文件錯(cuò)誤!);exit(0); for(i = 0; i 4; i+)for(j

41、 = 0; j nb; j+)outji = stateij;for(i = 0; i 4; i+)for(j = 0; j 0, position 4; time-, position+)memcpy(temp, stateposition, time);memcpy(stateposition, stateposition+time, 4-time);memcpy(stateposition+4-time, temp, time);/逆字節(jié)替代void invsubbytes(byte state4nb)int i, j;for(i = 0; i 4; i+)for(j = 0; j nb

42、; j+)stateij = invs_boxstateij;/逆列混合void invmixcolumns(byte state4nb) byte temp4nb;int i, j;for(i = 0; i 4; i+)for(j = 0; j nb; j+)tempij = stateij;for(i = 0; i 4; i+)state0i = mul(0x0e, temp0i) mul(0x0b, temp1i) mul(0x0d, temp2i) mul(0x09, temp3i);state1i = mul(0x0e, temp1i) mul(0x0b, temp2i) mul(0

43、x09, temp0i) mul(0x0d, temp3i); state2i = mul(0x0e, temp2i) mul(0x0b, temp3i) mul(0x09, temp1i) mul(0x0d, temp0i);state3i = mul(0x0e, temp3i) mul(0x0b, temp0i) mul(0x09, temp2i) mul(0x0d, temp1i);/解密模塊void invcipher(char file164, char file264)byte state4nb;byte wnr+1nk4;byte out4nb;int i, j, k;file

44、*fp;int round = 10;openfile(file1, file2);keyexpansion(key,w);for(i = 0; i 4; i+)for(j = 0; j nb; j+)stateji = inij;/將初始密文和初始密鑰存入文件中并在程序中輸出 if (fp = fopen(解密要求輸出結(jié)果.txt, w) = null)afxmessagebox(打開文件錯(cuò)誤!);exit(0);fprintf(fp,初始密文:n);for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j nb; j+)fprintf(fp,%0x ,stateij);fprintf(fp, nn初始密鑰:n);for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j 0; round-)printf(第%d輪n, round);invshiftrows(state);invsubbytes(state);addroundkey

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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ǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論