DES加密解密算法C++實現


 

 

 DES加密算法並不難,是由一些簡單的變換得來的,難的是要有足夠的耐心。蒟蒻並不想說自己用了多久才把代碼寫好的。

 

 

 

 

 

 

 

代碼:

         我真的太難了QAQ

  1 #include<iostream>
  2 using namespace std;
  3  
  4         
  5  
  6 int jiami(){
  7     
  8     // 明文 
  9     int mingwen[64];
 10            
 11            
 12     cout<<"請輸入16位十六進制的明文:";
 13     string  kk;
 14     cin>>kk;
 15     int len=kk.length();
 16  
 17     
 18     while(len!=16){
 19         cout<<"請重新輸入16位十六進制的明文:";
 20         cin>>kk; 
 21         len=kk.length();
 22     }
 23     
 24     int jishu=0;
 25     for(int i=0;i<16;i++){
 26         int a;
 27         if(kk[i]>='0'&&kk[i]<='9')
 28            a=kk[i]-'0';
 29         else
 30            a=kk[i]-'A'+10;
 31            
 32         int n[4]={0};
 33         int f=0;
 34         while(a){
 35             n[f]=a%2;
 36             a=a/2;
 37             f++;
 38         }
 39         mingwen[jishu*4]=n[3];
 40         mingwen[jishu*4+1]=n[2];
 41         mingwen[jishu*4+2]=n[1];
 42         mingwen[jishu*4+3]=n[0];
 43         jishu++; 
 44     } 
 45  
 46    
 47  
 48     //初始置換IP 
 49     int IP[64]={58, 50, 42, 34, 26, 18, 10, 2,
 50                 60, 52, 44, 36, 28, 20, 12, 4,
 51                 62, 54, 46, 38, 30, 22, 14, 6,
 52                 64, 56, 48, 40, 32, 24, 16, 8,
 53                 57, 49, 41, 33, 25, 17,  9, 1,
 54                 59, 51, 43, 35, 27, 19, 11, 3,
 55                 61, 53, 45, 37, 29, 21, 13, 5,
 56                 63, 55, 47, 39, 31, 23, 15, 7};
 57                 
 58                 
 59                 
 60             
 61                 
 62     // 選擇運算E   32位明文擴充為48位                
 63     int E[48]={ 32,  1,  2,  3,  4,  5,
 64                 4,  5,  6,  7,  8,  9,
 65                 8,  9, 10, 11, 12, 13,
 66                12, 13, 14, 15, 16, 17,
 67                16, 17, 18, 19, 20, 21,
 68                20, 21, 22, 23, 24, 25,
 69                24, 25, 26, 27, 28, 29,
 70                28, 29, 30, 31, 32,  1 }; 
 71                       
 72               
 73     //64位秘鑰     0123456789ABCDEF    56位的秘鑰+8位校驗碼 
 74     int miyao[64];
 75     cout<<"請輸入16位十六進制的秘鑰:";
 76     string  k_2;
 77     cin>>k_2;
 78     int len_2=k_2.length();
 79  
 80     
 81     while(len_2!=16){
 82         cout<<"請重新輸入16位十六進制的秘鑰:";
 83         cin>>k_2; 
 84         len_2=k_2.length();
 85     }
 86     
 87     int jishu_2=0;
 88     for(int i=0;i<16;i++){
 89         int a;
 90         if(k_2[i]>='0'&&k_2[i]<='9')
 91            a=k_2[i]-'0';
 92         else
 93            a=k_2[i]-'A'+10;
 94            
 95         int n[4]={0};
 96         int f=0;
 97         while(a){
 98             n[f]=a%2;
 99             a=a/2;
100             f++;
101         }
102         miyao[jishu_2*4]=n[3];
103         miyao[jishu_2*4+1]=n[2];
104         miyao[jishu_2*4+2]=n[1];
105         miyao[jishu_2*4+3]=n[0];
106         jishu_2++; 
107     }      
108            
109            
110     //置換選擇1
111     int IP_1[56]={57, 49, 41, 33, 25, 17,  9,
112                    1, 58, 50, 42, 34, 26, 18,
113                   10,  2, 59, 51, 43, 35, 27,
114                   19, 11,  3, 60, 52, 44, 36,
115                   63, 55, 47, 39, 31, 23, 15,
116                    7, 62, 54, 46, 38, 30, 22,
117                   14,  6, 61, 53, 45, 37, 29,
118                   21, 13,  5, 28, 20, 12,  4};
119      
120                 
121  
122     //16次左移對應的位數                 
123     int weiyi[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};     
124     // 置換選擇2 秘鑰56->48位壓縮 
125     int IP_2[48]={14, 17, 11, 24,  1,  5,
126                    3, 28, 15,  6, 21, 10,
127                   23, 19, 12,  4, 26,  8,
128                   16,  7, 27, 20, 13,  2,
129                   41, 52, 31, 37, 47, 55,
130                   30, 40, 51, 45, 33, 48,
131                   44, 49, 39, 56, 34, 53,
132                   46, 42, 50, 36, 29, 32};
133                                 
134      //S盒
135                 
136     int s[8][65]=
137 {
138     {
139         14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
140         0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
141         4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
142         15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
143     },
144     {
145         15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
146         3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
147         0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
148         13,8,10,1,3,15,4,2,11,6,7,12,10,5,14,9
149     },
150     {
151         10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
152         13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
153         13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
154         1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
155     },
156     {
157         7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
158         13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
159         10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
160         3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
161     },
162     {
163         2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
164         14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
165         4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
166         11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
167     },
168     {
169         12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
170         10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
171         9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
172         4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
173     },
174     {
175         4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
176         13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
177         1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
178         6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
179     },
180     {
181         13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
182         1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
183         7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
184         2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
185     }
186 };
187  
188                 
189      //P置換      
190      int P[32]={16,  7, 20, 21, 29, 12, 28, 17,
191                 1, 15, 23, 26,  5, 18, 31, 10,
192                 2,  8, 24, 14, 32, 27,  3,  9,
193                 19, 13, 30,  6, 22, 11,  4, 25 }; 
194                 
195                 
196             
197     //1初始置換            
198     //1.1  64明文進行初始置換分左右 
199     int mingwen_1[64];
200     int l[32],r[32];
201     for (int i=0;i<64;i++){
202         mingwen_1[i]=mingwen[IP[i]-1];
203     }   
204     for(int i=0;i<32;i++){
205         l[i]=mingwen_1[i];
206         r[i]=mingwen_1[i+32];
207     } 
208     cout<<"明文初始置換M   =";
209     for(int i=0;i<64;i++){
210         if(i%64==0) cout<<" ";
211         cout<<mingwen_1[i];
212     }
213     cout<<endl;
214     
215     
216     //1.2 56位秘鑰初始置換分左右 
217     
218     cout<<"秘鑰初始置換k0  =";
219     for(int i=0;i<64;i++){
220         if(i%64==0) cout<<" ";
221         cout<<miyao[i];
222     }
223     cout<<endl;
224     
225     int ml[28],mr[28];
226     int miyao_0[58]; 
227     for(int i=0;i<58;i++){
228         miyao_0[i]=miyao[IP_1[i]-1];
229     } 
230     for(int i=0;i<28;i++){
231         ml[i]=miyao_0[i];
232         mr[i]=miyao_0[i+28];
233     }
234     
235      
236     //2.循環加密                  
237     for (int i=0;i<16;i++){
238         
239         cout<<"-------------------------------第"<<i+1<<"輪循環-----------------------------------" <<endl;
240         
241         //2.1明文左右交換 
242         int new_l[32],new_r[48];
243         for(int j=0;j<32;j++){
244             new_l[j]=r[j];
245         }
246         
247         //2.2 右邊32位拓展變換成48位
248         for(int j=0;j<48;j++){
249             new_r[j]=r[E[j]-1];
250         }
251         
252         
253         //2.3 左右秘鑰 左移
254         int new_ml[28],new_mr[28];
255          for(int j=0;j<28;j++){
256              new_ml[j]=ml[(j+28+weiyi[i])%28];
257              new_mr[j]=mr[(j+28+weiyi[i])%28];
258          }
259     
260         //2.4  重新合並成56位的秘鑰
261         int miyao_1[56];
262         for(int j=0;j<28;j++){
263             miyao_1[j]=new_ml[j];
264             miyao_1[j+28]=new_mr[j];
265         } 
266         //2.5  IP_2 56位秘鑰壓縮成48位的秘鑰
267         int k[48];
268         for (int j=0;j<48;j++){
269             k[j]=miyao_1[IP_2[j]-1];
270         } 
271         
272     
273         cout<<"k"<<i+1<<"             =";
274         for(int j=0;j<48;j++){
275           if(j%6==0) cout<<" ";
276           cout<<k[j];
277        }
278         cout<<endl;
279         
280     
281         cout<<"R"<<i<<"            "<<"=";
282         for(int j=0;j<48;j++){
283           if(j%6==0) cout<<" ";
284           cout<<new_r[j];
285        }
286         cout<<endl;
287         
288         
289         //2.6   2.2和2.5XOR
290         int new_r2[48];
291         for(int j=0;j<48;j++){
292             new_r2[j]=new_r[j] ^ k[j];
293         } 
294         
295         
296         cout<<"R(i-1)^ki      "<<"=";
297         for(int j=0;j<48;j++){
298           if(j%6==0) cout<<" ";
299           cout<<new_r2[j];
300        }
301         cout<<endl;
302         
303         //2.7 s盒 
304         int new_r3[32];
305         int b1,b2,b3,b4,b5,b6;
306         int m=0;
307         for(int j=0;j<8;j++){
308             int row = ((new_r2[j*6])<<1)+(new_r2[j*6+5]);   //第1,6位組成行號
309             int col = ((new_r2[j*6+1])<<3)+((new_r2[j*6+2])<<2)+((new_r2[j*6+3])<<1)+(new_r2[j*6+4]);  //第2,3,4,5位組成列號
310               //找到s盒對應的數 
311             int a=s[j][16*row+col];
312     
313             //轉成對應的2進制 
314             int n[4]={0};
315             int f=0;
316             while(a){
317                 n[f]=a%2;
318                 a=a/2;
319                 f++;    
320             }
321             
322             new_r3[m*4]=n[3];
323             new_r3[m*4+1]=n[2];
324             new_r3[m*4+2]=n[1];
325             new_r3[m*4+3]=n[0];
326             m++;
327         }
328         
329         cout<<""<<i+1<<"輪s盒"<<"       =";
330         for(int j=0;j<32;j++){
331           if(j%8==0) cout<<" ";
332           cout<<new_r3[j];
333        }
334         cout<<endl;
335         
336         //2.8 P置換
337         int new_r4[32];
338         for(int j=0;j<32;j++){
339             new_r4[j]=new_r3[P[j]-1];
340         } 
341         
342         cout<<"P置換          "<<"=";
343         for(int j=0;j<32;j++){
344           if(j%8==0) cout<<" ";
345           cout<<new_r4[j];
346        }
347         cout<<endl;
348         
349         
350         
351         //2.9 明文左邊32位和2.8 new_r4[32] XOR
352         int new_r5[32];
353         for(int j=0;j<32;j++){
354             new_r5[j]=l[j] ^ new_r4[j];
355             //更新左右明文,以便於下次循環 
356             l[j]=new_l[j];
357             r[j]=new_r5[j];
358         } 
359     
360         cout<<"R"<<i+1<<"             =";
361         for(int j=0;j<32;j++){
362           if(j%8==0) cout<<" ";
363           cout<<new_r5[j];
364        }
365         cout<<endl;
366         cout<<"L"<<i+1<<"             =";
367         for(int j=0;j<32;j++){
368           if(j%8==0) cout<<" ";
369           cout<<new_l[j];
370        }
371         cout<<endl;
372         //2.10 更新左右秘鑰,以便於下次循環 
373          for(int j=0;j<28;j++){
374              ml[j]=new_ml[j];
375              mr[j]=new_mr[j];
376          } 
377          
378          
379          cout<<"----------------------------------------------------------------------------"<<endl<<endl<<endl; 
380          
381     }
382     
383     //3.  循環加密之后的明文mingwen_1[64]
384     for(int i=0;i<32;i++){
385         mingwen_1[i]=r[i];
386         mingwen_1[i+32]=l[i];
387     } 
388     
389     
390 //    cout<<"逆置換前=";
391 //    for(int i=0;i<64;i++){
392 //        cout<<mingwen_1[i]; 
393 //    } 
394 //    cout<<endl<<endl;
395     
396     //4.  逆置換 
397     int miwen[64];
398     for(int i=0;i<64;i++){
399         miwen[IP[i]-1]=mingwen_1[i];
400     } 
401     
402     
403     //5.輸出
404     
405     cout<<"原明文為:"; 
406     for(int i=0;i<64;i++){
407         if(i%64==0) cout<<endl;
408         cout<<mingwen[i];
409     } 
410     cout<<endl<<endl;
411     cout<<"秘鑰為:";
412     for(int i=0;i<64;i++){
413         if(i%64==0) cout<<endl;
414         cout<<miyao[i];
415     } 
416     cout<<endl<<endl;
417     cout<<"密文為:";
418     for(int i=0;i<64;i++){
419         if(i%64==0) cout<<endl;
420         cout<<miwen[i];
421     }
422      //是否將密文轉換成十六進制?(Y  or  N)
423       
424     cout<<endl<<endl<<"是否將密文轉換成十六進制?(Y  or  N):";
425     char YN;
426     cin>>YN; 
427     cout<<endl<<"十六進制密文表示:";
428     if(YN=='Y'){
429         for(int i=0;i<16;i++){
430             int q=miwen[i*4]*8+miwen[i*4+1]*4+miwen[i*4+2]*2+miwen[i*4+3];
431             if(q>=0 && q<= 9){
432                 cout<<q;
433             } 
434             else{
435                 cout<<hex<<uppercase<<q;                
436             }
437         }
438     }
439     cout<<endl<<endl<<"!!!!皆大歡喜,普天同慶!!!!"<<endl;
440     return 0; 
441 }
442  
443 int jiemi(){
444     // 密文 
445     int miwen[64];
446            
447            
448     cout<<"請輸入16位十六進制的密文:";
449     string  kk;
450     cin>>kk;
451     int len=kk.length();
452     
453     
454     while(len!=16){
455         cout<<"請重新輸入16位十六進制的密文:";
456         cin>>kk; 
457         len=kk.length();
458     }
459     
460     int jishu=0;
461     for(int i=0;i<16;i++){
462         int a;
463         if(kk[i]>='0'&&kk[i]<='9')
464            a=kk[i]-'0';
465         else
466            a=kk[i]-'A'+10;
467            
468         int n[4]={0};
469         int f=0;
470         while(a){
471             n[f]=a%2;
472             a=a/2;
473             f++;
474         }
475         miwen[jishu*4]=n[3];
476         miwen[jishu*4+1]=n[2];
477         miwen[jishu*4+2]=n[1];
478         miwen[jishu*4+3]=n[0];
479         jishu++; 
480     } 
481  
482     //初始置換IP 
483     int IP[64]={58, 50, 42, 34, 26, 18, 10, 2,
484                 60, 52, 44, 36, 28, 20, 12, 4,
485                 62, 54, 46, 38, 30, 22, 14, 6,
486                 64, 56, 48, 40, 32, 24, 16, 8,
487                 57, 49, 41, 33, 25, 17,  9, 1,
488                 59, 51, 43, 35, 27, 19, 11, 3,
489                 61, 53, 45, 37, 29, 21, 13, 5,
490                 63, 55, 47, 39, 31, 23, 15, 7};
491                             
492     // 選擇運算E   32位明文擴充為48位                
493     int E[48]={ 32,  1,  2,  3,  4,  5,
494                 4,  5,  6,  7,  8,  9,
495                 8,  9, 10, 11, 12, 13,
496                12, 13, 14, 15, 16, 17,
497                16, 17, 18, 19, 20, 21,
498                20, 21, 22, 23, 24, 25,
499                24, 25, 26, 27, 28, 29,
500                28, 29, 30, 31, 32,  1 }; 
501                                     
502     //64位秘鑰     0123456789ABCDEF    56位的秘鑰+8位校驗碼 
503     int miyao[64];
504     cout<<"請輸入16位十六進制的秘鑰:";
505     string  k_2;
506     cin>>k_2;
507     int len_2=k_2.length();
508  
509     
510     while(len_2!=16){
511         cout<<"請重新輸入16位十六進制的秘鑰:";
512         cin>>k_2; 
513         len_2=k_2.length();
514     }
515     
516     int jishu_2=0;
517     for(int i=0;i<16;i++){
518         int a;
519         if(k_2[i]>='0'&&k_2[i]<='9')
520            a=k_2[i]-'0';
521         else
522            a=k_2[i]-'A'+10;
523            
524         int n[4]={0};
525         int f=0;
526         while(a){
527             n[f]=a%2;
528             a=a/2;
529             f++;
530         }
531         miyao[jishu_2*4]=n[3];
532         miyao[jishu_2*4+1]=n[2];
533         miyao[jishu_2*4+2]=n[1];
534         miyao[jishu_2*4+3]=n[0];
535         jishu_2++; 
536     }      
537            
538            
539     //置換選擇1
540     int IP_1[56]={57, 49, 41, 33, 25, 17,  9,
541                    1, 58, 50, 42, 34, 26, 18,
542                   10,  2, 59, 51, 43, 35, 27,
543                   19, 11,  3, 60, 52, 44, 36,
544                   63, 55, 47, 39, 31, 23, 15,
545                    7, 62, 54, 46, 38, 30, 22,
546                   14,  6, 61, 53, 45, 37, 29,
547                   21, 13,  5, 28, 20, 12,  4};
548      
549     //16次左移對應的位數                 
550     int weiyi[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};     
551     // 置換選擇2 秘鑰56->48位壓縮 
552     int IP_2[48]={14, 17, 11, 24,  1,  5,
553                    3, 28, 15,  6, 21, 10,
554                   23, 19, 12,  4, 26,  8,
555                   16,  7, 27, 20, 13,  2,
556                   41, 52, 31, 37, 47, 55,
557                   30, 40, 51, 45, 33, 48,
558                   44, 49, 39, 56, 34, 53,
559                   46, 42, 50, 36, 29, 32};
560                                 
561      //S盒                
562     int s[8][65]=
563 {
564     {
565         14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
566         0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
567         4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
568         15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
569     },
570     {
571         15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
572         3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
573         0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
574         13,8,10,1,3,15,4,2,11,6,7,12,10,5,14,9
575     },
576     {
577         10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
578         13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
579         13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
580         1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
581     },
582     {
583         7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
584         13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
585         10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
586         3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
587     },
588     {
589         2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
590         14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
591         4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
592         11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
593     },
594     {
595         12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
596         10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
597         9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
598         4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
599     },
600     {
601         4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
602         13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
603         1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
604         6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
605     },
606     {
607         13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
608         1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
609         7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
610         2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
611     }
612 };
613  
614                 
615      //P置換      
616      int P[32]={16,  7, 20, 21, 29, 12, 28, 17,
617                 1, 15, 23, 26,  5, 18, 31, 10,
618                 2,  8, 24, 14, 32, 27,  3,  9,
619                 19, 13, 30,  6, 22, 11,  4, 25 }; 
620                             
621     //1初始置換    
622             
623     //1.1  64密文進行逆置換分左右 
624     int miwen_1[64];
625     int l[32],r[32];
626     for (int i=0;i<64;i++){
627         miwen_1[i]=miwen[IP[i]-1];
628     }   
629     for(int i=0;i<32;i++){
630         r[i]=miwen_1[i];
631         l[i]=miwen_1[i+32];
632     } 
633     cout<<"密文逆置換     =";
634     for(int i=0;i<64;i++){
635         if(i%64==0) cout<<" ";
636         cout<<miwen_1[i];
637     }
638     cout<<endl;
639     
640     
641     //1.2 56位秘鑰初始置換分左右 
642     
643     cout<<"秘鑰初始置換k0 =";
644     for(int i=0;i<64;i++){
645         if(i%64==0) cout<<" ";
646         cout<<miyao[i];
647     }
648     cout<<endl;
649     
650     int ml_0[28],mr_0[28];
651     int miyao_0[58]; 
652     for(int i=0;i<58;i++){
653         miyao_0[i]=miyao[IP_1[i]-1];
654     } 
655     for(int i=0;i<28;i++){
656         ml_0[i]=miyao_0[i];
657         mr_0[i]=miyao_0[i+28];
658     }
659     
660     // 因為加密過程一共左移了28位,回到了原來的位置,
661     //所以我們要先將原始秘鑰左移一次。
662     
663     int ml[28],mr[28];
664     for(int j=0;j<28;j++){
665         ml[j]=ml_0[(j+28+1)%28];
666         mr[j]=mr_0[(j+28+1)%28];
667     }    
668         
669      
670     //2.循環解密                  
671     for (int i=0;i<16;i++){
672         
673         cout<<"-------------------------------第"<<i+1<<"輪循環解密-----------------------------------" <<endl;
674         
675         //2.1密文左右交換 
676         int new_l[32],new_r[48];
677         for(int j=0;j<32;j++){
678             new_r[j]=l[j];
679         }
680         
681         //2.2 左邊32位拓展變換成48位
682         for(int j=0;j<48;j++){
683             new_l[j]=new_r[E[j]-1];
684         }
685         
686         //2.3 左右秘鑰 右移
687          
688         int new_ml[28],new_mr[28];
689          for(int j=0;j<28;j++){
690              new_ml[j]=ml[(j+28-weiyi[16-i])%28];
691              new_mr[j]=mr[(j+28-weiyi[16-i])%28];
692          }
693     
694         //2.4  重新合並成56位的秘鑰
695         int miyao_1[56];
696         for(int j=0;j<28;j++){
697             miyao_1[j]=new_ml[j];
698             miyao_1[j+28]=new_mr[j];
699         } 
700         //2.5  IP_2 56位秘鑰壓縮成48位的秘鑰
701         int k[48];
702         for (int j=0;j<48;j++){
703             k[j]=miyao_1[IP_2[j]-1];
704         } 
705         
706         cout<<"k"<<16-i<<"             =";
707         for(int j=0;j<48;j++){
708           if(j%6==0) cout<<" ";
709           cout<<k[j];
710        }
711         cout<<endl;
712         
713         cout<<"L"<<16-i<<"             "<<"=";
714         for(int j=0;j<48;j++){
715           if(j%6==0) cout<<" ";
716           cout<<new_l[j];
717        }
718         cout<<endl;
719         
720         
721         //2.6   2.2和2.5XOR
722         int new_l2[48];
723         for(int j=0;j<48;j++){
724             new_l2[j]=new_l[j] ^ k[j];
725         } 
726         
727         cout<<"Li^ki         "<<"=";
728         for(int j=0;j<48;j++){
729           if(j%6==0) cout<<" ";
730           cout<<new_l2[j];
731        }
732         cout<<endl;
733         
734         //2.7 s盒 
735         int new_l3[32];
736         int b1,b2,b3,b4,b5,b6;
737         int m=0;
738         for(int j=0;j<8;j++){
739             int row = ((new_l2[j*6])<<1)+(new_l2[j*6+5]);   //第1,6位組成行號
740             int col = ((new_l2[j*6+1])<<3)+((new_l2[j*6+2])<<2)+((new_l2[j*6+3])<<1)+(new_l2[j*6+4]);  //第2,3,4,5位組成列號
741               //找到s盒對應的數 
742             int a=s[j][16*row+col];
743     
744             //轉成對應的2進制 
745             int n[4]={0};
746             int f=0;
747             while(a){
748                 n[f]=a%2;
749                 a=a/2;
750                 f++;    
751             }
752             
753             new_l3[m*4]=n[3];
754             new_l3[m*4+1]=n[2];
755             new_l3[m*4+2]=n[1];
756             new_l3[m*4+3]=n[0];
757             m++;
758         }
759         
760         cout<<""<<16-i<<"輪s盒"<<"       =";
761         for(int j=0;j<32;j++){
762           if(j%8==0) cout<<" ";
763           cout<<new_l3[j];
764        }
765         cout<<endl;
766         
767         //2.8 P置換
768         int new_l4[32];
769         for(int j=0;j<32;j++){
770             new_l4[j]=new_l3[P[j]-1];
771         } 
772         
773         cout<<"P置換          "<<"=";
774         for(int j=0;j<32;j++){
775           if(j%8==0) cout<<" ";
776           cout<<new_l4[j];
777        }
778         cout<<endl;
779         
780         //2.9 密文右邊32位和2.8 new_l4[32] XOR   
781         int new_l5[32];
782         for(int j=0;j<32;j++){
783             new_l5[j]=r[j] ^ new_l4[j];
784             //更新左右明文,以便於下次循環 
785             r[j]=new_r[j];
786             l[j]=new_l5[j];
787         } 
788     
789         cout<<"L"<<16-i-1<<"            =";
790         for(int j=0;j<32;j++){
791           if(j%8==0) cout<<" ";
792           cout<<new_l5[j];
793        }
794         cout<<endl;
795         cout<<"R"<<16-i-1<<"            =";
796         for(int j=0;j<32;j++){
797           if(j%8==0) cout<<" ";
798           cout<<new_r[j];
799        }
800         cout<<endl;
801         
802         //2.10 更新左右秘鑰,以便於下次循環 
803          for(int j=0;j<28;j++){
804              ml[j]=new_ml[j];
805              mr[j]=new_mr[j];
806          } 
807          
808          cout<<"ml"<<16-i<<"=";
809          for(int j=0;j<28;j++){
810              cout<<ml[j];
811          } 
812          cout<<endl;
813          cout<<"mr"<<16-i<<"=";
814          for(int j=0;j<28;j++){
815              cout<<mr[j];
816          } 
817          cout<<endl;
818          
819          cout<<"------------------------------------------------------------------------------"<<endl<<endl<<endl; 
820          
821     }
822     
823     //3.  循環解密之后的密文miwen_1[64]
824     for(int i=0;i<32;i++){
825         miwen_1[i]=l[i];
826         miwen_1[i+32]=r[i];
827     } 
828     //4.  初始置換 
829     int mingwen[64];
830     for(int i=0;i<64;i++){
831         mingwen[IP[i]-1]=miwen_1[i];
832     } 
833             
834     
835     //5.輸出
836     
837     cout<<"原密文為:"; 
838     for(int i=0;i<64;i++){
839         if(i%64==0) cout<<endl;
840         cout<<miwen[i];
841     } 
842     cout<<endl<<endl;
843     cout<<"秘鑰為:";
844     for(int i=0;i<64;i++){
845         if(i%64==0) cout<<endl;
846         cout<<miyao[i];
847     } 
848     cout<<endl<<endl;
849     cout<<"明文為:";
850     for(int i=0;i<64;i++){
851         if(i%64==0) cout<<endl;
852         cout<<mingwen[i];
853     }
854      //是否將明文轉換成十六進制?(Y  or  N)
855       
856     cout<<endl<<endl<<"是否將明文轉換成十六進制?(Y  or  N):";
857     char YN;
858     cin>>YN; 
859     cout<<endl<<"十六進制明表示:";
860     if(YN=='Y'){
861         for(int i=0;i<16;i++){
862             int q=mingwen[i*4]*8+mingwen[i*4+1]*4+mingwen[i*4+2]*2+mingwen[i*4+3];
863             if(q>=0 && q<= 9){
864                 cout<<q;
865             } 
866             else{
867                 cout<<hex<<uppercase<<q;                
868             }
869         }
870     }
871     cout<<endl<<endl<<"!!!!皆大歡喜,普天同慶!!!!"<<endl;
872     return 0;
873 } 
874  
875 int main(){
876     
877     
878     while(1){
879     int number;
880     cout<<"===歡迎來到DES加密算法系統==="<<endl;
881     cout<<"                  Author:十七"<<endl; 
882     cout<<"目前支持的操作有:"<<endl;
883     cout<<"  1.加密操作;"<<endl;
884     cout<<"  2.解密操作;"<<endl;
885     cout<<"  0.退出。"<<endl;
886     cout<<"============================="<<endl; 
887     cout<<"請選擇您項進行的操作:";
888     cin>>number;
889     cout<<endl;
890      
891     if(number==1){
892         jiami();
893     }
894     else if(number==2){
895         jiemi();
896     } 
897     else{
898         cout<<"您已成功退出系統,如果您對本次服務感到滿意,歡迎五星好評^_^"<<endl;
899         return 0;    
900      }
901     } 
902     return 0;
903 } 

 

運行結果截圖:

 

 ......

 

 

 ......

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM