|
![]() | 作者: hardwater [hardwater]
![]() |
登录 |
#include<iostream.h> //定义好用的结构 struct subS{ int ss[2]; }; struct word{ subS B[4]; }; void main(){ int key[32]={2,10,7,14,1,5,1,6, 2,8,10,14,13,2,10,6, 10,11,15,7,1,5,8,8, 0,9,12,15,4,15,3,12 };//128bit密钥 subS S[16][16]={ {{6,3},{7,12},{7,7},{7,11},{15,2},{6,11},{6,15},{12,5},{3,0},{0,1},{6,7},{2,11},{15,14},{13,7},{10,11},{7,6}}, {{12,10},{8,2},{12,9},{7,13},{15,10},{5,9},{4,7},{15,0},{10,13},{13,4},{10,2},{10,15},{9,12},{10,4},{7,2},{12,0}}, {{11,7},{15,13},{9,3},{2,6},{3,6},{3,15},{15,7},{12,12},{3,4},{10,5},{14,5},{15,1},{7,1},{13,8},{3,1},{1,5}}, {{0,4},{12,7},{2,3},{12,3},{1,8},{9,6},{0,5},{9,10},{0,7},{1,2},{8,0},{14,2},{14,11},{2,7},{11,2},{7,5}}, {{0,9},{8,3},{2,12},{1,10},{1,11},{6,14},{5,10},{10,0},{5,2},{3,11},{13,6},{11,3},{2,9},{14,3},{2,15},{8,4}}, {{5,3},{13,1},{0,0},{14,13},{2,0},{15,12},{11,1},{5,11},{6,10},{12,11},{11,14},{3,9},{4,10},{4,12},{5,8},{12,15}}, {{13,0},{14,15},{10,10},{15,11},{4,3},{4,13},{3,3},{8,5},{4,5},{15,9},{0,2},{7,15},{5,0},{3,12},{9,15},{10,8}}, {{5,1},{10,3},{4,0},{8,15},{9,2},{9,13},{3,8},{15,5},{11,12},{11,6},{13,10},{2,1},{1,0},{15,15},{15,3},{13,2}}, {{12,13},{0,12},{1,3},{14,12},{5,15},{9,7},{4,4},{1,7},{12,4},{10,7},{7,14},{3,13},{6,4},{5,13},{1,9},{7,3}}, {{6,0},{8,1},{4,15},{13,12},{2,2},{2,10},{9,0},{8,8},{4,6},{14,14},{11,8},{1,4},{13,14},{5,14},{0,11},{13,12}}, {{14,0},{3,2},{3,10},{0,10},{4,9},{0,6},{2,4},{5,12},{12,2},{13,3},{10,12},{6,2},{9,1},{9,5},{14,4},{7,9}}, {{14,7},{12,8},{3,7},{6,13},{8,13},{13,5},{4,14},{10,9},{6,12},{5,6},{15,4},{14,10},{6,5},{7,10},{10,14},{0,8}}, {{11,10},{7,8},{2,5},{2,14},{1,12},{10,6},{11,4},{12,6},{14,8},{13,13},{7,4},{1,15},{4,11},{11,13},{8,11},{8,10}}, {{7,0},{3,14},{11,5},{6,6},{4,8},{0,3},{15,6},{0,14},{6,1},{3,5},{5,7},{11,9},{8,6},{12,1},{1,13},{9,14}}, {{14,1},{15,8},{9,8},{1,1},{6,9},{13,9},{8,14},{9,4},{9,11},{1,14},{8,7},{14,9},{12,14},{5,5},{2,8},{13,15}}, {{8,12},{10,1},{8,9},{0,13},{11,15},{14,6},{4,2},{6,8},{4,1},{9,9},{2,13},{0,15},{11,0},{5,4},{11,11},{1,6}} }; //S盒的初始化 int RCon[10][8]={ {0,1,0,0,0,0,0,0}, {0,2,0,0,0,0,0,0}, {0,4,0,0,0,0,0,0}, {0,8,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0}, {2,0,0,0,0,0,0,0}, {4,0,0,0,0,0,0,0}, {8,0,0,0,0,0,0,0}, {1,11,0,0,0,0,0,0}, {3,6,0,0,0,0,0,0} };//固定变换码 word W[44]; for(int i=0 ;i<4;i++) { for(int j=0;j<4;j++) for(int k=0;k<2;k++) { W[i].B[j].ss[k]=key[8*i+2*j+k]; cout<<W[i].B[j].ss[k]<<" "; } cout<<endl; cout<<"-----------------------"<<"就是W"<<i<<"密钥的值"<<endl; } //---------------------------------------完成W[1-------4]的初始化 for(int f=4 ;f<44;f++) { word temp; for(int x=0;x<4;x++) for(int y=0;y<2;y++){ temp.B[x].ss[y]=W[f-1].B[x].ss[y]; //cout<<temp.B[x].ss[y]<<" "; };//完成W[f-1]-->temp //-----------------------------------------------// if((f%4)==0) { subS t=temp.B[0]; for(int x=1;x<4;x++) temp.B[x-1]=temp.B[x]; temp.B[3]=t; //完成Rotword //------------------------------------------------// for(int e=0;e<4;e++) { temp.B[e]=S[temp.B[e].ss[0]][temp.B[e].ss[1]]; } //完成S盒的变换 /* for(int y=0;y<4;y++) for(int r=0;r<2;r++) cout<< temp.B[y].ss[r] <<" "; cout<<endl; */ //-----------------------------------------------// int xytemp[32]; for(int r=0;r<4;r++) { for(int u=0;u<2;u++) for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) for(int h=0;h<2;h++) if(8*i+4*j+2*k+1*h==temp.B[r].ss[u]) { int xtemp[4]={i,j,k,h}; for(int p=0;p<4;p++) { xytemp[8*r+4*u+p]=xtemp[p]; } } }//for(r) /* for(int p=0;p<32;p++) { cout<<xytemp[p]<<" "; if((p+1)%8==0) cout<<endl; } cout<<endl; */ //--------------------//完成temp的16位到2位的转化 int RContemp[32]; for(int d=0;d<8;d++) { for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) for(int h=0;h<2;h++) if(8*i+4*j+2*k+1*h==RCon[f/4][d]) { int xtemp[4]={i,j,k,h}; for(int p=0;p<4;p++) { RContemp[4*d+p]=xtemp[p]; } } }//for(d) /* for(int q=0;q<32;q++) { cout<<RContemp[q]<<" "; // if((p+1)%8==0) // cout<<endl; } cout<<endl; */ //--------------------//完成RCon[f/4]的16位到2位的转化 int xyrtemp[32]; for(int TR=0;TR<32;TR++) { xyrtemp[TR]=(xytemp[TR]+RContemp[TR])%2; // cout<<xytemp[TR]<<" "; } for(int j=0;j<4;j++) { for(int k=0;k<2;k++) { temp.B[j].ss[k]=8*xyrtemp[8*j+4*k]+4*xyrtemp[8*j+4*k+1]+2*xyrtemp[8*j+4*k+2]+1*xyrtemp[8*j+4*k+3]; // cout<<temp.B[j].ss[k]<<" "; } } // cout<<endl; //--------------------//完成RCon[f/4]与temp的模2 }//if int wtemp[32]; for(int r=0;r<4;r++) { for(int u=0;u<2;u++) for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) for(int h=0;h<2;h++) if(8*i+4*j+2*k+1*h==W[f-4].B[r].ss[u]) { int xtemp[4]={i,j,k,h}; for(int p=0;p<4;p++) { wtemp[8*r+4*u+p]=xtemp[p]; } } }//for(r) /* for(int p=0;p<32;p++) { cout<<wtemp[p]<<" "; if((p+1)%8==0) cout<<endl; } cout<<endl; */ //--------------------------------------//完成W[f-4]的16位到2位的转化 int xxyytemp[32]; for(int rr=0;rr<4;rr++) { for(int u=0;u<2;u++) for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) for(int h=0;h<2;h++) if(8*i+4*j+2*k+1*h==temp.B[rr].ss[u]) { int xtemp[4]={i,j,k,h}; for(int p=0;p<4;p++) { xxyytemp[8*rr+4*u+p]=xtemp[p]; } } }//for(r) /* for(int pp=0;pp<32;pp++) { cout<<xxyytemp[pp]<<" "; if((pp+1)%8==0) cout<<endl; } cout<<endl; */ //---------------------------------------//完成temp'的16位到2位的转化 int xxyywtemp[32]; for(int TR=0;TR<32;TR++) { xxyywtemp[TR]=(xxyytemp[TR]+wtemp[TR])%2; // cout<<xxyywtemp[TR]<<" "; // if((TR+1)%8==0) // cout<<endl; } for(int j=0;j<4;j++) { for(int k=0;k<2;k++) { W[f].B[j].ss[k]=8*xxyywtemp[8*j+4*k]+4*xxyywtemp[8*j+4*k+1]+2*xxyywtemp[8*j+4*k+2]+1*xxyywtemp[8*j+4*k+3]; cout<<W[f].B[j].ss[k]<<" "; } } cout<<endl; cout<<"-----------------------"<<"就是W"<<f<<"密钥的值"<<endl; }//////////////////////////for(f)(4--44) } |
地主 发表时间: 11/04 17:10 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号