📄 des.cpp
字号:
#include "StdAfx.h"
#include ".\des.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CDes::CDes(void)
{
m_nRound = 3;
}
CDes::CDes(int nRound)
{
if(nRound<1)
nRound = 1;
if(nRound>16)
nRound = 16;
m_nRound = nRound;
}
CDes::~CDes(void)
{
}
//初始置换
void CDes::IP(bool bDest[64],const bool bSrc[64])
{
for(int i = 0;i<64;i++)
{
bDest[i] = bSrc[i];//[IPTable[i/8][i%8]-1];
//bDest[i] = bSrc[IPTable[i/8][i%8]-1];
}
}
void CDes::IPInvert(bool bDest[64],const bool bSrc[64])
{
for(int i = 0;i<64;i++)
{
bDest[i] = bSrc[i];//[IPInvertTable[i/8][i%8]-1];
//bDest[i] = bSrc[IPInvertTable[i/8][i%8]-1];
}
}
void CDes::Fk(bool bDest[32],const bool bSrc[32],const bool bKey[48])
{
bool bTmp2[32];
bool bTmp[48];
//扩充置换
Expansion(bTmp,bSrc);
//异或
Xor(bTmp,bKey,48);
//代换/选择(S-Box)
S_BOX(bTmp2,bTmp);
//P置换
P(bDest,bTmp2);
}
//扩充置换
void CDes::Expansion(bool bDest[48],const bool bSrc[32])
{
for(int i = 0;i<48;i++)
{
bDest[i] = bSrc[ETable[i/6][i%6]-1];
}
}
void CDes::S_BOX(bool bDest[32],bool bSrc[48])
{
bool *p1 = bSrc;
bool *p2 = bDest;
//代换/选择(S-Box)
for(char i=0,j,k; i<8; ++i,p1+=6,p2+=4)
{
j = (p1[5]<<1) + p1[0];
k = (p1[4]<<3) + (p1[3]<<2) + (p1[2]<<1) + p1[1];
ByteToBit(p2, &SBOXTable[i][j][k],4);
}
}
void CDes::P(bool bDest[32],const bool bSrc[32])
{
for(int h = 0;h<32;h++)
{
//bDest[h] = bSrc[h];//[PTable[h/4][h%4]-1];
bDest[h] = bSrc[PTable[h/4][h%4]-1];
}
}
bool CDes::Encrypt(char *pDest,char *pSrc,const int nLen,char* pKey,const int nKeyLen)
{
for(long i=0,j = nLen>>3; i<j; ++i,pDest+=8,pSrc+=8)
Encrypt(pDest,pSrc,pKey);
return true;
}
bool CDes::Encrypt(char cry[8],const char plaintext[8],const char key[8])
{
bool bCry[64],bPlain[64],bKey[64];
ByteToBit(bPlain,plaintext,64);
ByteToBit(bKey,key,64);
bool bRet = Encrypt(bCry,bPlain,bKey);
BitToByte(cry,bCry,64);
return bRet;
}
bool CDes::Encrypt(bool bCryptograph[64],const bool bPlaintext[64],const bool bKey[64])
{
bool bTmpCry[64];
ProduceKey(bKey);
//IP置换
IP(bTmpCry,bPlaintext);
for(int i = 0;i<m_nRound;i++)
{
bool bSubKey[48];
GetSubKey(bSubKey,i);
bool bL[32],bR[32];
memcpy(bL,bTmpCry,32);
memcpy(bR,bTmpCry + 32,32);
//复杂函数
Fk(bR,bR,bSubKey);
//Ri = Li-1 XOR Fk(Ri-1,Ki);
Xor(bR,bL,32);
memcpy(bL,bTmpCry+32,32);
memcpy(bTmpCry,bL,32);
memcpy(bTmpCry+32,bR,32);
}
//IP逆
IPInvert(bCryptograph,bTmpCry);
return true;
}
bool CDes::Decode(char *pDest,char *pSrc,const int nLen,char* pKey,const int nKeyLen)
{
for(long i=0,j = nLen>>3; i<j; ++i,pDest+=8,pSrc+=8)
Decode(pDest,pSrc,pKey);
return true;
}
bool CDes::Decode(char plaintext[8],const char cry[8],const char key[8])
{
bool bCry[64],bPlain[64],bKey[64];
ByteToBit(bCry,cry,64);
ByteToBit(bKey,key,64);
bool bRet = Decode(bPlain,bCry,bKey);
BitToByte(plaintext,bPlain,64);
return bRet;
}
bool CDes::Decode(bool bPlaintext[64],const bool bCryptograph[64],const bool bKey[64])
{
bool bTmpPlain[64];
ProduceKey(bKey);
//IP置换
IP(bTmpPlain,bCryptograph);
TRACE0("IP:\n");
//DUMPBITSET(m_Plaintext[0]);
for(int i = m_nRound -1;i>= 0;i--)
{
TRACE1("第 %d 轮:\n\n",i);
bool bSubKey[48];
GetSubKey(bSubKey,i);
bool bL[32],bR[32];
memcpy(bL,bTmpPlain,32);
memcpy(bR,bTmpPlain + 32,32);
//复杂函数
Fk(bL,bL,bSubKey);
TRACE0("Fk:\n");
//DUMPBITSET(bitRNew);
//Ri = Li-1 XOR Fk(Ri-1,Ki);
Xor(bL,bR,32);
memcpy(bR,bTmpPlain,32);
memcpy(bTmpPlain,bL,32);
memcpy(bTmpPlain+32,bR,32);
TRACE0("结果:\n");
//DUMPBITSET(m_Plaintext[i]);
}
//IP逆
IPInvert(bPlaintext,bTmpPlain);
TRACE0("IPInvert:\n");
//DUMPBITSET(cryptograph);
return true;
}
void CDes::PC1(bool bDest[56],const bool bSrc[64])
{
for(int i = 0;i<56;i++)
{
bDest[i] = bSrc[PC1Table[i/7][i%7]-1];
}
}
void CDes::PC2(bool bDest[48],const bool bSrc[56])
{
for(int i = 0;i<48;i++)
{
bDest[i] = bSrc[PC2Table[i/8][i%8]-1];
}
}
void CDes::GetSubKey(bool bSubKey[48],const int nRound)
{
ASSERT((nRound>=0)&&(nRound<16));
memcpy(bSubKey,m_SubKey[nRound],48);
}
bool CDes::ProduceKey(const bool bKey[64])
{
bool bSubKeyTmp[56];
PC1(bSubKeyTmp,bKey);
for(int i = 0;i<m_nRound;i++)
{
RotateL(bSubKeyTmp,28,I_ShiftTable[i]);
RotateL(bSubKeyTmp+28,28,I_ShiftTable[i]);
PC2(m_SubKey[i],bSubKeyTmp);
}
return true;
}
void CDes::ByteToBit(bool *Out, const char *In, int bits)
{
for(int i=0; i<bits; ++i)
Out[i] = (In[i>>3]>>(i&7)) & 1;
}
void CDes::BitToByte(char *Out, const bool *In, int bits)
{
memset(Out, 0, bits>>3);
for(int i=0; i<bits; ++i)
Out[i>>3] |= In[i]<<(i&7);
}
void CDes::RotateL(bool *In, int len, int loop)
{
bool* bTmp = new bool[len];
memcpy(bTmp, In, len);//loop);
memcpy(In, bTmp+loop, len-loop);
memcpy(In+len-loop, bTmp, loop);
delete bTmp;
}
void CDes::Xor(bool *pbDest, const bool *pbSrc, int len)
{
for(int i=0; i<len; ++i)
pbDest[i] ^= pbSrc[i];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -