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

下載本文檔

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

文檔簡介

1、1.頭文件:DES.h設(shè)計(jì)方法:類的共有接口只設(shè)計(jì)一個(gè),即加密解密函數(shù),用一個(gè)bool參數(shù)區(qū)分是加密還是解密。其他像讀文件,寫文件,加密變換函數(shù)等等設(shè)置為私有涉及內(nèi)容:文件操作,STL向量,數(shù)制變換,位操作及函數(shù)設(shè)計(jì)等等。代碼:#include <fstream>#include <string>#include <iostream>#include <strstream>#include <vector>#include <assert.h>using namespace std;/*/*/* DES類聲明 */clas

2、s DESpublic:DES();/*構(gòu)造函數(shù)*/void encrypt(bool flag = true);/*加解密函數(shù)*/private:string plaintextFilePath_;/*明文文件路徑*/string ciphertextFilePath_;/*密文文件路徑*/string keyFilename_;/*密鑰文件路徑*/string plaintext_;/*明文存儲*/string ciphertext_;/*密文存儲*/string key_;/*密鑰存儲*/vector<vector<int> > subkey_;/*子密鑰存儲*/

3、void getsubkey_();/*計(jì)算子密鑰*/void setPlaintextFilePath_();/*設(shè)置明文路徑*/void setCiphertextFilePath_();/*設(shè)置密文路徑*/void setKeyFilePath_();/*設(shè)置密鑰路徑*/string readData_(string filename);/*讀取文件*/void saveData_(string filename,string data);/*保存文件*/vector<int> string2bit_(string str); /*string類型轉(zhuǎn)為ASCII二進(jìn)制數(shù)*/s

4、tring bit2string_(vector<int> v);/*ASCII二進(jìn)制數(shù)轉(zhuǎn)為string類型*/vector<int> int2bit_(vector<int> vecInt);/*整形數(shù)0-15化為二進(jìn)制*/vector<int> vecXor_(vector<int> vL, vector<int> vR);/*向量異或*/vector<int> leftIterMove_(vector<int> movVec,int loopStep);/*數(shù)據(jù)左移loopStep位*/vec

5、tor<int> vecReplace_(vector<int> v,const unsigned int vArray);/*向量數(shù)據(jù)按vArray中數(shù)據(jù)重排序*/vector<int> vecMerge_(vector<int> vL, vector<int> vR);/*合并兩個(gè)向量*/vector<int> f_(vector<int> vecR, vector<int> vecKey);/*f函數(shù)*/vector<int> S_(vector<int> vecRKe

6、y);/*S盒函數(shù)*/ /*/* 變換矩陣 */ static const unsigned int PC_1_56;static const unsigned int PC_2_48;static const unsigned int LOOP_16;static const unsigned int IP_64;static const unsigned int IPR_64;static const unsigned int SBOX_8416;static const unsigned int E_48;static const unsigned int P_32;/*/;2. DES

7、加密代碼創(chuàng)建基于DES加解密的函數(shù),以備后面的代碼調(diào)用。代碼:#include "DES.h"/*/*/* 靜態(tài)常量放在這里 */const unsigned int DES:IP_64 = 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,

8、37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7;const unsigned int DES:IPR_64 = 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

9、, 41, 9, 49, 17, 57, 25;const unsigned int DES:PC_1_56 = /*注釋的部分是對應(yīng)64位帶奇偶校驗(yàn)的*/*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,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*/50, 43,

10、36, 29, 22, 15, 8, 1, 51, 44, 37, 30, 23, 16,9, 2, 52, 45, 38, 31, 24, 17, 10, 3, 53, 46, 39, 32,56, 49, 42, 35, 28, 21, 14, 7, 55, 48, 41, 34, 27, 20,13, 6, 54, 47, 40, 33, 26, 19, 12, 5, 25, 18, 11, 4;const unsigned int DES:PC_2_48 = /*注釋的部分是對應(yīng)64位帶奇偶校驗(yàn)的*/*14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10

11、,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, 7, 27, 20, 13, 2,13, 24, 3, 9, 19, 27, 2, 12, 23, 17, 5, 20,16, 21, 11, 28, 6, 15, 18, 14, 1

12、2, 8, 1, 4;const unsigned int DES:LOOP_16 = 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1;const unsigned int DES:SBOX_8416 = / S1 14, 4, 13, 1, 2, 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,

13、 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,/ S2 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 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,/ S3 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 1

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

15、 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,/ S5 2, 12, 4, 1, 7, 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, 1

16、0, 4, 5, 3,/ S6 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, 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,/ S7 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,13, 0, 11, 7,

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

18、 3, 5, 8,2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11;const unsigned int DES:E_48 = 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, 30, 31, 32, 1;const unsigned int DES:P_32 = 16,

19、7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25;/*/*/* DES類實(shí)現(xiàn) */DES:DES():subkey_(16,vector<int>(48)/*構(gòu)造函數(shù)*/void DES:encrypt(bool flag /* = true */)/*加密函數(shù)*/*讀取文件*/vector<int> textBit;if (flag) /*加密*/ setPlaintextFilePath_()

20、; plaintext_ = readData_(plaintextFilePath_); textBit = string2bit_(plaintext_);/*不夠64bit的整數(shù),補(bǔ)0,如下*/ unsigned int zeroFill = (textBit.size()%64) ? (64 - textBit.size()%64) : 0; for (unsigned int i = 0; i < zeroFill; i+) textBit.push_back(0); else /*解密*/ setCiphertextFilePath_(); ciphertext_ = rea

21、dData_(ciphertextFilePath_); textBit = string2bit_(ciphertext_);/*讀取密鑰文件*/getsubkey_();/*循環(huán)讀取64bit加密*/unsigned int len = textBit.size();unsigned int count = 0;vector<int> secret;/*保存密文*/while ( count < (len / 64) ) vector<int> textBitTmp; for (unsigned int i = 64*count; i < 64*coun

22、t+64; i+) textBitTmp.push_back(textBiti); /*IP置換*/ vector<int> textBitTmp_IP; textBitTmp_IP = vecReplace_(textBitTmp,IP_); /*劃分L0和R0*/ vector<vector<int> > L(17,vector<int>(32); vector<vector<int> > R(17,vector<int>(32); for (int i = 0; i < 32; i+) L0i =

23、textBitTmp_IPi; for (int i = 0; i < 32; i+) R0i = textBitTmp_IPi+32; /*迭代生成L1-L16,R1-R16*/ for (int i = 1; i < 17; i+) Li = Ri-1; if (flag) Ri = vecXor_(Li-1, f_(Ri-1,subkey_i-1); else Ri = vecXor_(Li-1, f_(Ri-1,subkey_16-i); vector<int> RL = vecMerge_(R16,L16); for (unsigned int i = 0;

24、i < 64; i+) secret.push_back(RLIPR_i-1); count+;if (flag) ciphertext_ = bit2string_(secret); setCiphertextFilePath_(); saveData_(ciphertextFilePath_,ciphertext_);else plaintext_ = bit2string_(secret); setPlaintextFilePath_(); saveData_(plaintextFilePath_,plaintext_);void DES:setPlaintextFilePath_

25、()/*設(shè)置明文路徑*/string plaintextFilePath;cout << "請輸入明文文件路徑(例如:c:/plaintext.txt):"cin >> plaintextFilePath;plaintextFilePath_ = plaintextFilePath;void DES:setCiphertextFilePath_()/*設(shè)置密文路徑*/string ciphertextFilePath;cout << "請輸入密文文件路徑(例如:c:/ciphertext.txt):"cin >&

26、gt; ciphertextFilePath;ciphertextFilePath_ = ciphertextFilePath;void DES:setKeyFilePath_()/*設(shè)置密鑰路徑*/string keyFilename;cout << "請輸入密鑰文件路徑(例如:c:/key.txt):"cin >> keyFilename;keyFilename_ = keyFilename;string DES:readData_(string filename)/*通用函數(shù):讀取文件內(nèi)容到string中,包含空格和回車符等*/ifstream

27、 in;ostrstream os;in.open(filename.c_str(),ios:in);os << in.rdbuf() << ends;in.close();return os.str();void DES:saveData_(string filename,string data)/*通用函數(shù):保存數(shù)據(jù)到文件中,包含空格和回車*/ofstream out(filename.c_str();out.write(data.c_str(),data.size();out.close();vector<int> DES:string2bit_(st

28、ring 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;string DES:bit2string_(vector<int> v)/*二進(jìn)制轉(zhuǎn)為string*/assert(v.size()%8 = 0);unsigned int cou

29、nt = 0,len = v.size();string str;while ( count < (len/8) ) 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; count+;return str;vector<int> DES:int2bit_(vector<int> vecInt)/*整形數(shù)0-15化為二進(jìn)制

30、*/vector<int> v;for (unsigned int i = 0; i < vecInt.size(); i+) int mask = 8; while (mask > 0) v.push_back( (mask&vecInti) > 0) ); mask = mask >> 1; return v;vector<int> DES:vecXor_(vector<int> vL, vector<int> vR)/*向量逐位異或*/assert(vL.size() = vR.size();for (

31、unsigned int i = 0; i < vL.size(); i+) vLi = vRi;return vL;vector<int> DES:leftIterMove_(vector<int> movVec,int loopStep)/*數(shù)據(jù)左移loopStep位*/unsigned int len = movVec.size();for (int i = 0; i < loopStep; i+) movVec.push_back(movVeci);for (unsigned int i = 0; i < len; i+) movVeci =

32、movVeci+loopStep;movVec.erase(movVec.end()-loopStep,movVec.end();return movVec;vector<int> DES:vecReplace_(vector<int> v,const unsigned int vArray)/*通用函數(shù):數(shù)據(jù)按照數(shù)組元素?fù)Q位*/vector<int> vTmp;for (unsigned int i = 0; i < v.size(); i+) vTmp.push_back(vvArrayi-1);return vTmp;vector<int&

33、gt; 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;vector<int> DES:f_(vector<int> vecR, vector<int&g

34、t; vecKey)/*f函數(shù),DES加密的核心*/assert( (vecR.size() = 32) && (vecKey.size() = 48) );vector<int> vecR_E,vecR_E_P;for (unsigned int i = 0; i < 48; i+) vecR_E.push_back(vecRE_i-1);vecR_E = S_(vecXor_(vecR_E,vecKey);vecR_E_P = vecReplace_(vecR_E,P_);return vecR_E_P;vector<int> DES:S_(v

35、ector<int> vecRKey)/*S函數(shù),6bit入4bit出*/assert(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_outij = vecRKey6*i+j; int a = 2 * S_outi0 + S_outi5; int b = 8 *

36、S_outi1 + 4 * S_outi2 + 2 * S_outi3 + S_outi4; tmp.push_back(SBOX_iab);return int2bit_(tmp);void DES:getsubkey_()/*函數(shù)功能:獲得子密鑰*/*讀取密鑰文件*/setKeyFilePath_();key_ = readData_(keyFilename_);vector<int> keyBit = string2bit_(key_);/*生成子密鑰*/*1 - keybit通過PC_1*/vector<int> keyBit_PC_1 = vecReplace_(keyBit,PC_1_);/*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 = keyB

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論