标签:
程序分三部分,des头文件,des类实现,main函数调用。
1 //panda 2 //2013-4-13 3 //des 4 5 //des.h 6 7 class DES 8 { 9 private: 10 //public: 11 //明文 12 char msg[8]; 13 bool bmsg[64]; 14 //密钥 15 char key[8]; 16 bool bkey[64]; 17 //16个子密钥 18 bool subkey[16][48]; 19 //l0 r0中间变量 20 bool rmsgi[32],lmsgi[32];//第i个 21 bool rmsgi1[32],lmsgi1[32];//第i+1个 22 //密文 23 bool bcryptedmsg[64]; 24 char cryptedmsg[8]; 25 //解密的结果 26 bool bdecipher[64]; 27 char decipher[8]; 28 private: 29 //静态常量 30 31 //不允许在类内初始化 32 //初始值换ip 33 const static int ip[64]; 34 //子密钥 35 //置换选择1 36 const static int c0[28]; 37 const static int d0[28]; 38 //循环左移表 39 const static int keyoff[16]; 40 //置换选择2 41 const static int di[48]; 42 //加密函数 43 //e运算 44 const static int e_operate[48]; 45 //sbox 46 const static int sbox[8][64]; 47 //置换运算p 48 const static int p_operate[32]; 49 //逆初始置换ip 50 const static int back_ip[64]; 51 //位掩码 52 const static char bitmask[8]; 53 public: 54 //设置明文和密钥 55 //_length要小于或等于8 56 void SetMsg(char* _msg,int _length); 57 void SetKey(char* _msg,int _length); 58 //生产子密钥 59 void ProduceSubKey(); 60 //总的的加密流程 61 void Crypte(); 62 //解密 63 void Decipher(); 64 //输出密文 65 void OutPutCryptedMsg(); 66 //二进制转成字符 67 void Bit2Char(bool* _barray,char* _carray);//length=64 68 //输出解密后的明文 69 void OutPutDecipher(); 70 private: 71 //字符转成二进制,并保存到64位bool数组中 72 void Char2Bit(char* _carray,bool* _barray,int length); 73 ////二进制转成字符 74 //void Bit2Char(bool* _barray,char* _carray);//length=64 75 //初始置换 76 void InitSwap(bool in[64]); 77 //初始逆置换 78 void InitReSwap(bool out[64]); 79 //循环左移 80 void SubKeyOff(bool* _subkey,int _off); 81 //e运算操作函数 82 void EOperation(bool a[32],bool b[48]); 83 //模2相加 84 //相同为0 不同为1 85 void Mode2Add(bool a[],bool b[],bool c[],int length); 86 //sbox 87 void DealSBox(bool in[48],bool out[32]); 88 void _DealSBox(bool in[6],bool out[4],int box); 89 //p opraration 90 void POperation(bool temp[32],bool result[32]); 91 //加密函数 92 void CrypteFunction(bool in[32],int isubkey,bool out[32]); 93 94 //数组之间赋值 95 void CopyArray(bool array1[],bool array2[],int size); 96 }; 97 98 99 100 //2013-4-13 101 //panda 102 //des 103 #include<string> 104 #include<iostream> 105 #include"des.h" 106 using namespace std; 107 //静态常量 108 const int DES::ip[64]={ 109 58,50,42,34,26,18,10,2, 110 60,52,44,36,28,20,12,4, 111 62,54,46,38,30,22,14,6, 112 64,56,48,40,32,24,16,8, 113 57,49,41,33,25,17,9,1, 114 59,51,43,35,27,19,11,3, 115 61,53,45,37,29,21,13,5, 116 63,55,47,39,31,23,15,7 117 }; 118 const int DES::c0[28]={ 119 57,49,41,33,25,17,9, 120 1,58,50,42,34,26,18, 121 10,2,59,51,43,35,27, 122 19,11,3,60,52,44,36 123 }; 124 const int DES::d0[28]={ 125 63,55,47,39,31,23,15, 126 7,62,54,46,38,30,22, 127 14,6,61,53,45,37,29, 128 21,13,5,28,20,12,4 129 }; 130 const int DES::keyoff[16]={ 131 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 132 }; 133 const int DES::di[48]={ 134 14,17,11,24,1,5, 135 3,28,15,6,21,10, 136 23,19,12,4,26,8, 137 16,7,27,20,13,2, 138 41,52,31,37,47,55, 139 30,40,51,45,33,48, 140 44,49,39,56,34,53, 141 46,42,50,36,29,32 142 }; 143 const int DES::e_operate[48]={ 144 32,1,2,3,4,5, 145 4,5,6,7,8,9, 146 8,9,10,11,12,13, 147 12,13,14,15,16,17, 148 16,17,18,19,20,21, 149 20,21,22,23,24,25, 150 24,25,26,27,28,29, 151 28,29,30,31,32,1 152 }; 153 const int DES::sbox[8][64]={ 154 { 155 14, 4, 13, 1, 1, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 156 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 157 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 158 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 159 }, 160 161 { 162 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 163 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 164 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 165 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 166 }, 167 168 { 169 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 170 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 171 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 172 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 173 }, 174 175 { 176 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 177 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 178 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 179 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 180 }, 181 182 { 183 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 184 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 185 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 186 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 187 }, 188 189 { 190 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 191 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 192 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 193 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 194 }, 195 196 { 197 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 198 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 199 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 200 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 201 }, 202 203 { 204 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 13, 14, 5, 0, 12, 7, 205 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 206 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 207 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 208 } 209 }; 210 const int DES::p_operate[32]={ 211 16,7,20,21, 212 29,12,28,17, 213 1,15,23,26, 214 5,18,31,10, 215 2,8,24,14, 216 32,27,3,9, 217 19,13,30,6, 218 22,11,4,25 219 }; 220 const int DES::back_ip[64]={ 221 40,8,48,16,56,24,64,32, 222 39,7,47,15,55,23,63,31, 223 38,6,46,14,54,22,62,30, 224 37,5,45,13,53,21,61,29, 225 36,4,44,12,52,20,60,28, 226 35,3,43,11,51,19,59,27, 227 34,2,42,10,50,18,58,26, 228 33,1,41,9,49,17,57,25 229 }; 230 const char DES::bitmask[8]={ 128,64,32,16,8,4,2,1}; 231 232 //实现函数 233 234 // 235 //设置明文 236 // 237 void DES::SetMsg(char* _msg,int _length) 238 { 239 if (_length>8) 240 { 241 return; 242 } 243 for (int i = 0; i < _length; i++) 244 { 245 msg[i]=_msg[i]; 246 } 247 //转换成二进制 248 Char2Bit(msg,bmsg,8); 249 250 }; 251 // 252 //设置密钥 253 // 254 void DES::SetKey(char* _key,int _length) 255 { 256 if (_length>8) 257 { 258 return; 259 } 260 for (int i = 0; i < _length; i++) 261 { 262 key[i]=_key[i]; 263 } 264 //转成二进制 265 Char2Bit(key,bkey,8); 266 }; 267 // 268 //字符转成二进制 269 //ok length字符数组的长度 270 void DES::Char2Bit(char* _carray,bool* _barray,int length) 271 { 272 //int index=0; 273 for (int i = 0; i <length; i++) 274 { 275 for (int j = 0; j < 8; j++) 276 { 277 _barray[i*8+7-j]=(_carray[i]>>j)&1; 278 } 279 } 280 }; 281 // 282 //二进制转成字符 283 // 284 void DES::Bit2Char(bool* _barray,char* _carray) 285 { 286 char temp; 287 for (int i = 0; i < 8; i++) 288 { 289 //数学方法转成字符 290 temp=0; 291 for (int j = 0; j < 8; j++) 292 { 293 if (_barray[i*8+j]==1) 294 { 295 temp|=bitmask[j]; 296 } 297 } 298 //cout<<temp; 299 _carray[i]=temp; 300 } 301 }; 302 // 303 //初始置换函数 304 //ok 305 void DES::InitSwap(bool in[64]) 306 { 307 //打乱 308 for (int i = 0; i < 32; i++) 309 { 310 lmsgi[i]=in[ip[i]-1]; 311 rmsgi[i]=in[ip[i+32]-1]; 312 } 313 }; 314 // 315 //初始逆置换函数 316 //ok 317 void DES::InitReSwap(bool out[64]) 318 { 319 //组合成64数组 320 bool temp[64]; 321 for (int i = 0; i < 32; i++) 322 { 323 temp[i]=rmsgi[i]; 324 temp[32+i]=lmsgi[i]; 325 } 326 //按照逆ip矩阵 327 for (int i = 0; i < 64; i++) 328 { 329 out[i]=temp[back_ip[i]-1]; 330 } 331 }; 332 // 333 //循环左移 334 //ok 335 void DES::SubKeyOff(bool* _subkey,int _off) 336 { 337 //有没有更好的办法??? 338 bool temp; 339 for (int i = 0; i < _off; i++) 340 { 341 temp=_subkey[0]; 342 for (int i = 0; i < 27; i++) 343 { 344 _subkey[i]=_subkey[i+1]; 345 } 346 _subkey[27]=temp; 347 } 348 }; 349 // 350 //生产子密钥 351 //ok 352 void DES::ProduceSubKey() 353 { 354 //置换选择1 355 bool ctemp[28],dtemp[28]; 356 for (int i = 0; i < 28; i++) 357 { 358 ctemp[i]=bkey[c0[i]-1]; 359 dtemp[i]=bkey[d0[i]-1]; 360 } 361 bool keytemp[56]; 362 for (int i = 0; i < 16; i++) 363 { 364 //循环左移 365 SubKeyOff(ctemp,keyoff[i]); 366 SubKeyOff(dtemp,keyoff[i]); 367 //合并成一个56数组 368 for (int j = 0; j <28; j++) 369 { 370 keytemp[j]=ctemp[j]; 371 keytemp[28+j]=dtemp[j]; 372 } 373 //置换选择2 374 for (int j = 0; j < 48; j++) 375 { 376 subkey[i][j]=keytemp[di[j]-1]; 377 } 378 } 379 }; 380 // 381 //e运算 382 //ok 383 void DES::EOperation(bool a[32],bool b[48]) 384 { 385 for (int i = 0; i < 48; i++) 386 { 387 b[i]=a[e_operate[i]-1]; 388 } 389 }; 390 // 391 //模2想加 392 //ok 393 void DES::Mode2Add(bool a[],bool b[],bool c[],int length) 394 { 395 for (int i = 0; i < length; i++) 396 { 397 if (a[i]==b[i]) 398 { 399 c[i]=0; 400 }else 401 { 402 c[i]=1; 403 } 404 } 405 }; 406 // 407 //sbox处理 408 //ok 409 void DES::DealSBox(bool in[48],bool out[32]) 410 { 411 bool _in[6],_out[4]; 412 //8个盒子 413 for (int i = 0; i < 8; i++) 414 { 415 //提取盒子 416 for (int j = 0; j < 6; j++) 417 { 418 _in[j]=in[i*6+j]; 419 } 420 //压缩 421 _DealSBox(_in,_out,i); 422 //放进out数组 423 for (int jj = 0; jj < 4; jj++) 424 { 425 out[i*4+jj]=_out[jj]; 426 } 427 } 428 }; 429 // 430 //_dealsbox 431 //ok 432 void DES::_DealSBox(bool in[6],bool out[4],int box) 433 { 434 int raw,col; 435 raw=in[0]*2+in[5];//转换成十进制 行 436 col=in[1]*2*2*2+in[2]*2*2+in[3]*2+in[4];//列 437 int result=sbox[box][raw*16+col]; 438 //转成二进制 439 for (int i = 3; i >=0; i--) 440 { 441 out[i]=(result>>(3-i))&1; 442 } 443 }; 444 // 445 //p操作 446 //ok 447 void DES::POperation(bool temp[32],bool result[32]) 448 { 449 for (int i = 0; i < 32; i++) 450 { 451 result[i]=temp[p_operate[i]-1]; 452 } 453 }; 454 // 455 //加密函数 456 //isubkey表明用那个子密钥加密 ok 457 void DES::CrypteFunction(bool in[32],int isubkey,bool out[32]) 458 { 459 //e 操作 460 bool temp1[48]; 461 EOperation(in,temp1); 462 bool temp2[48]; 463 Mode2Add(temp1,(bool *)subkey[isubkey],temp2,48);//ok 464 //盒子压缩 465 bool temp3[48]; 466 DealSBox(temp2,temp3); 467 //置换运算p 468 POperation(temp3,out); 469 470 }; 471 // 472 // des加密流程 473 //ok 474 void DES::Crypte() 475 { 476 //直接用bmsg明文 477 //直接用cryptedmsg存放密文 478 bool temp1[32],temp2[32]; 479 //初始置换ip 480 InitSwap(bmsg); 481 //16轮迭代 482 for (int i = 0; i < 16; i++) 483 { 484 if (i%2==0) 485 { 486 //L1=R0 487 CopyArray(rmsgi,lmsgi1,32); 488 //f(R0,k0) 489 CrypteFunction(rmsgi,i,temp1); 490 //L0+f(R0,k0) 491 Mode2Add(lmsgi,temp1,temp2,32); 492 //R1=L0+f(R0,k0) 493 CopyArray(temp2,rmsgi1,32); 494 }else 495 { 496 //L2=R1 497 CopyArray(rmsgi1,lmsgi,32); 498 //f(R1,k1) 499 CrypteFunction(rmsgi1,i,temp1); 500 //L1+f(R1,k1) 501 Mode2Add(lmsgi1,temp1,temp2,32); 502 //R2=L1+f(R1,k1) 503 CopyArray(temp2,rmsgi,32); 504 } 505 } 506 507 //逆初始置换ip 508 InitReSwap(bcryptedmsg); 509 //转成字符 510 Bit2Char(bcryptedmsg,cryptedmsg); 511 }; 512 // 513 //数组赋值 514 //ok 515 void DES::CopyArray(bool content[],bool empty[],int size) 516 { 517 for (int i = 0; i < size; i++) 518 { 519 empty[i]=content[i]; 520 } 521 }; 522 // 523 //解密 524 //ok 525 void DES::Decipher() 526 { 527 bool temp1[32],temp2[32]; 528 //初始置换ip 529 InitSwap(bcryptedmsg); 530 //16轮迭代加密 531 532 for (int i = 0; i < 16; i++) 533 { 534 if (i%2==0) 535 { 536 //L1=R0 537 CopyArray(rmsgi,lmsgi1,32); 538 //f(R0,k0) 539 CrypteFunction(rmsgi,15-i,temp1); 540 //L0+f(R0,k0) 541 Mode2Add(lmsgi,temp1,temp2,32); 542 //R1=L0+f(R0,k0) 543 CopyArray(temp2,rmsgi1,32); 544 }else 545 { 546 //L2=R1 547 CopyArray(rmsgi1,lmsgi,32); 548 //f(R1,k1) 549 CrypteFunction(rmsgi1,15-i,temp1); 550 //L1+f(R1,k1) 551 Mode2Add(lmsgi1,temp1,temp2,32); 552 //R2=L1+f(R1,k1) 553 CopyArray(temp2,rmsgi,32); 554 } 555 } 556 //逆初始置换ip 557 InitReSwap(bdecipher); 558 //转成字符 559 Bit2Char(bdecipher,decipher); 560 561 }; 562 // 563 //输出密文 564 // 565 void DES::OutPutCryptedMsg() 566 { 567 //Bit2Char(bcryptedmsg,cryptedmsg); 568 cout<<endl<<"密文:"; 569 for (int i = 0; i < 8; i++) 570 { 571 cout<<cryptedmsg[i]<<‘ ‘; 572 } 573 }; 574 // 575 //输出解密明文 576 // 577 void DES::OutPutDecipher() 578 { 579 //Bit2Char(bdecipher,decipher); 580 cout<<endl<<"解密:"; 581 for (int i = 0; i < 8; i++) 582 { 583 cout<<decipher[i]<<‘ ‘; 584 } 585 cout<<endl; 586 }; 587 588 589 #include<iostream> 590 #include"des.h" 591 #include<string.h> 592 using namespace std; 593 594 int main() 595 { 596 //教材的测试数据 597 char msg[8]={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘}; 598 char key[8]={‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘}; 599 cout<<"明文:"; 600 for (int i = 0; i < 8; i++) 601 { 602 cout<<msg[i]<<‘ ‘; 603 } 604 DES des; 605 //设置明文 606 des.SetMsg(msg,8); 607 //设置密钥 608 des.SetKey(key,8); 609 //生产子密钥 610 des.ProduceSubKey(); 611 //加密 612 des.Crypte(); 613 //输出密文 614 des.OutPutCryptedMsg(); 615 //解密 616 des.Decipher(); 617 //输出解密后的明文 618 des.OutPutDecipher(); 619 620 system("pause"); 621 return 0; 622 }
标签:
原文地址:http://www.cnblogs.com/pandang/p/4849154.html