DES加密解密算法C++实现-LMLPHP

DES加密算法并不难,是由一些简单的变换得来的,难的是要有足够的耐心。蒟蒻并不想说自己用了多久才把代码写好的。

DES加密解密算法C++实现-LMLPHP

DES加密解密算法C++实现-LMLPHP

代码:

我真的太难了QAQ

 #include<iostream>
using namespace std; int jiami(){ // 明文
int mingwen[]; cout<<"请输入16位十六进制的明文:";
string kk;
cin>>kk;
int len=kk.length(); while(len!=){
cout<<"请重新输入16位十六进制的明文:";
cin>>kk;
len=kk.length();
} int jishu=;
for(int i=;i<;i++){
int a;
if(kk[i]>=''&&kk[i]<='')
a=kk[i]-'';
else
a=kk[i]-'A'+; int n[]={};
int f=;
while(a){
n[f]=a%;
a=a/;
f++;
}
mingwen[jishu*]=n[];
mingwen[jishu*+]=n[];
mingwen[jishu*+]=n[];
mingwen[jishu*+]=n[];
jishu++;
} //初始置换IP
int IP[]={, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , }; // 选择运算E 32位明文扩充为48位
int E[]={ , , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , }; //64位秘钥 0123456789ABCDEF 56位的秘钥+8位校验码
int miyao[];
cout<<"请输入16位十六进制的秘钥:";
string k_2;
cin>>k_2;
int len_2=k_2.length(); while(len_2!=){
cout<<"请重新输入16位十六进制的秘钥:";
cin>>k_2;
len_2=k_2.length();
} int jishu_2=;
for(int i=;i<;i++){
int a;
if(k_2[i]>=''&&k_2[i]<='')
a=k_2[i]-'';
else
a=k_2[i]-'A'+; int n[]={};
int f=;
while(a){
n[f]=a%;
a=a/;
f++;
}
miyao[jishu_2*]=n[];
miyao[jishu_2*+]=n[];
miyao[jishu_2*+]=n[];
miyao[jishu_2*+]=n[];
jishu_2++;
} //置换选择1
int IP_1[]={, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , }; //16次左移对应的位数
int weiyi[]={,,,,,,,,,,,,,,,};
// 置换选择2 秘钥56->48位压缩
int IP_2[]={, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , }; //S盒 int s[][]=
{
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
}
}; //P置换
int P[]={, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , }; //1初始置换
//1.1 64明文进行初始置换分左右
int mingwen_1[];
int l[],r[];
for (int i=;i<;i++){
mingwen_1[i]=mingwen[IP[i]-];
}
for(int i=;i<;i++){
l[i]=mingwen_1[i];
r[i]=mingwen_1[i+];
}
cout<<"明文初始置换M =";
for(int i=;i<;i++){
if(i%==) cout<<" ";
cout<<mingwen_1[i];
}
cout<<endl; //1.2 56位秘钥初始置换分左右 cout<<"秘钥初始置换k0 =";
for(int i=;i<;i++){
if(i%==) cout<<" ";
cout<<miyao[i];
}
cout<<endl; int ml[],mr[];
int miyao_0[];
for(int i=;i<;i++){
miyao_0[i]=miyao[IP_1[i]-];
}
for(int i=;i<;i++){
ml[i]=miyao_0[i];
mr[i]=miyao_0[i+];
} //2.循环加密
for (int i=;i<;i++){ cout<<"-------------------------------第"<<i+<<"轮循环-----------------------------------" <<endl; //2.1明文左右交换
int new_l[],new_r[];
for(int j=;j<;j++){
new_l[j]=r[j];
} //2.2 右边32位拓展变换成48位
for(int j=;j<;j++){
new_r[j]=r[E[j]-];
} //2.3 左右秘钥 左移
int new_ml[],new_mr[];
for(int j=;j<;j++){
new_ml[j]=ml[(j++weiyi[i])%];
new_mr[j]=mr[(j++weiyi[i])%];
} //2.4 重新合并成56位的秘钥
int miyao_1[];
for(int j=;j<;j++){
miyao_1[j]=new_ml[j];
miyao_1[j+]=new_mr[j];
}
//2.5 IP_2 56位秘钥压缩成48位的秘钥
int k[];
for (int j=;j<;j++){
k[j]=miyao_1[IP_2[j]-];
} cout<<"k"<<i+<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<k[j];
}
cout<<endl; cout<<"R"<<i<<" "<<"=";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_r[j];
}
cout<<endl; //2.6 2.2和2.5XOR
int new_r2[];
for(int j=;j<;j++){
new_r2[j]=new_r[j] ^ k[j];
} cout<<"R(i-1)^ki "<<"=";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_r2[j];
}
cout<<endl; //2.7 s盒
int new_r3[];
int b1,b2,b3,b4,b5,b6;
int m=;
for(int j=;j<;j++){
int row = ((new_r2[j*])<<)+(new_r2[j*+]); //第1,6位组成行号
int col = ((new_r2[j*+])<<)+((new_r2[j*+])<<)+((new_r2[j*+])<<)+(new_r2[j*+]); //第2,3,4,5位组成列号
//找到s盒对应的数
int a=s[j][*row+col]; //转成对应的2进制
int n[]={};
int f=;
while(a){
n[f]=a%;
a=a/;
f++;
} new_r3[m*]=n[];
new_r3[m*+]=n[];
new_r3[m*+]=n[];
new_r3[m*+]=n[];
m++;
} cout<<"第"<<i+<<"轮s盒"<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_r3[j];
}
cout<<endl; //2.8 P置换
int new_r4[];
for(int j=;j<;j++){
new_r4[j]=new_r3[P[j]-];
} cout<<"P置换 "<<"=";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_r4[j];
}
cout<<endl; //2.9 明文左边32位和2.8 new_r4[32] XOR
int new_r5[];
for(int j=;j<;j++){
new_r5[j]=l[j] ^ new_r4[j];
//更新左右明文,以便于下次循环
l[j]=new_l[j];
r[j]=new_r5[j];
} cout<<"R"<<i+<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_r5[j];
}
cout<<endl;
cout<<"L"<<i+<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_l[j];
}
cout<<endl;
//2.10 更新左右秘钥,以便于下次循环
for(int j=;j<;j++){
ml[j]=new_ml[j];
mr[j]=new_mr[j];
} cout<<"----------------------------------------------------------------------------"<<endl<<endl<<endl; } //3. 循环加密之后的明文mingwen_1[64]
for(int i=;i<;i++){
mingwen_1[i]=r[i];
mingwen_1[i+]=l[i];
} // cout<<"逆置换前=";
// for(int i=0;i<64;i++){
// cout<<mingwen_1[i];
// }
// cout<<endl<<endl; //4. 逆置换
int miwen[];
for(int i=;i<;i++){
miwen[IP[i]-]=mingwen_1[i];
} //5.输出 cout<<"原明文为:";
for(int i=;i<;i++){
if(i%==) cout<<endl;
cout<<mingwen[i];
}
cout<<endl<<endl;
cout<<"秘钥为:";
for(int i=;i<;i++){
if(i%==) cout<<endl;
cout<<miyao[i];
}
cout<<endl<<endl;
cout<<"密文为:";
for(int i=;i<;i++){
if(i%==) cout<<endl;
cout<<miwen[i];
}
//是否将密文转换成十六进制?(Y or N) cout<<endl<<endl<<"是否将密文转换成十六进制?(Y or N):";
char YN;
cin>>YN;
cout<<endl<<"十六进制密文表示:";
if(YN=='Y'){
for(int i=;i<;i++){
int q=miwen[i*]*+miwen[i*+]*+miwen[i*+]*+miwen[i*+];
if(q>= && q<= ){
cout<<q;
}
else{
cout<<hex<<uppercase<<q;
}
}
}
cout<<endl<<endl<<"!!!!皆大欢喜,普天同庆!!!!"<<endl;
return ;
} int jiemi(){
// 密文
int miwen[]; cout<<"请输入16位十六进制的密文:";
string kk;
cin>>kk;
int len=kk.length(); while(len!=){
cout<<"请重新输入16位十六进制的密文:";
cin>>kk;
len=kk.length();
} int jishu=;
for(int i=;i<;i++){
int a;
if(kk[i]>=''&&kk[i]<='')
a=kk[i]-'';
else
a=kk[i]-'A'+; int n[]={};
int f=;
while(a){
n[f]=a%;
a=a/;
f++;
}
miwen[jishu*]=n[];
miwen[jishu*+]=n[];
miwen[jishu*+]=n[];
miwen[jishu*+]=n[];
jishu++;
} //初始置换IP
int IP[]={, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , }; // 选择运算E 32位明文扩充为48位
int E[]={ , , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , }; //64位秘钥 0123456789ABCDEF 56位的秘钥+8位校验码
int miyao[];
cout<<"请输入16位十六进制的秘钥:";
string k_2;
cin>>k_2;
int len_2=k_2.length(); while(len_2!=){
cout<<"请重新输入16位十六进制的秘钥:";
cin>>k_2;
len_2=k_2.length();
} int jishu_2=;
for(int i=;i<;i++){
int a;
if(k_2[i]>=''&&k_2[i]<='')
a=k_2[i]-'';
else
a=k_2[i]-'A'+; int n[]={};
int f=;
while(a){
n[f]=a%;
a=a/;
f++;
}
miyao[jishu_2*]=n[];
miyao[jishu_2*+]=n[];
miyao[jishu_2*+]=n[];
miyao[jishu_2*+]=n[];
jishu_2++;
} //置换选择1
int IP_1[]={, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , }; //16次左移对应的位数
int weiyi[]={,,,,,,,,,,,,,,,};
// 置换选择2 秘钥56->48位压缩
int IP_2[]={, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , }; //S盒
int s[][]=
{
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
},
{
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,
}
}; //P置换
int P[]={, , , , , , , ,
, , , , , , , ,
, , , , , , , ,
, , , , , , , }; //1初始置换 //1.1 64密文进行逆置换分左右
int miwen_1[];
int l[],r[];
for (int i=;i<;i++){
miwen_1[i]=miwen[IP[i]-];
}
for(int i=;i<;i++){
r[i]=miwen_1[i];
l[i]=miwen_1[i+];
}
cout<<"密文逆置换 =";
for(int i=;i<;i++){
if(i%==) cout<<" ";
cout<<miwen_1[i];
}
cout<<endl; //1.2 56位秘钥初始置换分左右 cout<<"秘钥初始置换k0 =";
for(int i=;i<;i++){
if(i%==) cout<<" ";
cout<<miyao[i];
}
cout<<endl; int ml_0[],mr_0[];
int miyao_0[];
for(int i=;i<;i++){
miyao_0[i]=miyao[IP_1[i]-];
}
for(int i=;i<;i++){
ml_0[i]=miyao_0[i];
mr_0[i]=miyao_0[i+];
} // 因为加密过程一共左移了28位,回到了原来的位置,
//所以我们要先将原始秘钥左移一次。 int ml[],mr[];
for(int j=;j<;j++){
ml[j]=ml_0[(j++)%];
mr[j]=mr_0[(j++)%];
} //2.循环解密
for (int i=;i<;i++){ cout<<"-------------------------------第"<<i+<<"轮循环解密-----------------------------------" <<endl; //2.1密文左右交换
int new_l[],new_r[];
for(int j=;j<;j++){
new_r[j]=l[j];
} //2.2 左边32位拓展变换成48位
for(int j=;j<;j++){
new_l[j]=new_r[E[j]-];
} //2.3 左右秘钥 右移 int new_ml[],new_mr[];
for(int j=;j<;j++){
new_ml[j]=ml[(j+-weiyi[-i])%];
new_mr[j]=mr[(j+-weiyi[-i])%];
} //2.4 重新合并成56位的秘钥
int miyao_1[];
for(int j=;j<;j++){
miyao_1[j]=new_ml[j];
miyao_1[j+]=new_mr[j];
}
//2.5 IP_2 56位秘钥压缩成48位的秘钥
int k[];
for (int j=;j<;j++){
k[j]=miyao_1[IP_2[j]-];
} cout<<"k"<<-i<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<k[j];
}
cout<<endl; cout<<"L"<<-i<<" "<<"=";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_l[j];
}
cout<<endl; //2.6 2.2和2.5XOR
int new_l2[];
for(int j=;j<;j++){
new_l2[j]=new_l[j] ^ k[j];
} cout<<"Li^ki "<<"=";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_l2[j];
}
cout<<endl; //2.7 s盒
int new_l3[];
int b1,b2,b3,b4,b5,b6;
int m=;
for(int j=;j<;j++){
int row = ((new_l2[j*])<<)+(new_l2[j*+]); //第1,6位组成行号
int col = ((new_l2[j*+])<<)+((new_l2[j*+])<<)+((new_l2[j*+])<<)+(new_l2[j*+]); //第2,3,4,5位组成列号
//找到s盒对应的数
int a=s[j][*row+col]; //转成对应的2进制
int n[]={};
int f=;
while(a){
n[f]=a%;
a=a/;
f++;
} new_l3[m*]=n[];
new_l3[m*+]=n[];
new_l3[m*+]=n[];
new_l3[m*+]=n[];
m++;
} cout<<"第"<<-i<<"轮s盒"<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_l3[j];
}
cout<<endl; //2.8 P置换
int new_l4[];
for(int j=;j<;j++){
new_l4[j]=new_l3[P[j]-];
} cout<<"P置换 "<<"=";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_l4[j];
}
cout<<endl; //2.9 密文右边32位和2.8 new_l4[32] XOR
int new_l5[];
for(int j=;j<;j++){
new_l5[j]=r[j] ^ new_l4[j];
//更新左右明文,以便于下次循环
r[j]=new_r[j];
l[j]=new_l5[j];
} cout<<"L"<<-i-<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_l5[j];
}
cout<<endl;
cout<<"R"<<-i-<<" =";
for(int j=;j<;j++){
if(j%==) cout<<" ";
cout<<new_r[j];
}
cout<<endl; //2.10 更新左右秘钥,以便于下次循环
for(int j=;j<;j++){
ml[j]=new_ml[j];
mr[j]=new_mr[j];
} cout<<"ml"<<-i<<"=";
for(int j=;j<;j++){
cout<<ml[j];
}
cout<<endl;
cout<<"mr"<<-i<<"=";
for(int j=;j<;j++){
cout<<mr[j];
}
cout<<endl; cout<<"------------------------------------------------------------------------------"<<endl<<endl<<endl; } //3. 循环解密之后的密文miwen_1[64]
for(int i=;i<;i++){
miwen_1[i]=l[i];
miwen_1[i+]=r[i];
}
//4. 初始置换
int mingwen[];
for(int i=;i<;i++){
mingwen[IP[i]-]=miwen_1[i];
} //5.输出 cout<<"原密文为:";
for(int i=;i<;i++){
if(i%==) cout<<endl;
cout<<miwen[i];
}
cout<<endl<<endl;
cout<<"秘钥为:";
for(int i=;i<;i++){
if(i%==) cout<<endl;
cout<<miyao[i];
}
cout<<endl<<endl;
cout<<"明文为:";
for(int i=;i<;i++){
if(i%==) cout<<endl;
cout<<mingwen[i];
}
//是否将明文转换成十六进制?(Y or N) cout<<endl<<endl<<"是否将明文转换成十六进制?(Y or N):";
char YN;
cin>>YN;
cout<<endl<<"十六进制明表示:";
if(YN=='Y'){
for(int i=;i<;i++){
int q=mingwen[i*]*+mingwen[i*+]*+mingwen[i*+]*+mingwen[i*+];
if(q>= && q<= ){
cout<<q;
}
else{
cout<<hex<<uppercase<<q;
}
}
}
cout<<endl<<endl<<"!!!!皆大欢喜,普天同庆!!!!"<<endl;
return ;
} int main(){ while(){
int number;
cout<<"===欢迎来到DES加密算法系统==="<<endl;
cout<<" Author:十七"<<endl;
cout<<"目前支持的操作有:"<<endl;
cout<<" 1.加密操作;"<<endl;
cout<<" 2.解密操作;"<<endl;
cout<<" 0.退出。"<<endl;
cout<<"============================="<<endl;
cout<<"请选择您项进行的操作:";
cin>>number;
cout<<endl; if(number==){
jiami();
}
else if(number==){
jiemi();
}
else{
cout<<"您已成功退出系统,如果您对本次服务感到满意,欢迎五星好评^_^"<<endl;
return ;
}
}
return ;
}

运行结果截图:

DES加密解密算法C++实现-LMLPHP

......

DES加密解密算法C++实现-LMLPHP

DES加密解密算法C++实现-LMLPHP

......

DES加密解密算法C++实现-LMLPHP

05-11 16:05