2 lsjjesus lsjjesus 于 2013.09.13 12:21 提问

c++ des加密出现的问题

`这是DESCode.h的代码
#include
#include
#include

#ifndef DESCODE
#define DESCODE

#define DES_ENCODE 0
#define DES_DECODE 1

class DESCODE
{
private:
char Key[16][48];
char PassWord[8];

    int InitKey();
    int IPChange(char *Source,char *L,char *R);
    int _IPChange(char *Dest,char *L,char *R);
    int CalcLR(char *L,char *R,int bCodeFlag);
    int GetBit(char *szSource,char *Array);
    int f(char *L,char *R,int nCount);
    int SBox(char *E);
public:
    int Des( char *Dest, char *Source, int bCodeFlag);
    // bCodeFlag = 0 EnCode, bCodeFlag = 1 DeCode
    int SetKey(char *szPassWord);

};

#endif

`这是DESCode.的代码

#include "DESCode.h"

int DESCODE::Des( char Dest, char *Source, int bCodeFlag)
/*des加/解密主程序,bCodeFlag=0 EnCode, bCodeFlag=1 DeCode
/
{
char L[32],R[32];

if (!InitKey())        /*初始化密钥*/
    return 0;
IPChange(Source,L,R);      /*IP置换*/
CalcLR(L,R,bCodeFlag);      /*L与R进行16次迭代*/
_IPChange(Dest,L,R);      /*IP_1置换*/

return 1;

}

int DESCODE::InitKey() /*初始化密钥*/
{
int pc1[] = {
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 pc2[] = {
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};
int lShift[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
char c[29],d[29],k[64];
int i,j,nCount;

GetBit(PassWord,k);      /*将密码转为64位,存在K数组中*/
for (i=0;i<28;i++)      /*pc-1置换*/
{
    c[i]=k[pc1[i]-1];
    d[i]=k[pc1[i+28]-1];
}
for (i=0;i<16;i++)      /*16次迭代*/
{
    for (j=0;j<lShift[i];j++)   /*左移运算*/
    {
        c[28]=c[0];
        d[28]=d[0];
        for(nCount=0;nCount<27;nCount++)
        {
            c[nCount]=c[nCount+1];
            d[nCount]=d[nCount+1];
        }
        c[27]=c[28];
        d[27]=d[28];
    }
    for (j=0;j<48;j++)     /*pc-2置换*/
        if ((pc2[j]-1)<28)
            Key[i][j]=c[pc2[j]-1];
        else
            Key[i][j]=d[pc2[j]-29];
}
return 1;

}

int DESCODE::IPChange(char szSource,char *L,char *R) /*IP置换/
{
int ip[] = {
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};
char m[64];
int nCount;

GetBit(szSource,m);      /*将要加密的数据转为64位,存在m数组中*/
for (nCount=0;nCount<32;nCount++)  /*ip置换*/
{
    *(L+nCount)=m[ip[nCount]-1];
    *(R+nCount)=m[ip[nCount+32]-1];
}
return 1;

}

int DESCODE::CalcLR(char L,char *R,int bCodeFlag) /*L、R迭代/
{
int nCount;
char cTemp[32];

if (!bCodeFlag)        /*判断是加密还是解密,0为加密,1为解密*/
    for (nCount=0;nCount<16;nCount++)  /*进行16次迭代*/
        f(L,R,nCount);      
else
    for (nCount=15;nCount>=0;nCount--)  /*进行16次迭代*/
        f(L,R,nCount);
for (nCount=0;nCount<32;nCount++)   /*交换L、R*/
{
    cTemp[nCount]=*(R+nCount);
    *(R+nCount)=*(L+nCount);
    *(L+nCount)=cTemp[nCount];
}
return 1;

}

int DESCODE::f(char *L,char *R,int nCount)
{
int e[] = {
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};
int p[] = {
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};
char cTemp[32],E[48];
int i;

for (i=0;i<32;i++)      /*保存R*/
    cTemp[i]=*(R+i);
for (i=0;i<48;i++)      /*将R进行e置换并与密钥异或*/
    E[i]=R[e[i]-1]^Key[nCount][i];
SBox(E);        /*s盒置换*/
for (i=0;i<32;i++)
    R[i]=E[p[i]-1]^L[i];    /*与L异或*/
for (i=0;i<32;i++)
    *(L+i)=cTemp[i];     /*将原来的R变为L*/
return 1;

}

int DESCODE::SBox(char E) /*s盒/
{
int sBox[8][64] = {
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, /*s1*/
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, /*s2*/
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, //s3
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, /*s4*/
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, /*s5*/
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, /*s6*/
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, /*s7*/
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}; /*s8*/
char s[6],cTemp[32];
int i,j,nCount,iRow,iColumn;

for (nCount=0;nCount<8;nCount++)    /*分成8组*/
{
    for (i=0;i<6;i++)       /*从E中取出6位放入s中*/
        s[i]=*(E+nCount*6+i);     
    iRow=s[0]*2+s[5];       /*计算行与列*/
    iColumn=s[1]*8+s[2]*4+s[3]*2+s[4];
    j=sBox[nCount][iRow*16+iColumn];
    j<<=4;
    for (i=0;i<4;i++)
    {
        cTemp[nCount*4+i]=j&128?1:0;     /*将s盒里的结果转为4比特输出*/
        j<<=1;
    }
}
for (i=0;i<32;i++)
    *(E+i)=cTemp[i];
return 1;

}

int DESCODE::_IPChange(char Dest,char *L,char *R) /*IP_1置换/
{
int ip_1[] = {
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};
char c[64];
int i,j;
//char TDest=Dest;
for (i=0;i<64;i++) /
进行IP_1置换*/
{
if (ip_1[i]-1 c[i]=*(L+ip_1[i]-1);
else
c[i]=*(R+ip_1[i]-33);
}
// for (i=0;i //{
// *(TDest+i)='\0';
// for (j=0;j // if (c[i*8+j])
// *(TDest+i)+=(int) pow((double)2,7-j);
//}
//memset(Dest, 0, 64>>3);
for(int i=0; i Dest[i>>3] |= Dest[i]<<(7 - i&7);

return 1;

}
int DESCODE::GetBit(char szSource,char *Array) /将输入变成64位输出*/
{
int i,j;
int nTemp;

for (i=0;i<8;i++)       
{
    nTemp=szSource[i];
    for (j=0;j<8;j++)
    {
        *(Array+i*8+j)=(nTemp&128?1:0);
        nTemp<<=1;
    }
}
return 1;

}
/*******************************************************************/
/*
函 数 名 称: BitToByte
功 能 描 述: 把Bit转化为Byte流
参 数 说 明: Out: 输出的BYTE流[in][out]
In: 输入的Bit流[in]
bits: Bit流的长度[in]

返回值 说明: void
作 者: 邹德强
更 新 日 期: 2003.12.19
/*******************************************************************/
static void BitToByte(char *Out, const bool *In, int bits)
{
memset(Out, 0, bits>>3);
for(int i=0; i Out[i>>3] |= In[i]<<(7 - i&7);
}

int DESCODE::SetKey(char szPassWord) /设置密码变量*/
{
int i;

for (i=0;i<8;i++)
    PassWord[i]=*(szPassWord+i);
return 1;

}
int main()
{

DESCODE de;
char dest;
de.SetKey("20100708AABBAABB");
de.Des(&dest,"1234567812345678",0);
int i=0;

//8D1899AC052E81B2

}

但是运行调试的时候,出现这样问题:`CSDN移动问答

CSDN移动问答

得到的值应该是8D1899AC052E81B2;
CSDN移动问答
而我调试得到值是-52,无论是加密还是解释密,请问各位高手,c++是这样调试的吗,我引用出问题了吗?该如何正确引用?小弟在此感谢不尽。

1个回答

oyljerry
oyljerry   Ds   Rxr 2015.01.05 18:45
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!