| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 482 人关注过本帖
标题:关于DES的C/C++编程问题求高手
只看楼主 加入收藏
jzmjzm
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
关于DES的C/C++编程问题求高手
DES加密解密程序,编译通过!但无论输入如何改变输出恒不变。纠结。。。。。,程序如下。求指点
//DES加密解密程序
#include<stdio.h>
#include<iostream.h>
void main(void)
{
    int i,j,k;
    k=0;
    int mingwen[8],miyao[8],ch;
    int mingwenB[64],key[64],str[8];
    int IP[64]={58,50,42,34,26,18,10,2,
                60,52,44,36,28,20,12,4,
                62,54,46,38,30,22,14,6,
                64,56,48,40,32,24,16,8,
                57,49,41,33,25,17,9,1,
                59,51,43,35,27,19,11,3,
                61,53,45,37,29,21,13,5,
                63,55,47,39,31,23,15,7};//IP初始变换表
    int IPR[64]={40,8,48,16,56,24,64,32,
                 39,7,47,15,55,23,63,31,
                 38,6,46,14,54,22,62,30,
                 37,5,45,13,53,21,61,29,
                 36,4,44,12,52,20,60,28,
                 35,3,43,11,51,19,59,27,
                 34,2,42,10,50,18,58,26,
                 33,1,41,9,49,17,57,25};//IP逆变换表
    int E[48]={ 32,1,2,3,4,5,
                4,5,6,7,8,9,
                8,9,10,11,12,13,
                12,13,14,15,16,17,
                16,17,18,19,20,21,
                20,21,22,23,24,25,
                24,25,26,27,28,29,
                28,29,30,31,32,1};//E扩展置换表
    int p[32]={16,7,20,21,
                29,12,28,17,
                1,15,23,26,
                5,18,31,10,
                2,8,24,14,
                32,27,3,9,
                19,13,30,6,
                22,11,4,25};//作用于S盒输出的P置换
    int pc_1[56]={57,49,41,33,25,17,9,
                  1,58,50,42,34,26,18,
                  10,2,59,51,43,35,27,
                  19,11,3,60,52,44,36,
                  63,55,47,39,31,23,15,
                  7,62,54,46,38,30,22,
                  14,6,61,53,45,37,29,
                  21,13,5,28,20,12,4};//密钥初始置换表
    int pc_2[48]={14,17,11,24,1,5,
                  3,28,15,6,21,10,
                  23,19,12,4,26,8,
                  16,7,27,20,13,2,
                  41,52,31,37,47,55,
                  30,40,51,45,33,48,
                  44,49,39,56,34,53,
                  46,42,50,36,29,32};//密钥压缩变换
                          //s盒
    int sBox[8][4][16]={
                           //S1
                14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
                0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
                4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
                15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,

                           //S2
                15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
                3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
                0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
                13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
                           //S3

                10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
                13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
                13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
                1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,
                          //S4

                7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
                13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
                10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
                3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,
                          //S5
                2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
                14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
                4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
                11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,

                          //S6
                12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
                10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
                9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
                4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,
                          //S7

                4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
                13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
                1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
                6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,
                          //S8

                13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
                1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
                7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
                2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11};
                /*数据的加密过程*/
   cout<<"请输入8位待加密数据:"<<endl;
   for(i=0;i<8;i++)
       cin>>mingwen[i];
   cout<<"请输入8位密钥key:"<<endl;
   for(j=0;j<8;j++)
       cin>>miyao[j];
   /*明文转化为64比特二进制*/
   for(i=0;i<8;i++)
   {
       ch=mingwen[i];
       for(j=0;j<8;j++)
       {
           str[j]=ch%2;
           ch=ch/2;
       }
       for(j=7;j<=0;j--)
       {
           mingwenB[k]=str[j];
           k++;
       }
   }
   k=0;
   /*将密钥转化为64比特二进制*/

for(i=0;i<8;i++)
   {
       ch=miyao[i];
       for(j=0;j<8;j++)
       {
           str[j]=ch%2;
           ch=ch/2;
       }
       for(j=7;j<=0;j--)
       {
         key[k]=str[j];
           k++;
       }
   }
k=0;
/*初始置换IP*/
int afterIP[64];
for(j=0;j<64;j++)
afterIP[j]=mingwenB[IP[j]-1];
/*进行明文的第0次分组得到l0和r0*/
int l0[32],r0[32];
for(i=0;i<32;i++)
l0[i]=afterIP[i];
for(j=0,i=32;i<64;i++,j++)
r0[j]=afterIP[i];
/*16轮子密钥的生成*/
int afterpc_1[56];
for(i=0;i<56;i++)
afterpc_1[i]=key[pc_1[i]-1];//pc-1
/*密钥的第0次分组*/
int c0[28],d0[28];
for(i=0;i<28;i++)
c0[i]=afterpc_1[i];
for(j=0,i=28;i<56;i++,j++)
d0[j]=afterpc_1[i];
/*循环左移,第一次移一位*/
int c[16][28],d[16][28];
int Q,D;
Q=c0[0];
D=d0[0];
for(i=0;i<27;i++)
{
    c0[i]=c0[i+1];
    d0[i]=d0[i+1];
}
c0[27]=Q;
d0[27]=D;
for(i=0;i<28;i++)
{
    c[0][i]=c0[i];
    d[0][i]=d0[i];
 }
for(i=1;i<16;i++)
{
    if(i==1||i==8||i==15)//左移一位
    {
        int e,f;
        e=c[i][0];
        f=d[i][0];
        for(j=0;j<27;j++)
        {
        c[i][j]=c[i][j+1];
        d[i][j]=d[i][j+1];
        }
       c[i][27]=e;
       d[i][27]=f;
       }
     else
    {
         int g,h,m,s;
         g=c[i][0];
         h=c[i][1];
         m=d[i][0];
         s=d[i][1];
        for(j=0;j<26;j++)
        {
        c[i][j]=c[i][j+2];
        d[i][j]=d[i][j+2];
        }

       c[i][26]=g;
       d[i][26]=m;
       c[i][27]=h;
       d[i][27]=s;
    }
}
/*16轮c[i],d[i]合并*/
int beforekey[16][56];
for(i=0;i<16;i++)
{
    for(j=0;j<28;j++)
        beforekey[i][j]=c[i][j];
    for(k=0;k<28;k++,j++)
        beforekey[i][j]=d[i][k];
}
k=0;
/*pc-2置换*/
int keys[16][48];
for(i=0;i<16;i++)
{
    for(j=0;j<48;j++)
    keys[i][j]=beforekey[i][pc_2[j]-1];
}//生成16组密钥
/*生成l[1]和r[1]*/
int l[16][32],r[16][32];
for(i=0;i<32;i++)
{
 l[0][i]=l0[i];
 r[0][i]=r0[i];
}
int afterEr0[48],befores[48];
for(i=0;i<32;i++)//l[i]=r[i-1]
l[1][i]=r0[i];
for(i=0;i<48;i++)
afterEr0[i]=r0[E[i]-1];//E置换
for(i=0;i<48;i++)
{
    if(afterEr0[i]==keys[0][i])
    befores[i]=0;
    else
    befores[i]=1;
}
/*数据分组*/
int beforesbox[8][6];
for(i=0;i<8;i++)
{
    for(j=0;j<6;j++)
    {
      beforesbox[i][j]=befores[k];
      k++;
    }
}
k=0;
/*S盒变换*/
int aftersbox[8],a,b;
for(i=0;i<8;i++)
{
    a=beforesbox[i][0]+beforesbox[i][5]*2;
    b=beforesbox[i][1]+beforesbox[i][2]*2+beforesbox[i][3]*4+beforesbox[i][4]*8;
    aftersbox[i]=sBox[i][a][b];
}
/*S盒输出转换为32比特二进制*/
int beforep[32];
int temp,str1[4];
for(i=0;i<8;i++)
{
    temp=aftersbox[i];
    for(j=0;j<4;j++)
    {
        str1[j]=temp%2;
        temp=temp/2;
    }
    for(j=3;j>=0;j--)
    {
        beforep[k]=str1[j];
        k++;
    }
}
k=0;
/*P置换*/
int afterp[32];
for(i=0;i<32;i++)
afterp[i]=beforep[p[i]-1];
for(i=0;i<32;i++)
{
    r[1][i]=afterp[i]+l[0][i];
    if(r[1][i]==2)
        r[1][i]=0;
}
/*现已经生成l[1][32]和r[1][32],下面进行十五轮循环,生成其余l[i][32]和r[i][32]*/
    int afterEr[16][48];
    int n;
    for(i=2;i<16;i++)
    {
        for(j=0;j<32;j++)
            l[i][j]=r[i-1][j];
        for(j=0;j<48;j++)        //E置换
            afterEr[i][j]=r[i][E[j]-1];
        for(j=0;j<48;j++)        //与子密钥进行异或
        {
            if(afterEr[i][j]==keys[i][j])
                befores[j]=0;
            else
                befores[j]=1;
        }
   
    for(j=0;j<8;j++)    //生成进入S盒前的8个数组,分别进入8个S盒
    {
            for(n=0;n<6;n++)
            {
                beforesbox[j][n]=befores[k];
                k++;
    }
    }
        for(j=0;j<8;j++)        //进入S盒,从8个S盒中生成8个数
        {
        a=beforesbox[j][0]+beforesbox[j][5]*2;
        b=beforesbox[j][1]+beforesbox[j][2]*2+beforesbox[j][3]*4+beforesbox[j][4]*8;
        aftersbox[j]=sBox[j][a][b];
        }
        k=0;
        for(j=0;j<8;j++)    //S盒出来的8个十进制数转换成32位的二进制数
        {
            temp=aftersbox[j];
            for(n=0;n<4;n++)
            {
                str1[n]=temp%2;
                temp=temp/2;
            }
            for(n=3;n>=0;n--)
            {
                beforep[k]=str1[n];
                k++;
        }
        }
        for(j=0;j<32;j++)
            afterp[j]=beforep[p[j]-1];
        for(j=0;j<32;j++)
        {
            if(afterp[j]==l[i-1][j])
                r[i][j]=0;
            else
                r[i][j]=1;}

            k=0;
    }
/*通过十五轮循环,最后得到l[15][32]和r[15][32],合并之后进行IP逆置换生成密文*/
    int beforeoutput[64],output[64];
     for(i=0;i<32;i++)        //数据合并,R[15][32]在前,L[15][32]在后面
        beforeoutput[i]=r[15][i];
    for(i=32,j=0;i<64;i++,j++)
        beforeoutput[i]=l[15][j];
    for(i=0;i<64;i++)
        output[i]=beforeoutput[IPR[i]-1];//得到64位密文
cout<<"由输入的明文和密钥所得的加密结果为:"<<endl;
for(i=0;i<64;i++)
 cout<<output[i];
 cout<<endl;
/*以下为解密过程:已知64比特密文,直接进行IP置换*/
int afterIP2[64];
for(j=0;j<64;j++)
afterIP2[j]=output[p[i]-1];
/*进行密文的第0次分组*/
int l20[32],r20[32];
for(i=0;i<32;i++)
l20[i]=afterIP2[i];
for(j=0,i=32;i<64;i++,j++)
r20[j]=afterIP2[i];
/*以下为生成l2[1],r2[1]*/
int l2[16][32],r2[16][32];
for(i=0;i<32;i++)
{
    l2[0][i]=l20[i];
    r2[0][i]=r20[i];
}
int afterEr20[48],befores2[48];
for(i=0;i<32;i++)//l[i]=r[i-1]
l2[1][i]=r20[i];
for(i=0;i<48;i++)
afterEr20[i]=r20[E[i]-1];//E置换
for(k=0;k<48;k++)
{
    if(afterEr20[k]==keys[15][k])
    befores2[k]=0;
    else
    befores2[k]=1;
}
/*数据分组*/
int beforesbox2[8][6];
for(i=0;i<8;i++)
{
    for(j=0;j<6;j++)
    {
      beforesbox2[i][j]=befores2[k];
      k++;
    }
}
k=0;
/*S盒变换*/
int aftersbox2[8],a2,b2;
 for(i=0;i<8;i++)
 {
    a2=beforesbox2[i][0]+beforesbox2[i][5]*2;
    b2=beforesbox2[i][1]+beforesbox2[i][2]*2+beforesbox2[i][3]*4+beforesbox2[i][4]*8;
    aftersbox2[i]=sBox[i][a2][b2];
}
/*S盒输出转换为32比特二进制*/
 int beforep2[32],temp2,str12[4];
 for(i=0;i<8;i++)
{
    temp2=aftersbox2[i];
    for(j=0;j<4;j++)
    {
        str12[j]=temp2%2;
        temp2=temp2/2;
    }
    for(j=3;j>=0;j--)
    {
        beforep2[k]=str12[j];
        k++;
    }
}
k=0;
/*P置换*/
int afterp2[32];
for(i=0;i<32;i++)
afterp2[i]=beforep2[p[i]-1];
for(i=0;i<32;i++)
{
    r2[1][i]=afterp2[i]+l2[0][i];
    if(r2[1][i]==2)
        r2[1][i]=0;
}
/*已经生成l2[1][32]和r2[1][32],下面进行十五轮循环,生成其余l[i][32]和r[i][32]*/
    int afterEr2[16][48];
    for(i=1;i<16;i++)
    {
        for(j=0;j<32;j++)
            l2[i][j]=r2[i-1][j];
        for(j=0;j<48;j++)        //E置换
        afterEr2[i][j]=r2[i][E[j]-1];
        for(j=0;j<48;j++)        //与子密钥进行异或
        {
            if(afterEr2[i][j]==keys[15-i][j])
                befores2[j]=0;
            else
                befores2[j]=1;
        }
    for(j=0;j<8;j++)    //生成进入S盒前的8个数组,分别进入8个S盒
    {      
        for(n=0;n<6;n++)
            {
                beforesbox2[j][n]=befores2[k];
                k++;
            }
    }
        for(j=0;j<8;j++)        //进入S盒,从8个S盒中生成8个数
        {
            a2=beforesbox2[j][0]+beforesbox2[j][5]*2;
            b2=beforesbox2[j][1]+beforesbox2[j][2]*2+beforesbox2[j][3]*4+beforesbox2[j][4]*8;
            aftersbox2[j]=sBox[j][a2][b2];
        }
        k=0;
        for(j=0;j<8;j++)    //S盒出来的8个十进制数转换成32位的二进制数
        {   
          temp2=aftersbox2[j];
            for(n=0;n<4;n++)
            {
                str12[n]=temp2%2;
                temp2=temp2/2;
            }
            for(n=3;n>=0;n--)
            {
                beforep2[k]=str12[n];
                k++;
            }
        }
             k=0;
        for(j=0;j<32;j++)
            afterp2[j]=beforep2[p[j]-1];
        for(j=0;j<32;j++)
        {
            if(afterp2[j]==l2[i-1][j])
                r2[i][j]=0;
            else
                r2[i][j]=1;
        }
    }
   
/*通过十五轮循环,最后得到l2[15][32]和r2[15][32],合并之后进行IP逆置换生成明文*/
    int beforeoutput2[64],output2[64];
        for(i=0;i<32;i++)        //数据合并,R[15][32]在前,L[15][32]在后面
        beforeoutput2[i]=r2[15][i];
    for(i=32,j=0;i<64;i++,j++)
        beforeoutput2[i]=l2[15][j];
    for(i=0;i<64;i++)
        output2[i]=beforeoutput2[IPR[i]-1];//得到64位密文
cout<<"由密文和解密密钥所得明文为:"<<endl;
for(i=0;i<64;i++)
cout<<output2[i];
cout<<endl;
}
搜索更多相关主题的帖子: include 加密 如何 
2011-04-10 09:43
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
  又是代码轰炸

                                         
===========深入<----------------->浅出============
2011-04-10 09:45
我是传奇
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:88
专家分:109
注 册:2011-3-21
收藏
得分:0 
重写吧,
图片附件: 游客没有浏览图片的权限,请 登录注册
2011-04-10 11:21
我是传奇
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:88
专家分:109
注 册:2011-3-21
收藏
得分:0 
没那么长
2011-04-10 11:22
jzmjzm
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-9
收藏
得分:0 
回复 3楼 我是传奇
大哥,等的就是你啊!求代码!!!!!求代码!!!
2011-04-12 16:16
我是传奇
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:88
专家分:109
注 册:2011-3-21
收藏
得分:20 
给个邮箱发给你
2011-04-12 22:26
那又怎样
Rank: 2
等 级:论坛游民
帖 子:22
专家分:58
注 册:2011-4-11
收藏
得分:0 
晕,一片黑!!!
2011-04-14 16:44
快速回复:关于DES的C/C++编程问题求高手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020636 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved