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

下載本文檔

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

文檔簡(jiǎn)介

1、ISMDEEPDES一、目標(biāo):學(xué)習(xí)和了解DES加密算法,設(shè)計(jì)一個(gè)基于DES加密算法的加解密軟件二.意義:煉團(tuán)隊(duì)合作能力,加強(qiáng)對(duì)網(wǎng)絡(luò)安全技術(shù)的了解,學(xué)習(xí)新的編程軟件的使用方法與技術(shù)三、理論基礎(chǔ):DES加密算法DES使用一個(gè)56位的密鑰以及附加的8位奇偶校驗(yàn)位,產(chǎn)生最大 64位的分組大小。這是一個(gè)迭代的分組密碼,使用稱為Feistel的技術(shù), 其中將加密的文本塊分成兩半。使用子密鑰對(duì)其中一半應(yīng)用循環(huán)功能,然 后將輸出與另一半進(jìn)行"異或"運(yùn)算:接著交換這兩半,這一過程會(huì)繼續(xù)下去, 但繪后一個(gè)循環(huán)不交換。DES使用16個(gè)循環(huán),使用異或,置換,代換, 移位操作四種基本運(yùn)算。其入口參數(shù)

2、有三個(gè):key、data, mode。 key為加密解密使用的密鑰, data為加密,解密的數(shù)據(jù),mode為其工作模式。當(dāng)模式為加密模式時(shí),明文 按照64位進(jìn)行分組,形成明文組,key用于對(duì)數(shù)據(jù)加密,當(dāng)模式為解密模 式時(shí),key用于對(duì)數(shù)據(jù)解密。實(shí)際運(yùn)用中,密鑰只用到了 64位中的56位, 這樣才具有高的安全性。本次里面用到計(jì)算有三種:置換,移位和異或。算法的整體結(jié)構(gòu):有16個(gè)相同的處理過程,稱為“回次,并在首位各 有一次置換。在主處理回次前,數(shù)據(jù)塊被分成兩個(gè)32位的半塊,并被分別 處理。圖中的符號(hào)代表異或操作。“F函數(shù)將數(shù)據(jù)半塊與某個(gè)子密鑰進(jìn)行 處理。然后,一個(gè)F函數(shù)的輸出與另一個(gè)半塊異或之后

3、,再與原本的半塊 組合并交換順序,進(jìn)入下一個(gè)回次的處理。在最后一個(gè)回次完成時(shí),兩個(gè) 半塊不必交換順序。擴(kuò)張一用擴(kuò)張置換將32位的半塊擴(kuò)展到48位,其輸出包括8個(gè)6位的 塊,每塊包含4位對(duì)應(yīng)的輸入位,加上兩個(gè)鄰接的塊中緊鄰的位。與密鑰混合一用異或操作將擴(kuò)張的結(jié)果和一個(gè)子密鑰進(jìn)行混合。16個(gè) 48位的子密鑰一每個(gè)用于一個(gè)回次的F變換一是利用密鑰調(diào)度從主密鑰生 成的。S盒一在與子密鑰混合之后,塊被分成8個(gè)6位的塊,然后使用“S盒, 或稱"置換盒進(jìn)行處理。8個(gè)S盒的每一個(gè)都使用以査找表方式提供的非線 性的變換將它的6個(gè)輸入位變成4個(gè)輸出位。S盒提供了 DES的核心安全性 一如果沒有S盒,密碼

4、會(huì)是線性的,很容易破解。P置換一最后,S盒的32個(gè)輸出位利用固定的置換,“P置換進(jìn)行重組。 這個(gè)設(shè)計(jì)是為了將每個(gè)S盒的4位輸出在下一回次的擴(kuò)張后,使用4個(gè)不 同的S盒進(jìn)行處理。密鑰調(diào)度右圖顯示了加密過程中的密鑰調(diào)度一產(chǎn)生子密鑰的算法。首 先,使用選擇置換1(PC1)從64位輸入密鑰中選出56位的密鑰一剩下的 8位要么直接丟棄,要么作為奇偶校驗(yàn)位。然后,56位分成兩個(gè)28位的半 密鑰;每個(gè)半密鑰接下來都被分別處理。在接下來的回次中,兩個(gè)半密鑰 都被左移1或2位(由回次數(shù)決定),然后通過選擇置換2 (PC-2)產(chǎn)生481/15ISMDEEPDES位的子密鑰一每個(gè)半密鑰24位。移位(圖中由<&

5、lt;標(biāo)示)表明每個(gè)子密鑰中 使用了不同的位,每個(gè)位大致在16個(gè)子密鑰中的14個(gè)出現(xiàn)。解密過程中,除了子密鑰輸出的順M相反外,密鑰調(diào)度的過程與加密 完全相同。詳細(xì)設(shè)計(jì):1頭文件 DES.h設(shè)計(jì)方法;類的共有接口只設(shè)計(jì)一個(gè),即加密解密函數(shù),用一個(gè)bool參數(shù) 區(qū)分是加密還是解密。其他像讀文件,寫文件,加密變換函數(shù)等等設(shè)置為 私有涉及內(nèi)容:文件操作,STL向量,數(shù)制變換,位操作及函數(shù)設(shè)計(jì)等等。 代碼:include <fstream>#inelude <string>#in elude <iostream>include <strstream>#in

6、elude <vector>#include <assert.h>using namespace std;/* DES類聲明/class DESpublic:DES();/*構(gòu)造函數(shù)*/void encryptfbool flag = true);/*加解密函數(shù)*7private:string plaintextFilePath_;/*明文文件路徑/ string ciphertextFilePath _;/* 密文文件路徑 */ string keyFilename_;/*密鑰文件路徑*/string plaintext/* 明文存儲(chǔ)*/string cipherte

7、xt廠密文存儲(chǔ)"7string key_;/*密鑰存儲(chǔ)*/vector<vector<int> > subkey_;/* 子密鑰存儲(chǔ) *7 void getsubkeyj);/* 計(jì)算子密鑰"72/15ISMDEEPDESvoid setPlaintextFilePath_();/* 設(shè)置明文路徑拿/void setCiphertextFilePathJ);/* 設(shè)置密文路徑 */void setKeyFilePath_();/* 設(shè)置密鑰路徑 */string read Data Jstring filename);/*讀取文件拿/void sav

8、eDataJstring filename,string data);/*保存文件*/vector<int> string2bit_(string str); /*string 類型轉(zhuǎn)為 ASCII 二進(jìn)制數(shù)*/ string bit2string_(vector<int> v);/*ASCII 進(jìn)制數(shù)轉(zhuǎn)為 string 類型*/ vector<int> int2bit_(vector<int> veclnt);/*整形數(shù) 0-15 化為:進(jìn)制未/vector<int> vecXorJvector<int> vL, vec

9、tor<int> vR);/* 向量異或"/vector<int> leftlterMove_(vector<int> movVecJnt loopStep);/*數(shù)據(jù)左移 loopStep 位/ vector<int> vecReplace_(vector<int> v,const unsigned int vArray);/*向杲數(shù)據(jù)按 vArray 中數(shù)據(jù) 重排序*/一vector<int> vecMerge_(vector<int> vL# vector<int> vR);/合并兩

10、個(gè)向量*7vector<int> f_(vector<int> vecR, vector<int> vecKey);/*f 函數(shù)/vector<int> S_(vector<int> vecRKey);/*S 盒函數(shù)*/*變換矩陣./static const unsigned int PC_1_56;static const unsigned int PC_2_48;static const unsigned int L00P_16;static const unsigned int IP_64);static const unsig

11、ned int IPR64;static const unsigned int SBOX 81 41 (161;static const unsigned int E_48;static const unsigned int P_32;;2. DES加密代碼創(chuàng)建基于DES加解密的函數(shù),以備后面的代碼調(diào)用。 代碼:include "DES.h"3/15ISMDEEPDES#/15ISMDEEPDES#/15ISMDEEPDES/*靜態(tài)常量放在這里*/const unsigned int DES:IP_64=5& 50, 42, 34, 26, 18, 10, 2, 6

12、0, 52, 44, 36, 28, 20, 12, 4,62, 54, 46, 3& 30, 22t 14, 6, 64, 56, 4& 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;const unsigned int DES:IPR_64=40, 8, 4&16, 56, 24,64, 32, 39,7, 47,15, 55, 23, 63, 31,3&

13、amp; 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,5& 26z 33,1, 41,9, 49, 17, 57, 25;const unsigned int DES:PC_1_56=廠注釋的部分是對(duì)應(yīng)64位帶奇偶校驗(yàn)的*7/*57, 49, 41, 33, 25,17,9,1, 5& 50, 42, 34, 26,18,10,2, 59, 51, 43, 35,

14、 27,19,11,3, 60, 52, 44, 36,63, 55, 47, 39, 31, 23,15,1, 62, 54, 46, 38, 30, 22,14,6, 61, 53, 45, 37, 29, 21, 13,5, 28, 20,12,4750, 43, 36, 29, 22,15,&1, 51, 44, 37, 30, 23,16,9,2, 52, 45, 3& 31, 24, 17, 10,3, 53, 46, 39, 32,56, 49,42, 35, 2& 21,14,7, 55, 48, 41, 34, 27, 20,13,6, 54, 47,

15、 40, 33, 26,19, 12,5, 25,18,11,4;const unsigned int DES:PC_2_48=廠注釋的部分是對(duì)應(yīng)64位帶奇偶校驗(yàn)的-7廠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*/14.17.11.24, 1,5,3,28,15,6,21,10,23,19,12, 4, 26,8,16

16、,7,27,20,13,2,13.24, 3,9, 19,27,2,12,23,17,5,20,16, 21,11, 2&6,15,14,12,8,1,4;const unsigned int DES:LOOP_16=5/15ISMDEEPDES1,1,2, 2, 2, 2, 2, 2,1, 2, 2, 2, 2, 2, 2,1 const unsigned int DES:SBOX_8416=/SI14,4,13,1,2,15,11,&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

17、,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,/S215,1, 8,14, 6,11, 3,4, 9, 7, 2,13t 12, 0, 5,10,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,/S310,0,9,14, 6, 3,15, 5,1,13,12,1,11, 4, 2, 8

18、,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,12, 5,10,14, 7,I, 10,13,0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2.12,/S47,13,14, 3, 0, 6, 9,10,1, 2, & 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,

19、10,1,13,8, 9,4, 5,11,12, 7, 2,14,/S52.12, 4,1, 7,10,11, 6, & 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,II, & 12,1,1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,/S612.1.10.15, 9, 2, 6, 8, 0,13, 3,4,14, 7, 5,11,10,15,4, 2, 7,12, 9, 5, 6,

20、1,13,14, 0,11,3, &9.14.15, 5, 2,8,12, 3, 7, 0, 4,10,1, 13,11, 6,4, 3,2,12, 9, 5,15,10z 11,14,1, 7, 6, 0, 8,13,/S74.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, & 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, 0,15,14, 2,

21、 3,12,/S813, 2, 8, 4, 6,15,11,1,10,9, 3,14, 5, 0,12, 7,8,10, 3, 7,4,12, 5, 6,11, 0,14, 9, 2,6/15ISMDEEPDES7,11, 4, 1, 9Z12,14,乙 0, 6,10,13,15, 3, 5, &2,1,14,1. 4,10, & 13,15,12,9,0, 3, 5Z 6,11 const unsigned int DES:EJ48=32,1,2,3,4,5,4,5,6,1,8,9,&9,10,11,12,13,12,13,14,15,16,17,16,17,18,

22、19, 20, 21, 20, 21, 22, 23, 24, 25,24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32,1; const unsigned int DES:P_32=16z lt 20, 21, 29z 12, 2& 17,1, 15, 23, 26,5,18, 31,10,2. & 24,14, 32, 27,3,9,19,13, 30,6, 22, llz 4, 25 y*#*審專*車*專療車車>|()|(*拿*床4(專療4<車*拿療車拿審拿*拿4<4(拿*拿*拿車嘟拿* /* DES類實(shí)現(xiàn)/DES:DE

23、S():subkey_(16/vector<int>(48)廠構(gòu)造函數(shù)"/void DES:encrypt(bool flag /* = true */)嚴(yán)加密函數(shù)*/廠讀取文件*/vector<int> textBit;if (flag)廠加密/setPlaintextFilePathJ);plaintext. = readData JplaintextFilePath;textBit = string2bit_(plaintext_);/*不夠 64bit 的整數(shù),補(bǔ) 0,如 E*/unsigned int zeroFill = (textBit.size

24、()%64) ? (64 - textBit.size()%64): 0;for (unsigned int i = 0; i < zeroFill; i+)textBit. push_back(0);6/15ISMDEEPDESelse廣解密*/setCiphertextFilePathJ);ciphertext. = readData_(ciphertextFilePath_); textBit = string2bitjciphertext_);/*讀取密鑰文件/getsubkey_();廠循環(huán)讀取64bit加密"7 unsigned int len = textBit.

25、size(); unsigned int count = 0;vector<int> secret;/* 保存密文*/while ( count < (len / 64)vector<int> textBitTmp;for (unsigned int i = 64*count; i < 64*count+64; i+) textBitTmp.push_back(textBiti);/ip置換*7vector<int> textBitTmpJP;textBitTmp IP = vecReplace (textBitTmpJP );廣劃分L0和RO*

26、/vector<vector<int> > L(17,vectorvint>(32); vector<vector<int> > R(17#vector<int>(32); for (int i = 0; i < 32; i+)L0i = textBitTmp叩;for (int i = 0; i < 32; i+)R0i = textBitTmp_IPi+32;廠迭代生成L1-L16ZR1-R167for (int i = 1; i < 17; i+)Li = R";if (flag)Ri = ve

27、cXorjLfi-l, f_(Ri-l,subkey_i-l);elseRi = vecXor_(Lil, f_(Ri-l,subkey_16i);vector<int> RL = vecMerge_(R16zL16);for (unsigned int i = 0; i < 64; i+)secret.push_back(RLIPR Ji-1); count+;if (flag)ciphertext_ = bit2stringjsecret); setCiphertextFilePathJ);saveData_(ciphertextFilePath_,ciphertext;

28、elseplain text = bit2string (secret); setPlaintextFilePathJ); saveDataJplaintextFilePathplaintext;void DES:setPlaintextFilePath_()廠設(shè)置明文路徑/string plaintextFilePath;cout« "請(qǐng)輸入明文文件路徑(例如:c:/plaintext.txt):"cin » plaintextFilePath;plaintextFilePath_ = plaintextFilePath;void DES:setCip

29、hertextFilePath_()廠設(shè)置密文路徑審/string ciphertextFilePath;cout« "請(qǐng)輸入密文文件路徑(例如:c:/ciphertext.txt):"cin » ciphertextFilePath;ciphertextFilePath_ = ciphertextFilePath;void DES:setKeyFilePath_()廠設(shè)置密鑰路徑*/string keyFilename;cout« ”請(qǐng)輸入密鑰文件路徑(例如:c:/key.txt):"cin >> keyFilename

30、;keyFilename_ = keyFile name;string DES:readData_(string filename)廠通用函數(shù):讀取文件內(nèi)容到string中,包含空格和回車符等*7 ifstream in;ostrstream os;in.op en (filename.c_str()Jos:in);os « in.rdbuf() « ends;in.close();return os.str();void DES:saveData (string filename.string data)廠通用函數(shù):保存數(shù)據(jù)到文件中,包含空格和回車*7of st ream

31、 out(filename.c_str();outwrite(data.c_str(),datasize();out.close();vector<int> DES:string2bit_(string str)/string轉(zhuǎn)為:進(jìn)制*/vector<int> v;for (unsigned int i = 0; i < str.length(); i+)int mask = 128;while (mask > 0)v.push_back( (mask&stri) > 0);mask = mask » 1;return v;stri

32、ng DES:bit2string_(vector<int> v)/* :進(jìn)制轉(zhuǎn)為string*/assert(v.size()%8 = 0);unsigned int count = OJen = v.sizef);string str;while ( count < (len)int mask = 128;int ch = 0;for (unsigned int i = 8*count; i < 8*count+8;i+) ch += (mask & (vi*mask);mask = mask » 1;str += (char)ch;cou nt+

33、;return str;vector<int> DES:int2bit_(vector<int> veclnt)廠整形數(shù)0-15化為二進(jìn)制/vector<int> v;for (unsigned int i = 0; i < veclnt.size(); i+)int mask = 8;while (mask > 0)v.push_back( (mask&veclnti( > 0); mask = mask » 1;return v;vector<int> DES:vecXor_(vector<int&g

34、t; vL, vector<int> vR)廠向量逐位異或*/assert(vL.size() = vR.size();for (unsigned int i = 0; i < vL.size(); i+)vLi A= vRi;return vL;vector<int> DES:leftlterMove_(vector<int> movVecJnt loopStep)/*數(shù)據(jù)左移loopStep位*/unsigned int len = movVec.sizef);for (int i = 0; i < loopStep; i+)movVec.p

35、ush_back(movVeci);for (unsigned int i = 0; i < len; i+)movVecfi = movVeci+loopStep;movVec.eraselmovVec.endO-loopStep.movVec.endf);return movVec;vector<int> DES:vecReplaceJvector<int> yconst unsigned int vArray)廠通用函數(shù):數(shù)據(jù)按照數(shù)組元素?fù)Q位*/vector<int> vTmp;for (unsigned int i = 0; i < v.

36、sizef); i+)vTmp.push_back(vvArrayi-l);return vTmp;vector<int> DES:vecMerge_(vector<int> vL, vector<int> vR)廠合并兩個(gè)向量*/vector<int> vLR;for (unsigned int i = 0; i < vL.size(); i+)vLR.push_back(vLi);for (unsigned int i = 0; i < vR.size(); i+)vLR.push_back(vRi);return vLR;vec

37、tor<int> DES:f_(vector<int> vecR, vector<int> vecKey)/*f函數(shù),DES加密的核心*7assertf (vecR.size() = 32) && (vecKey.sizef) = 48);vector<int> vecR_E,vecR_E_P;for (unsigned int i = 0; i < 48; i+)vecR_E.push_back(vecREil);vecR_E = S_(vecXor_(vecR_E.vecKey);vecR_E_P = vecReplac

38、e_(vecR_E,P_);return vecR E P;vector<int> DES:S_(vector<int> vecRKey)/*S 函數(shù),6bit 入 4bit 出*7assert(vecRKey.size() = 48);vector<vector<int> > S_out(8#vector<int>(6);vector<int> tmp;for (unsigned int i = 0; i < 8; i+)for (unsigned int j = 0; j < 6; j+)S_outiD =

39、 vecRKey6*i+j;int a = 2 * S_outi0 + S_outi;int b = 8 * S_outil + 4 * S_outi2 + 2 * S_outi3 + S_outi4; tmp.push_back(SBOX_i ab);return int2bit_(tmp);void DES:getsubkey_()廠函數(shù)功能:獲得子密鑰*/廠讀取密鑰文件*/setKeyFilePathJ);key_ = readData_(keyFilename_);vector<int> keyBit = string2bit_(key_);廠生成子密胡*/I-keybit

40、 通過 PC/vector<int> keyBit_PC_l = vecReplace_(keyBit,PC_l_);/*2 -迭代 C,D*/vector<vector<int> > C(17/vector<int>(28);vector<vector<int> > D(17/vector<int>(28);for (int i = 0; i < 28; i+)C0i = keyBit_PC_li;for (int i = 0; i < 2& i+)D0i = keyBit_PC_li+28;/*3循壞

溫馨提示

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