⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 desencrypt.cpp

📁 抄书本的DES对称加密算法
💻 CPP
字号:
// DesEncrypt.cpp: implementation of the CDesEncrypt class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Des.h"
#include "DesEncrypt.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDesEncrypt::CDesEncrypt()
{

}

CDesEncrypt::~CDesEncrypt()
{

}

bool CDesEncrypt::DesGo(char *Out, char *In, long datalen, const char *Key, int keylen, bool Type)
{
	if(!(Out && In && Key && (datalen=(datalen+7)&0xfffffff8))) 
		return false;
	SetKey(Key, keylen);
	if(!DES3)
	{   
		//1次DES
		for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
			DES(Out, In, &SubKey[0], Type);
	} 
	else
	{   
		//3次DES 加密:加(key0)-解(key1)-加(key0);解密:解(key0)-加(key1)-解(key0)
		for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8) 
		{
			DES(Out, In,  &SubKey[0], Type);
			DES(Out, Out, &SubKey[1], !Type);
			DES(Out, Out, &SubKey[0], Type);
		}
	}
	return true;
}

void CDesEncrypt::SetKey(const char *Key, int len)
{
	memset(DesKey, 0, 16);
	memcpy(DesKey, Key, len>16?16:len);
	SetSubKey(&SubKey[0], &DesKey[0]);
	DES3 = len>8 ? (SetSubKey(&SubKey[1], &DesKey[8]), true) : false;
}

void CDesEncrypt::DES(char Out[], char In[], const PSubKey pSubKey, bool Type)
{
	static bool M[64], temp[32], *Li=&M[0], *Ri=&M[32];
    ByteToBit(M, In, 64);
    Transform(M, M, INITable, 64);
    if(Type == TRUE)
	{
        for(int i=0; i<16; ++i)
		{
            memcpy(temp, Ri, 32);
            Ffunc(Ri, (*pSubKey)[i]);
            Xor(Ri, Li, 32);
            memcpy(Li, temp, 32);
        }
    }
	else
	{
        for(int i=15; i>=0; --i) 
		{
            memcpy(temp, Li, 32);
            Ffunc(Li, (*pSubKey)[i]);
            Xor(Li, Ri, 32);
            memcpy(Ri, temp, 32);
        }
	}
    Transform(M, M, IPRTable, 64);
    BitToByte(Out, M, 64);
}

void CDesEncrypt::SetSubKey(PSubKey pSubKey, const char Key[])
{
	static bool K[64], *KL=&K[0], *KR=&K[28];
    ByteToBit(K, Key, 64);
    Transform(K, K, PC1_Table, 56);
    for(int i=0; i<16; ++i)
	{
        RotateL(KL, 28, LOOP_Table[i]);
        RotateL(KR, 28, LOOP_Table[i]);
        Transform((*pSubKey)[i], K, PC2_Table, 48);
    }
}

void CDesEncrypt::Ffunc(bool In[], const bool Ki[])
{
	static bool MR[48];
    Transform(MR, In, EXTTable, 48);
    Xor(MR, Ki, 48);
    Sfunc(In, MR);
    Transform(In, In, PBOXTable, 32);
}

void CDesEncrypt::Sfunc(bool Out[], const bool In[])
{
	for(char i=0,j,k; i<8; ++i,In+=6,Out+=4)
	{
        j = (In[0]<<1) + In[5];
        k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
		ByteToBit(Out, &SBox[i][j][k], 4);
    }
}

void CDesEncrypt::Transform(bool *Out, bool *In, const char *Table, int len)
{
	for(int i=0; i<len; ++i)
	{
        Temp[i] = In[ Table[i]-1];
	}
    memcpy(Out, Temp, len);
}

void CDesEncrypt::Xor(bool *InA, const bool *InB, int len)
{
	for(int i=0; i<len; ++i)
	{
        InA[i] ^= InB[i];
	}
}

void CDesEncrypt::RotateL(bool *In, int len, int loop)
{
	memcpy(Temp, In, loop);
    memcpy(In, In+loop, len-loop);
    memcpy(In+len-loop, Temp, loop);
}

void CDesEncrypt::ByteToBit(bool *Out, const char *In, int bits)
{
	for(int i=0; i<bits; ++i)
	{
        Out[i] = (In[i>>3]>>(i&7)) & 1;
	}
}

void CDesEncrypt::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);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -