密碼學(xué)實(shí)驗(yàn)三—DES算法的實(shí)現(xiàn)_第1頁
密碼學(xué)實(shí)驗(yàn)三—DES算法的實(shí)現(xiàn)_第2頁
密碼學(xué)實(shí)驗(yàn)三—DES算法的實(shí)現(xiàn)_第3頁
密碼學(xué)實(shí)驗(yàn)三—DES算法的實(shí)現(xiàn)_第4頁
密碼學(xué)實(shí)驗(yàn)三—DES算法的實(shí)現(xiàn)_第5頁
已閱讀5頁,還剩15頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)三 用DES實(shí)現(xiàn)對(duì)數(shù)據(jù)的加解密一、實(shí)驗(yàn)?zāi)康?. 用DES加密方法實(shí)現(xiàn)對(duì)明文的加密2用DES解密加密后的密文二、實(shí)驗(yàn)內(nèi)容1、給定八字節(jié)明文,輸入8位密鑰對(duì)明文加密2、用同一個(gè)密鑰對(duì)加密的密文解密的到原來的明文三、實(shí)驗(yàn)原理實(shí)驗(yàn)步驟(包括流程圖、功能模塊)【實(shí)驗(yàn)原理】1、DES算法把64位的明文輸入塊變?yōu)?4位的密文輸出塊,它所使用的密鑰也是64位,其功能是把輸入的64位數(shù)據(jù)塊按位重新組合,并把輸出分為L0、R0兩部分,每部分各長32位,其置換規(guī)則見下表:58,50,12,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64

2、,56,48,40,32,24,16,8,57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,2. 將輸入的第58位換到第一位,第50位換到第2位,.,依此類推,最后一位是原來的第7位。L0、R0則是換位輸出后的兩部分,L0是輸出的左32位,R0 是右32位,例:設(shè)置換前的輸入值為D1D2D3.D64,則經(jīng)過初始置換后的結(jié)果為:L0=D550.D8;R0=D57D49.D7。經(jīng)過26次迭代運(yùn)算后。得到L16、R16,將此作為輸入,進(jìn)行逆置換,即得到密文輸出。逆置換

3、正好是初始置的逆運(yùn)算,例如,第1位經(jīng)過初始置換后,處于第40位,而通過逆置換,又將第40位換回到第1位,其逆置換規(guī)則如下表所示:40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58 26,33,1,41, 9,49,17,57,25,3. DES算法的解密過程是一樣的,區(qū)別僅僅在於第一次迭代時(shí)用子密鑰K15,第二次K14、.,最后一次用

4、K0,算法本身并沒有變化?!舅惴鞒虉D】【功能模塊】1. 輸入明文: void DES:SetMsg(char* _msg, int _length)if (_length>8)return;for (int i = 0; i < _length; i+)msgi = _msgi;/轉(zhuǎn)換成二進(jìn)制Char2Bit(msg, bmsg, 8);2. 輸入密鑰: void DES:SetKey(char* _key, int _length)if (_length>8)return;for (int i = 0; i < _length; i+)keyi = _keyi;/轉(zhuǎn)

5、成二進(jìn)制Char2Bit(key, bkey, 8);3. 各種置換: void DES:InitSwap(bool in64)/打亂for (int i = 0; i < 32; i+)lmsgii = inipi - 1;rmsgii = inipi + 32 - 1;/初始逆置換函數(shù)void DES:InitReSwap(bool out64)/組合成64數(shù)組bool temp64;for (int i = 0; i < 32; i+)tempi = rmsgii;temp32 + i = lmsgii;/按照逆ip矩陣for (int i = 0; i < 64; i

6、+)outi = tempback_ipi - 1;/void DES:SubKeyOff(bool* _subkey, int _off)bool temp;for (int i = 0; i < _off; i+)temp = _subkey0;for (int i = 0; i < 27; i+)_subkeyi = _subkeyi + 1;_subkey27 = temp;4. S盒處理: void DES:DealSBox(bool in48, bool out32)bool _in6, _out4;/8個(gè)盒子for (int i = 0; i < 8; i+)/

7、提取盒子for (int j = 0; j < 6; j+)_inj = ini * 6 + j;/壓縮_DealSBox(_in, _out, i);/放進(jìn)out數(shù)組for (int jj = 0; jj < 4; jj+)outi * 4 + jj = _outjj;5. 加密函數(shù): void DES:CrypteFunction(bool in32, int isubkey, bool out32)/e 操作bool temp148;EOperation(in, temp1);bool temp248;Mode2Add(temp1, (bool *)subkeyisubkey

8、, temp2, 48);/ok/盒子壓縮bool temp348;DealSBox(temp2, temp3);/置換運(yùn)算pPOperation(temp3, out);6. 解密函數(shù): void DES:Decipher()bool temp132, temp232;/初始置換ipInitSwap(bcryptedmsg);/16輪迭代加密for (int i = 0; i < 16; i+)if (i % 2 = 0)/L1=R0CopyArray(rmsgi, lmsgi1, 32);/f(R0,k0)CrypteFunction(rmsgi, 15 - i, temp1);/L

9、0+f(R0,k0)Mode2Add(lmsgi, temp1, temp2, 32);/R1=L0+f(R0,k0)CopyArray(temp2, rmsgi1, 32);else/L2=R1CopyArray(rmsgi1, lmsgi, 32);/f(R1,k1)CrypteFunction(rmsgi1, 15 - i, temp1);/L1+f(R1,k1)Mode2Add(lmsgi1, temp1, temp2, 32);/R2=L1+f(R1,k1)CopyArray(temp2, rmsgi, 32);/逆初始置換ipInitReSwap(bdecipher);/轉(zhuǎn)成字符B

10、it2Char(bdecipher, decipher);4、 軟件使用說明(開發(fā)環(huán)境、參數(shù)使用詳細(xì)說明、實(shí)驗(yàn)結(jié)果+相應(yīng)截圖等)【開發(fā)環(huán)境】Microsoft Visual Studio 2015Microsoft Windows 10【參數(shù)使用】 char* msg;char *key;【實(shí)驗(yàn)結(jié)果】實(shí)驗(yàn)數(shù)據(jù):六、參考資料(書籍或網(wǎng)絡(luò)文章)密碼編碼學(xué)與網(wǎng)絡(luò)安全原理與實(shí)踐(第五版)百度七、實(shí)驗(yàn)心得和總結(jié) 做這次DES加解密實(shí)驗(yàn)前首先要充分了解其原理,在這個(gè)實(shí)驗(yàn)中,比較難的就是,要將明文和密鑰經(jīng)過初始變換和十六輪加密變換等一系列變換后才能得到置換,其中的變換步驟比較多,而且比較復(fù)雜,還要將十六進(jìn)制

11、轉(zhuǎn)化為二進(jìn)制,工作量比較大。附:程序源代碼#include"stdafx.h"#include<iostream>#include<string.h>using namespace std;class DESprivate:/明文char msg8;bool bmsg64;/密鑰char key8;bool bkey64;/16個(gè)子密鑰bool subkey1648;/l0 r0中間變量bool rmsgi32, lmsgi32;/第i個(gè)bool rmsgi132, lmsgi132;/第i+1個(gè)/密文bool bcryptedmsg64;char

12、cryptedmsg8;/解密的結(jié)果bool bdecipher64;char decipher8;private:/靜態(tài)常量/不允許在類內(nèi)初始化/初始值換ipconst static int ip64;/子密鑰/置換選擇1const static int c028;const static int d028;/循環(huán)左移表const static int keyoff16;/置換選擇2const static int di48;/加密函數(shù)/e運(yùn)算const static int e_operate48;/sboxconst static int sbox864;/置換運(yùn)算pconst stat

13、ic int p_operate32;/逆初始置換ipconst static int back_ip64;/位掩碼const static char bitmask8;public:/設(shè)置明文和密鑰/_length要小于或等于8void SetMsg(char* _msg, int _length);void SetKey(char* _msg, int _length);/生產(chǎn)子密鑰void ProduceSubKey();/總的的加密流程void Crypte();/解密void Decipher();/輸出密文void OutPutCryptedMsg();/二進(jìn)制轉(zhuǎn)成字符void B

14、it2Char(bool* _barray, char* _carray);/length=64/輸出解密后的明文void OutPutDecipher();private:/字符轉(zhuǎn)成二進(jìn)制,并保存到64位bool數(shù)組中void Char2Bit(char* _carray, bool* _barray, int length);/二進(jìn)制轉(zhuǎn)成字符/void Bit2Char(bool* _barray,char* _carray);/length=64/初始置換void InitSwap(bool in64);/初始逆置換void InitReSwap(bool out64);/循環(huán)左移voi

15、d SubKeyOff(bool* _subkey, int _off);/e運(yùn)算操作函數(shù)void EOperation(bool a32, bool b48);/模2相加/相同為0 不同為1void Mode2Add(bool a, bool b, bool c, int length);/sboxvoid DealSBox(bool in48, bool out32);void _DealSBox(bool in6, bool out4, int box);/p oprarationvoid POperation(bool temp32, bool result32);/加密函數(shù)void

16、CrypteFunction(bool in32, int isubkey, bool out32);/數(shù)組之間賦值void CopyArray(bool array1, bool array2, int size);using namespace std;/靜態(tài)常量const int DES:ip64 = 58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45

17、,37,29,21,13,5,63,55,47,39,31,23,15,7;const int DES:c028 = 57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36;const int DES:d028 = 63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4;const int DES:keyoff16 = 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1;con

18、st int DES:di48 = 14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32;const int DES:e_operate48 = 32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29

19、,30,31,32,1;const int DES:sbox864 = 14, 4, 13, 1, 1, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10

20、,3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2

21、, 12, 5, 10, 14, 7,1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,2, 12, 4, 1, 7,

22、10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3,

23、8,9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,6, 11, 13, 8, 1, 4, 10, 7, 9, 5,

24、0, 15, 14, 2, 3, 12,13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 13, 14, 5, 0, 12, 7,1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11;const int DES:p_operate32 = 16,7,20,21,29,12,28,17,1,15,23,26,5,18,

25、31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25;const int DES:back_ip64 = 40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25;const char DES:bitmask8 = 128,64,32,16,8,4,

26、2,1 ;/實(shí)現(xiàn)函數(shù)/設(shè)置明文/void DES:SetMsg(char* _msg, int _length)if (_length>8)return;for (int i = 0; i < _length; i+)msgi = _msgi;/轉(zhuǎn)換成二進(jìn)制Char2Bit(msg, bmsg, 8);/設(shè)置密鑰/void DES:SetKey(char* _key, int _length)if (_length>8)return;for (int i = 0; i < _length; i+)keyi = _keyi;/轉(zhuǎn)成二進(jìn)制Char2Bit(key, bkey

27、, 8);/字符轉(zhuǎn)成二進(jìn)制void DES:Char2Bit(char* _carray, bool* _barray, int length)/int index=0;for (int i = 0; i <length; i+)for (int j = 0; j < 8; j+)_barrayi * 8 + 7 - j = (_carrayi >> j) & 1;/二進(jìn)制轉(zhuǎn)成字符/void DES:Bit2Char(bool* _barray, char* _carray)char temp;for (int i = 0; i < 8; i+)/數(shù)學(xué)方法

28、轉(zhuǎn)成字符temp = 0;for (int j = 0; j < 8; j+)if (_barrayi * 8 + j = 1)temp |= bitmaskj;/cout<<temp;_carrayi = temp;/初始置換函數(shù)/okvoid DES:InitSwap(bool in64)/打亂for (int i = 0; i < 32; i+)lmsgii = inipi - 1;rmsgii = inipi + 32 - 1;/初始逆置換函數(shù)/okvoid DES:InitReSwap(bool out64)/組合成64數(shù)組bool temp64;for (i

29、nt i = 0; i < 32; i+)tempi = rmsgii;temp32 + i = lmsgii;/按照逆ip矩陣for (int i = 0; i < 64; i+)outi = tempback_ipi - 1;/循環(huán)左移/okvoid DES:SubKeyOff(bool* _subkey, int _off)/有沒有更好的辦法?bool temp;for (int i = 0; i < _off; i+)temp = _subkey0;for (int i = 0; i < 27; i+)_subkeyi = _subkeyi + 1;_subke

30、y27 = temp;/生產(chǎn)子密鑰/okvoid DES:ProduceSubKey()/置換選擇1bool ctemp28, dtemp28;for (int i = 0; i < 28; i+)ctempi = bkeyc0i - 1;dtempi = bkeyd0i - 1;bool keytemp56;for (int i = 0; i < 16; i+)/循環(huán)左移SubKeyOff(ctemp, keyoffi);SubKeyOff(dtemp, keyoffi);/合并成一個(gè)56數(shù)組for (int j = 0; j <28; j+)keytempj = ctem

31、pj;keytemp28 + j = dtempj;/置換選擇2for (int j = 0; j < 48; j+)subkeyij = keytempdij - 1;/e運(yùn)算/okvoid DES:EOperation(bool a32, bool b48)for (int i = 0; i < 48; i+)bi = ae_operatei - 1;/模2想加/okvoid DES:Mode2Add(bool a, bool b, bool c, int length)for (int i = 0; i < length; i+)if (ai = bi)ci = 0;e

32、lseci = 1;/sbox處理/okvoid DES:DealSBox(bool in48, bool out32)bool _in6, _out4;/8個(gè)盒子for (int i = 0; i < 8; i+)/提取盒子for (int j = 0; j < 6; j+)_inj = ini * 6 + j;/壓縮_DealSBox(_in, _out, i);/放進(jìn)out數(shù)組for (int jj = 0; jj < 4; jj+)outi * 4 + jj = _outjj;/_dealsbox/okvoid DES:_DealSBox(bool in6, bool

33、 out4, int box)int raw, col;raw = in0 * 2 + in5;/轉(zhuǎn)換成十進(jìn)制 行col = in1 * 2 * 2 * 2 + in2 * 2 * 2 + in3 * 2 + in4;/列int result = sboxboxraw * 16 + col;/轉(zhuǎn)成二進(jìn)制for (int i = 3; i >= 0; i-)outi = (result >> (3 - i) & 1;/p操作/okvoid DES:POperation(bool temp32, bool result32)for (int i = 0; i < 3

34、2; i+)resulti = tempp_operatei - 1;/加密函數(shù)/isubkey表明用那個(gè)子密鑰加密 okvoid DES:CrypteFunction(bool in32, int isubkey, bool out32)/e 操作bool temp148;EOperation(in, temp1);bool temp248;Mode2Add(temp1, (bool *)subkeyisubkey, temp2, 48);/ok/盒子壓縮bool temp348;DealSBox(temp2, temp3);/置換運(yùn)算pPOperation(temp3, out);/ de

35、s加密流程void DES:Crypte()/直接用bmsg明文/直接用cryptedmsg存放密文bool temp132, temp232;/初始置換ipInitSwap(bmsg);/16輪迭代for (int i = 0; i < 16; i+)if (i % 2 = 0)/L1=R0CopyArray(rmsgi, lmsgi1, 32);/f(R0,k0)CrypteFunction(rmsgi, i, temp1);/L0+f(R0,k0)Mode2Add(lmsgi, temp1, temp2, 32);/R1=L0+f(R0,k0)CopyArray(temp2, rm

36、sgi1, 32);else/L2=R1CopyArray(rmsgi1, lmsgi, 32);/f(R1,k1)CrypteFunction(rmsgi1, i, temp1);/L1+f(R1,k1)Mode2Add(lmsgi1, temp1, temp2, 32);/R2=L1+f(R1,k1)CopyArray(temp2, rmsgi, 32);/逆初始置換ipInitReSwap(bcryptedmsg);/轉(zhuǎn)成字符Bit2Char(bcryptedmsg, cryptedmsg);/數(shù)組賦值void DES:CopyArray(bool content, bool empty, int size)for (int i = 0; i < size; i+)emptyi = contenti;/解密void DES:Decipher()bool temp132, temp232;/初始置換ipInitSwap(bcryptedmsg);/16輪迭代加密for (int i = 0; i < 16; i+)if (i % 2 = 0)/L1=R0CopyArray(rmsgi, lmsgi1, 32);

溫馨提示

  • 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)論