📄 subkeyproduce.h
字号:
#include <string.h>
#include <iostream.h>
#include "GlobalData.h"
/*************************************/
void circularLS1(int *ch);
void circularLS2(int *ch);
void compresspermu(int *chl,int *chr,int turn);
/***************************************************************************************************/
void SKeyProduce(char *KEY)
{
char key[7];
strncpy(key,KEY,7); //key末尾没有结束符的
// cout<<key[0]<<key[1]<<key[2]<<key[3]<<key[4]<<key[5]<<key[6]<<endl; 验证密钥拷贝无误
/****把密钥转换成56位的整数****/
int k[56];
int index=55;
for(int i=6;i>=0;i--){
for(int j=0;j<8;j++){
k[index--]=(int)key[i]%2;
key[i]=key[i]/2;
}
}
// for(int m=0;m<56;m++)cout<<k[m]<<" "; 验证密钥比特转换无误
// cout<<endl;
/****将56位密钥分为两半****/
int kl[28];
int kr[28];
for(i=0;i<28;i++)kl[i]=k[i];
for(i=0;i<28;i++)kr[i]=k[i+28];
/* for(m=0;m<28;m++)cout<<kl[m]<<" "; 验证密钥对半无误
cout<<endl;
for(m=0;m<28;m++)cout<<kr[m]<<" ";
*/
/****循环左移、压缩置换生成48位子密码****/
int j=1;
while(j<17){
switch(j){
case 1:
case 2:
case 9:
case 16:
circularLS1(kl);
circularLS1(kr);
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
circularLS2(kl);
circularLS2(kr);
break;
default:
cout<<"生成子密钥过程出错"<<endl;
}
compresspermu(kl,kr,j);
j++;
}
}
/*************************************************************************************************/
void circularLS1(int *ch) //循环左移1位,ch为长28的int数组
{
int temp=ch[0];
for(int i=1;i<28;i++)ch[i-1]=ch[i];
ch[27]=temp;
// cout<<"左循环一位:"<<endl; 验证密钥左循环一位无误
// for(i=0;i<28;i++)cout<<ch[i]<<" ";
}
void circularLS2(int *ch) //循环左移2位,ch为长28的int数组
{
int temp1=ch[0];
int temp2=ch[1];
for(int i=2;i<28;i=i+2){
ch[i-2]=ch[i];
ch[i-1]=ch[i+1];
}
ch[26]=temp1;
ch[27]=temp2;
// cout<<"左循环两位:"<<endl; 验证密钥左循环两位无误
// for(i=0;i<28;i++)cout<<ch[i]<<" ";
}
void compresspermu(int *chl,int *chr,int turn) //密钥作相应移位后,选择56位中的48位
{
int CPTable[]={13,16,10,23,0,4,2,27,14,5,20,9,
22,18,11,3,25,7,15,6,26,19,12,1,
40,51,30,36,46,54,29,39,50,44,32,47,
43,48,38,55,33,52,45,41,49,35,28,31};
for(int i=0;i<48;i++){ //生成第turn轮的48位子密钥
if(CPTable[i]<28)
SubKey[turn-1][i]=chl[CPTable[i]]; //chl和chr只用于赋值,不会改变
else
SubKey[turn-1][i]=chr[(CPTable[i]-28)];
}
// cout<<"第"<<turn<<"轮子密钥:"<<endl; 验证密钥压缩置换无误
// for(i=0;i<48;i++)cout<<SubKey[turn-1][i]<<" ";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -