当前位置:   article > 正文

DES加密算法实现(加解密代码C++)_des算法代码

des算法代码

参考博客:加密算法------DES加密算法详解_柯南的博客-CSDN博客_des加密算法

一、算法流程

 二、数据定义

数据名称

数据类型

数据描述

ExchangeRules

Int[56]

交换规则表

ShiftTable

Int[16]

移位

PC_2

Int[48]

PC-2置换规则表

IP

Int[64]

IP置换规则表

E

Int[48]

E置换规则表

SBox

Int [8][4][16]

S盒置换规则表

P

Int[32]

P置换规则表

IP_1

Int[64]

IP_1置换规则表

三、方法说明 

方法名称

返回值类型

参数类型

方法描述

int2BinString

string

int

int转四位string  +  int十进制转string二进制

hexToTwo

string

string

string十六进制转string二进制

binToDec

int

string

string二进制转int十进制

str2Dec

int

string

01字符转十进制

Bin2Hex

string

string

64位密文转十六进制

exchange

string

String, int[], int

利用交换表进行置换

circleMove

string

String, int

依据移位表进行移位

spiltShift

string

String, int

左右两部分移位

XOR

string

String, string

string 异或

SBoxWork

string

String, int[][][]

S盒工作

四、效果呈现

五、代码实现(仅加密)

  1. #include <iostream>
  2. #include <bitset>
  3. #include <string>
  4. #include <cmath>
  5. #include<stdlib.h>
  6. using namespace std;
  7. /**
  8. *函数声明
  9. */
  10. string hexToTwo(string str); //十六进制转二进制
  11. string int2BinString(int n); //int转四位string
  12. string exchange(string str, int rule[], int x); //置换
  13. string circleMove(string str, int j); //单步移位
  14. string spiltShift(string str, int j); // 左右分别移位
  15. string XOR(string str1, string str2); //异或
  16. string SBoxWork(string str, int SBox[][4][16]); //S盒工作
  17. int binToDec(string bin); //二进制转十进制
  18. string Bin2Hex(string strBin); //二进制转十六进制
  19. int str2Dec(string str);
  20. /**
  21. *全局变量
  22. */
  23. const int Key_SIZE = 16;
  24. /**
  25. *8张表
  26. */
  27. //交换规则表(8*7)
  28. const int ExchangeRules_SIZE = 56;
  29. int ExchangeRules[56] = {
  30. 57, 49, 41, 33, 25, 17, 9,
  31. 1, 58, 50, 42, 34, 26, 18,
  32. 10, 2, 59, 51, 43, 35, 27,
  33. 19, 11, 3, 60, 52, 44, 36,
  34. 63, 55, 47, 39, 31, 23, 15,
  35. 7, 62, 54, 46, 38, 30, 22,
  36. 14, 6, 61, 53, 45, 37, 29,
  37. 21, 13, 5, 28, 20, 12, 4
  38. };
  39. //移位表
  40. const int ShiftTable_SIZE = 16;
  41. int ShiftTable[16] = {
  42. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  43. };
  44. //PC-2(8*6)
  45. const int PC_2_SIZE = 48;
  46. int PC_2[48] = {
  47. 14, 17, 11, 24, 1, 5,
  48. 3, 28, 15, 6, 21, 10,
  49. 23, 19, 12, 4, 26, 8,
  50. 16, 7, 27, 20, 13, 2,
  51. 41, 52, 31, 37, 47, 55,
  52. 30, 40, 51, 45, 33, 48,
  53. 44, 49, 39, 56, 34, 53,
  54. 46, 42, 50, 36, 29, 32
  55. };
  56. //IP(8*8)
  57. const int IP_SIZE = 64;
  58. int IP[64] = {
  59. 58, 50, 42, 34, 26, 18, 10, 2,
  60. 60, 52, 44, 36, 28, 20, 12, 4,
  61. 62, 54, 46, 38, 30, 22, 14, 6,
  62. 64, 56, 48, 40, 32, 24, 16, 8,
  63. 57, 49, 41, 33, 25, 17, 9, 1,
  64. 59, 51, 43, 35, 27, 19, 11, 3,
  65. 61, 53, 45, 37, 29, 21, 13, 5,
  66. 63, 55, 47, 39, 31, 23, 15, 7
  67. };
  68. //扩展置换E(8*6)
  69. const int E_SIZE = 48;
  70. int E[48] = {
  71. 32, 1, 2, 3, 4, 5,
  72. 4, 5, 6, 7, 8, 9,
  73. 8, 9, 10, 11, 12, 13,
  74. 12, 13, 14, 15, 16, 17,
  75. 16, 17, 18, 19, 20, 21,
  76. 20, 21, 22, 23, 24, 25,
  77. 24, 25, 26, 27, 28, 29,
  78. 28, 29, 30, 31, 32, 1
  79. };
  80. //S盒
  81. int SBox[8][4][16] = {
  82. {
  83. {14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
  84. {0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
  85. {4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
  86. {15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}
  87. },
  88. {
  89. {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
  90. {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
  91. {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
  92. {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}
  93. },
  94. {
  95. {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
  96. {13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
  97. {13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
  98. {1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}
  99. },
  100. {
  101. {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
  102. {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
  103. {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
  104. {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}
  105. },
  106. {
  107. {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
  108. {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
  109. {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
  110. {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}
  111. },
  112. {
  113. {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
  114. {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
  115. {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
  116. {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}
  117. },
  118. {
  119. {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
  120. {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
  121. {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
  122. {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}
  123. },
  124. {
  125. {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
  126. {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
  127. {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
  128. {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
  129. }
  130. };
  131. //P盒(8*4)
  132. const int P_SIZE = 32;
  133. int P[32] = {
  134. 16, 7, 20, 21,
  135. 29, 12, 28, 17,
  136. 1, 15, 23, 26,
  137. 5, 18, 31, 10,
  138. 2, 8, 24, 14,
  139. 32, 27, 3, 9,
  140. 19, 13, 30, 6,
  141. 22, 11, 4, 25
  142. };
  143. //IP-1(8*8)
  144. const int IP_1_SIZE = 64;
  145. int IP_1[64] = {
  146. 40, 8, 48, 16, 56, 24, 64, 32,
  147. 39, 7, 47, 15, 55, 23, 63, 31,
  148. 38, 6, 46, 14, 54, 22, 62, 30,
  149. 37, 5, 45, 13, 53, 21, 61, 29,
  150. 36, 4, 44, 12, 52, 20, 60, 28,
  151. 35, 3, 43, 11, 51, 19, 59, 27,
  152. 34, 2, 42, 10, 50, 18, 58, 26,
  153. 33, 1, 41, 9, 49, 17, 57, 25
  154. };
  155. int main()
  156. {
  157. /**
  158. *初始条件
  159. **/
  160. /* 输入明文MingWen(十六进制),密钥Key(十六进制) */
  161. string MingWen ;
  162. string Key;
  163. cout<<"Welcome to DES encryption system! "<<endl;
  164. cout<<"Please enter MingWen: "<<endl;
  165. cin>>MingWen;
  166. cout<<"Please enter your Key: "<<endl;
  167. cin>>Key;
  168. string M = hexToTwo(MingWen);
  169. string K = hexToTwo(Key);
  170. /**
  171. *处理密钥,生成16个子密钥 *
  172. */
  173. /* 利用规则交换表(8*7)将K转换成 K0
  174. K0(56位) = C0(28位) + D0(28位) */
  175. string KKK = exchange(K, ExchangeRules, ExchangeRules_SIZE);
  176. /* 利用移位表转换得C1D1----C16D16,存入K_arr */
  177. int i = 0;
  178. string K_arr[Key_SIZE+1];
  179. K_arr[0] = KKK;
  180. for(i=1; i<=Key_SIZE; i++){
  181. K_arr[i] = spiltShift(K_arr[i-1], ShiftTable[i-1]);
  182. }
  183. /* Kn(48位)= PC-2(8*6)处理 CnDn得16个子密钥,存入Key_arr */
  184. string Key_arr[Key_SIZE];
  185. for(i=0; i<Key_SIZE; i++){
  186. Key_arr[i] = exchange(K_arr[i+1], PC_2, PC_2_SIZE);
  187. }
  188. /**
  189. * 用子密钥对明文加密
  190. **/
  191. /* 通过IP(8*8)处理M得L0(32位)  R0(32位) */
  192. string IP_M = exchange(M, IP, IP_SIZE);
  193. /* Ln= R(n-1); Rn= L(n-1) + f(R(n- 1); Kn)迭代16次 */
  194. string L[Key_SIZE+1];
  195. string R[Key_SIZE+1];
  196. L[0] = IP_M.substr(0, M.length()/2);
  197. R[0] = IP_M.substr(M.length()/2);
  198. string it = "";
  199. for(i=1; i<=Key_SIZE; i++){
  200. //将R0通过扩展置换E(8*6)从32位扩展到48位
  201. it = exchange(R[i-1], E, E_SIZE);
  202. //R0(48位)与 K1异或得E0(48位)
  203. it = XOR(it, Key_arr[i-1]);
  204. //将E0(48位)通过S盒转换成32位
  205. it = SBoxWork(it, SBox);
  206. //P盒(8*4)置换,得P0
  207. it = exchange(it, P, P_SIZE);
  208. //P0与L0进行异或,得J0
  209. it = XOR(it, L[i-1]);
  210. //左右交换位置,即R1 = J0; L1 = R0
  211. L[i] = R[i-1];
  212. R[i] = it;
  213. }
  214. /* 对R16 L16进行一次IP-1(8*8)排序得密文 */
  215. string res = "";
  216. res += R[16];
  217. res += L[16];
  218. string finalRes = Bin2Hex(exchange(res, IP_1, IP_1_SIZE));
  219. cout<<"DES encryption results are as follows: "<<endl;
  220. cout<<finalRes<<endl;
  221. return 0;
  222. }
  223. /**
  224. *int转四位string + int十进制转string二进制
  225. **/
  226. string int2BinString(int n) {
  227. bitset<4> bit(n);
  228. return bit.to_string();
  229. }
  230. /**
  231. *string十六进制转string二进制
  232. **/
  233. string hexToTwo(string str){
  234. string twoBin = "";
  235. int i;
  236. for(i=0; i<16; i++){
  237. if(str[i]>='0'&&str[i]<='9')
  238. twoBin.append(int2BinString(str[i]));
  239. else if(str[i]>='A'&&str[i]<='Z')
  240. twoBin.append(int2BinString(str[i]-'A'+10));
  241. else if(str[i]>='a'&&str[i]<='z')
  242. twoBin.append(int2BinString(str[i]-'a'+10));
  243. }
  244. return twoBin;
  245. }
  246. /**
  247. * string二进制转int十进制
  248. **/
  249. int binToDec(string bin){
  250. int sum = 0;
  251. for(int i=0; i<bin.size(); i++){
  252. if(bin[i]=='0' || bin[i]=='1'){
  253. sum += (bin[i]-'0') * pow(2, bin.size()-i-1);
  254. }else{
  255. cout<<"非法二进制字符!"<<endl;
  256. return 0;
  257. }
  258. }
  259. return sum;
  260. }
  261. /**
  262. * 01字符转十进制
  263. **/
  264. int str2Dec(string str) {
  265. bitset<64> bst(str);
  266. return (int)bst.to_ulong();
  267. }
  268. /**
  269. * 64位密文转十六进制
  270. **/
  271. //Bin2Hex转换表
  272. const static string Bin_Hex[16] {
  273. "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
  274. };
  275. string Bin2Hex(string strBin) {
  276. string hex;
  277. int a = strBin.length()/4;
  278. string trans;
  279. for(int i = 0; i < a; i++) {
  280. trans.clear();
  281. trans = strBin.substr(i*4, 4);
  282. hex += Bin_Hex[str2Dec(trans)];
  283. }
  284. return hex;
  285. }
  286. /**
  287. *利用交换表进行置换
  288. **/
  289. string exchange(string str, int rule[], int x){
  290. string exchangedStr = "";
  291. int i, temp;
  292. for(i=0; i<x; i++){
  293. temp = rule[i]-1;
  294. exchangedStr.append(1, str[temp]);
  295. }
  296. return exchangedStr;
  297. }
  298. /**
  299. *依据移位表进行移位
  300. **/
  301. string circleMove(string str, int j){
  302. string targetString = "";
  303. targetString.append(str.substr(j));
  304. targetString.append(str.substr(0, j));
  305. return targetString;
  306. }
  307. /**
  308. *左右两部分移位
  309. **/
  310. string spiltShift(string str, int j){
  311. string targetStr = "";
  312. string leftString = str.substr(0, str.length()/2);
  313. string rightString = str.substr(str.length()/2);
  314. targetStr.append(circleMove(leftString, j));
  315. targetStr.append(circleMove(rightString, j));
  316. return targetStr;
  317. }
  318. /**
  319. * string 异或
  320. **/
  321. string XOR(string str1, string str2){
  322. string targetString = "";
  323. for(int j=0; j<str1.length(); j++){
  324. targetString += ((str1[j] - '0') ^ (str2[j] - '0')) + '0';
  325. }
  326. return targetString;
  327. }
  328. /**
  329. * S盒工作
  330. **/
  331. string SBoxWork(string str, int SBox[][4][16]){
  332. string targetString = "";
  333. string temp = "";
  334. string x = "", y = "";
  335. int col = 0, row = 0;
  336. for(int i=0; i<str.size()/6; i++){
  337. temp = str.substr(6*i, 6);
  338. x = temp.substr(0, 1)+temp.substr(5, 1);
  339. y = temp.substr(1, 4);
  340. row = binToDec(x);
  341. col = binToDec(y);
  342. targetString.append(int2BinString(SBox[i][row][col]));
  343. }
  344. return targetString;
  345. }

六、代码实现(加解密全)

  1. #include <iostream>
  2. #include <bitset>
  3. #include <string>
  4. #include <cmath>
  5. #include<stdlib.h>
  6. #include <cstdlib>
  7. using namespace std;
  8. /**
  9. *函数声明
  10. */
  11. string hexToTwo(string str); //十六进制转二进制
  12. string int2BinString(int n); //int转四位string
  13. string exchange(string str, int rule[], int x); //置换
  14. string circleMove(string str, int j); //单步移位
  15. string spiltShift(string str, int j); // 左右分别移位
  16. string XOR(string str1, string str2); //异或
  17. string SBoxWork(string str, int SBox[][4][16]); //S盒工作
  18. int binToDec(string bin); //二进制转十进制
  19. string Bin2Hex(string strBin); //二进制转十六进制
  20. int str2Dec(string str); // string字符串转十进制
  21. void printMenu(); //打印功能菜单
  22. void controller(); //功能控制器
  23. void encryption(); //加密
  24. void decryption(); //解密
  25. /**
  26. *全局变量
  27. */
  28. const int Key_SIZE = 16;
  29. /**
  30. *8张表
  31. */
  32. //交换规则表(8*7)
  33. const int ExchangeRules_SIZE = 56;
  34. int ExchangeRules[56] =
  35. {
  36. 57, 49, 41, 33, 25, 17, 9,
  37. 1, 58, 50, 42, 34, 26, 18,
  38. 10, 2, 59, 51, 43, 35, 27,
  39. 19, 11, 3, 60, 52, 44, 36,
  40. 63, 55, 47, 39, 31, 23, 15,
  41. 7, 62, 54, 46, 38, 30, 22,
  42. 14, 6, 61, 53, 45, 37, 29,
  43. 21, 13, 5, 28, 20, 12, 4
  44. };
  45. //移位表
  46. const int ShiftTable_SIZE = 16;
  47. int ShiftTable[16] =
  48. {
  49. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  50. };
  51. //PC-2(8*6)
  52. const int PC_2_SIZE = 48;
  53. int PC_2[48] =
  54. {
  55. 14, 17, 11, 24, 1, 5,
  56. 3, 28, 15, 6, 21, 10,
  57. 23, 19, 12, 4, 26, 8,
  58. 16, 7, 27, 20, 13, 2,
  59. 41, 52, 31, 37, 47, 55,
  60. 30, 40, 51, 45, 33, 48,
  61. 44, 49, 39, 56, 34, 53,
  62. 46, 42, 50, 36, 29, 32
  63. };
  64. //IP(8*8)
  65. const int IP_SIZE = 64;
  66. int IP[64] =
  67. {
  68. 58, 50, 42, 34, 26, 18, 10, 2,
  69. 60, 52, 44, 36, 28, 20, 12, 4,
  70. 62, 54, 46, 38, 30, 22, 14, 6,
  71. 64, 56, 48, 40, 32, 24, 16, 8,
  72. 57, 49, 41, 33, 25, 17, 9, 1,
  73. 59, 51, 43, 35, 27, 19, 11, 3,
  74. 61, 53, 45, 37, 29, 21, 13, 5,
  75. 63, 55, 47, 39, 31, 23, 15, 7
  76. };
  77. //扩展置换E(8*6)
  78. const int E_SIZE = 48;
  79. int E[48] =
  80. {
  81. 32, 1, 2, 3, 4, 5,
  82. 4, 5, 6, 7, 8, 9,
  83. 8, 9, 10, 11, 12, 13,
  84. 12, 13, 14, 15, 16, 17,
  85. 16, 17, 18, 19, 20, 21,
  86. 20, 21, 22, 23, 24, 25,
  87. 24, 25, 26, 27, 28, 29,
  88. 28, 29, 30, 31, 32, 1
  89. };
  90. //S盒
  91. int SBox[8][4][16] =
  92. {
  93. {
  94. {14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
  95. {0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
  96. {4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
  97. {15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}
  98. },
  99. {
  100. {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
  101. {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
  102. {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
  103. {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}
  104. },
  105. {
  106. {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
  107. {13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
  108. {13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
  109. {1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}
  110. },
  111. {
  112. {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
  113. {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
  114. {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
  115. {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}
  116. },
  117. {
  118. {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
  119. {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
  120. {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
  121. {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}
  122. },
  123. {
  124. {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
  125. {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
  126. {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
  127. {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}
  128. },
  129. {
  130. {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
  131. {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
  132. {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
  133. {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}
  134. },
  135. {
  136. {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
  137. {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
  138. {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
  139. {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
  140. }
  141. };
  142. //P盒(8*4)
  143. const int P_SIZE = 32;
  144. int P[32] =
  145. {
  146. 16, 7, 20, 21,
  147. 29, 12, 28, 17,
  148. 1, 15, 23, 26,
  149. 5, 18, 31, 10,
  150. 2, 8, 24, 14,
  151. 32, 27, 3, 9,
  152. 19, 13, 30, 6,
  153. 22, 11, 4, 25
  154. };
  155. //IP-1(8*8)
  156. const int IP_1_SIZE = 64;
  157. int IP_1[64] =
  158. {
  159. 40, 8, 48, 16, 56, 24, 64, 32,
  160. 39, 7, 47, 15, 55, 23, 63, 31,
  161. 38, 6, 46, 14, 54, 22, 62, 30,
  162. 37, 5, 45, 13, 53, 21, 61, 29,
  163. 36, 4, 44, 12, 52, 20, 60, 28,
  164. 35, 3, 43, 11, 51, 19, 59, 27,
  165. 34, 2, 42, 10, 50, 18, 58, 26,
  166. 33, 1, 41, 9, 49, 17, 57, 25
  167. };
  168. int main()
  169. {
  170. printMenu();
  171. return 0;
  172. }
  173. /**
  174. *打印功能菜单
  175. **/
  176. void printMenu()
  177. {
  178. cout<<"Welcome to DES encryption system! "<<endl;
  179. cout<<"The following are the functions of the system: "<<endl;
  180. cout<<"1. encryption"<<endl;
  181. cout<<"2. decryption"<<endl;
  182. cout<<"3. exit"<<endl;
  183. controller();
  184. }
  185. /**
  186. *功能控制器
  187. **/
  188. void controller()
  189. {
  190. cout<<"Please select the action you want to take: "<<endl;
  191. int choice;
  192. cin>>choice;
  193. if(choice == 1) encryption();
  194. else if(choice == 2) decryption();
  195. else if(choice == 3) {
  196. cout<<"Looking forward to your use again!"<<endl;
  197. cout<<"Bye Bye!"<<endl;
  198. exit(0);
  199. }
  200. else
  201. {
  202. cout<<"Error! This option is not recognized. Please reselect it!"<<endl;
  203. controller();
  204. }
  205. }
  206. /**
  207. *加密
  208. **/
  209. void encryption()
  210. {
  211. /**
  212. *初始条件
  213. **/
  214. /* 输入明文MingWen(十六进制),密钥Key(十六进制) */
  215. string MingWen ;
  216. string Key;
  217. cout<<"Please enter MingWen: "<<endl;
  218. cin>>MingWen;
  219. cout<<"Please enter your Key: "<<endl;
  220. cin>>Key;
  221. string M = hexToTwo(MingWen);
  222. string K = hexToTwo(Key);
  223. /**
  224. *处理密钥,生成16个子密钥 *
  225. */
  226. /* 利用规则交换表(8*7)将K转换成 K0 ; K0(56位) = C0(28位) + D0(28位) */
  227. string KKK = exchange(K, ExchangeRules, ExchangeRules_SIZE);
  228. /* 利用移位表转换得C1D1----C16D16,存入K_arr */
  229. int i = 0;
  230. string K_arr[Key_SIZE+1];
  231. K_arr[0] = KKK;
  232. for(i=1; i<=Key_SIZE; i++)
  233. {
  234. K_arr[i] = spiltShift(K_arr[i-1], ShiftTable[i-1]);
  235. }
  236. /* Kn(48位)= PC-2(8*6)处理 CnDn得16个子密钥,存入Key_arr */
  237. string Key_arr[Key_SIZE];
  238. for(i=0; i<Key_SIZE; i++)
  239. {
  240. Key_arr[i] = exchange(K_arr[i+1], PC_2, PC_2_SIZE);
  241. }
  242. /**
  243. * 用子密钥对明文加密
  244. **/
  245. /* 通过IP(8*8)处理M得L0(32位) R0(32位) */
  246. string IP_M = exchange(M, IP, IP_SIZE);
  247. /* Ln= R(n-1); Rn= L(n-1) + f(R(n- 1); Kn)迭代16次 */
  248. string L[Key_SIZE+1];
  249. string R[Key_SIZE+1];
  250. L[0] = IP_M.substr(0, M.length()/2);
  251. R[0] = IP_M.substr(M.length()/2);
  252. string it = "";
  253. for(i=1; i<=Key_SIZE; i++)
  254. {
  255. //将R0通过扩展置换E(8*6)从32位扩展到48位
  256. it = exchange(R[i-1], E, E_SIZE);
  257. //R0(48位)与 K1异或得E0(48位)
  258. it = XOR(it, Key_arr[i-1]);
  259. //将E0(48位)通过S盒转换成32位
  260. it = SBoxWork(it, SBox);
  261. //P盒(8*4)置换,得P0
  262. it = exchange(it, P, P_SIZE);
  263. //P0与L0进行异或,得J0
  264. it = XOR(it, L[i-1]);
  265. //左右交换位置,即R1 = J0; L1 = R0
  266. L[i] = R[i-1];
  267. R[i] = it;
  268. }
  269. /* 对R16 L16进行一次IP-1(8*8)排序得密文 */
  270. string res = "";
  271. res += R[16];
  272. res += L[16];
  273. string finalRes = Bin2Hex(exchange(res, IP_1, IP_1_SIZE));
  274. cout<<"DES encryption results are as follows: "<<endl;
  275. cout<<finalRes<<endl;
  276. cout<<"------------------------"<<endl;
  277. printMenu();
  278. }
  279. /**
  280. *解密
  281. **/
  282. void decryption()
  283. {
  284. string MiWen;
  285. string Key;
  286. cout<<"Please enter MiWen: "<<endl;
  287. cin>>MiWen;
  288. cout<<"Please enter your Key: "<<endl;
  289. cin>>Key;
  290. string M = hexToTwo(MiWen);
  291. string K = hexToTwo(Key);
  292. /**
  293. *处理密钥,生成16个子密钥 *
  294. */
  295. /* 利用规则交换表(8*7)将K转换成 K0 ; K0(56位) = C0(28位) + D0(28位) */
  296. string KKK = exchange(K, ExchangeRules, ExchangeRules_SIZE);
  297. /* 利用移位表转换得C1D1----C16D16,存入K_arr */
  298. int i = 0;
  299. string K_arr[Key_SIZE+1];
  300. K_arr[0] = KKK;
  301. for(i=1; i<=Key_SIZE; i++)
  302. {
  303. K_arr[i] = spiltShift(K_arr[i-1], ShiftTable[i-1]);
  304. }
  305. /* Kn(48位)= PC-2(8*6)处理 CnDn得16个子密钥,存入Key_arr */
  306. string Key_arr[Key_SIZE];
  307. for(i=0; i<Key_SIZE; i++)
  308. {
  309. Key_arr[i] = exchange(K_arr[i+1], PC_2, PC_2_SIZE);
  310. }
  311. /**
  312. * 用子密钥对明文加密
  313. **/
  314. /* 通过IP(8*8)处理M得L0(32位) R0(32位) */
  315. string IP_M = exchange(M, IP, IP_SIZE);
  316. /* Ln= R(n-1); Rn= L(n-1) + f(R(n- 1); Kn)迭代16次 */
  317. string L[Key_SIZE+1];
  318. string R[Key_SIZE+1];
  319. L[0] = IP_M.substr(0, M.length()/2);
  320. R[0] = IP_M.substr(M.length()/2);
  321. string it = "";
  322. for(i=1; i<=Key_SIZE; i++)
  323. {
  324. //将R0通过扩展置换E(8*6)从32位扩展到48位
  325. it = exchange(R[i-1], E, E_SIZE);
  326. //R0(48位)与 K1异或得E0(48位)
  327. it = XOR(it, Key_arr[16-i]);
  328. //将E0(48位)通过S盒转换成32位
  329. it = SBoxWork(it, SBox);
  330. //P盒(8*4)置换,得P0
  331. it = exchange(it, P, P_SIZE);
  332. //P0与L0进行异或,得J0
  333. it = XOR(it, L[i-1]);
  334. //左右交换位置,即R1 = J0; L1 = R0
  335. L[i] = R[i-1];
  336. R[i] = it;
  337. }
  338. /* 对R16 L16进行一次IP-1(8*8)排序得密文 */
  339. string res = "";
  340. res += R[16];
  341. res += L[16];
  342. string finalRes = Bin2Hex(exchange(res, IP_1, IP_1_SIZE));
  343. cout<<"DES decryption results are as follows: "<<endl;
  344. cout<<finalRes<<endl;
  345. cout<<"------------------------"<<endl;
  346. printMenu();
  347. }
  348. /**
  349. *int转四位string + int十进制转string二进制
  350. **/
  351. string int2BinString(int n)
  352. {
  353. bitset<4> bit(n);
  354. return bit.to_string();
  355. }
  356. /**
  357. *string十六进制转string二进制
  358. **/
  359. string hexToTwo(string str)
  360. {
  361. string twoBin = "";
  362. int i;
  363. for(i=0; i<16; i++)
  364. {
  365. if(str[i]>='0'&&str[i]<='9')
  366. twoBin.append(int2BinString(str[i]));
  367. else if(str[i]>='A'&&str[i]<='Z')
  368. twoBin.append(int2BinString(str[i]-'A'+10));
  369. else if(str[i]>='a'&&str[i]<='z')
  370. twoBin.append(int2BinString(str[i]-'a'+10));
  371. }
  372. return twoBin;
  373. }
  374. /**
  375. * string二进制转int十进制
  376. **/
  377. int binToDec(string bin)
  378. {
  379. int sum = 0;
  380. for(int i=0; i<bin.size(); i++)
  381. {
  382. if(bin[i]=='0' || bin[i]=='1')
  383. {
  384. sum += (bin[i]-'0') * pow(2, bin.size()-i-1);
  385. }
  386. else
  387. {
  388. cout<<"非法二进制字符!"<<endl;
  389. return 0;
  390. }
  391. }
  392. return sum;
  393. }
  394. /**
  395. * 01字符转十进制
  396. **/
  397. int str2Dec(string str)
  398. {
  399. bitset<64> bst(str);
  400. return (int)bst.to_ulong();
  401. }
  402. /**
  403. * 64位密文转十六进制
  404. **/
  405. //Bin2Hex转换表
  406. const static string Bin_Hex[16]
  407. {
  408. "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"
  409. };
  410. string Bin2Hex(string strBin)
  411. {
  412. string hex;
  413. int a = strBin.length()/4;
  414. string trans;
  415. for(int i = 0; i < a; i++)
  416. {
  417. trans.clear();
  418. trans = strBin.substr(i*4, 4);
  419. hex += Bin_Hex[str2Dec(trans)];
  420. }
  421. return hex;
  422. }
  423. /**
  424. *利用交换表进行置换
  425. **/
  426. string exchange(string str, int rule[], int x)
  427. {
  428. string exchangedStr = "";
  429. int i, temp;
  430. for(i=0; i<x; i++)
  431. {
  432. temp = rule[i]-1;
  433. exchangedStr.append(1, str[temp]);
  434. }
  435. return exchangedStr;
  436. }
  437. /**
  438. *依据移位表进行移位
  439. **/
  440. string circleMove(string str, int j)
  441. {
  442. string targetString = "";
  443. targetString.append(str.substr(j));
  444. targetString.append(str.substr(0, j));
  445. return targetString;
  446. }
  447. /**
  448. *左右两部分移位
  449. **/
  450. string spiltShift(string str, int j)
  451. {
  452. string targetStr = "";
  453. string leftString = str.substr(0, str.length()/2);
  454. string rightString = str.substr(str.length()/2);
  455. targetStr.append(circleMove(leftString, j));
  456. targetStr.append(circleMove(rightString, j));
  457. return targetStr;
  458. }
  459. /**
  460. * string 异或
  461. **/
  462. string XOR(string str1, string str2)
  463. {
  464. string targetString = "";
  465. for(int j=0; j<str1.length(); j++)
  466. {
  467. targetString += ((str1[j] - '0') ^ (str2[j] - '0')) + '0';
  468. }
  469. return targetString;
  470. }
  471. /**
  472. * S盒工作
  473. **/
  474. string SBoxWork(string str, int SBox[][4][16])
  475. {
  476. string targetString = "";
  477. string temp = "";
  478. string x = "", y = "";
  479. int col = 0, row = 0;
  480. for(int i=0; i<str.size()/6; i++)
  481. {
  482. temp = str.substr(6*i, 6);
  483. x = temp.substr(0, 1)+temp.substr(5, 1);
  484. y = temp.substr(1, 4);
  485. row = binToDec(x);
  486. col = binToDec(y);
  487. targetString.append(int2BinString(SBox[i][row][col]));
  488. }
  489. return targetString;
  490. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/881794
推荐阅读
相关标签
  

闽ICP备14008679号