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

📄 des.cpp

📁 des加密算法 des加密算法 des加密算法 解密
💻 CPP
字号:
/*
*   DES加解密算法-演示程序
*   作者:谢振哲(XieZhenZhe)
*	原创代码 @ 2009.03
*/
#include<iostream>
#include<string>
#include<bitset>
#include<time.h>
using namespace std;

#define BYTE unsigned char

class CDES
{
public:

	bool DesEnter(const BYTE *input, BYTE *output, BYTE *key, int datalen, int mode);

private:
	//初始换位表IP
	static int ip[64];
	
	//逆初始换位表IP_1
	static int ip_1[64];

	//16组子密钥
	BYTE nkey[16][6];

	//缩小换位表1
	static int pc_1[56];

	//缩小换位表2
	static int pc_2[48];

	//16轮密钥移位次数
	static int MoveBit[16];

	//放大换位表 
	static int e[48];

	//P置换
	static int p[32];

	//S盒置换表
	static BYTE s[8][4][16];

	bool chkKey(BYTE *key);

	void BinToByte(BYTE in[8], BYTE out[64], int len);

	void ByteToBin(BYTE in[64], BYTE out[8], int len);

	//加密函数
	void Des_En(BYTE *output);

	//加密函数
	void Des_De(BYTE *output);

	//最后一个分组补全64bit
	void AddTo64bit(int datalen, BYTE *output);

	//初始IP变换
	void IPRound(BYTE *output);

	//初始IP逆变换
	void IPRound_1(BYTE *output);

	//生成子密钥
	void getRoundKey(BYTE *key);

	//密钥转换选择PC_1
	void KeyPC_1(BYTE *key, BYTE *out);

	//密钥转换选择PC_2
	void KeyPC_2(int S, BYTE *L, BYTE *R, BYTE *out);

	//扩展转换E
	void Expand(BYTE *in, BYTE *out);

	//S盒替换及P盒置换
	void SPReplace(BYTE *in, BYTE *out);
};


//初始换位表IP
int CDES::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_1
int CDES::ip_1[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 
    };

//缩小换位表1
int CDES::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
	}; 

//缩小换位表2
int CDES::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
	}; 

//放大换位表 
int CDES::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
    };

//P置换
int CDES::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盒
BYTE CDES::s[8][4][16] =
{ 
            {
                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 
            }, 
            {  
                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 
            },  
            {
                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
            },
            { 
                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 
            }, 
            {
                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, 
            }, 
            { 
                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 
            },
            {
                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 
            },   
            { 
                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 
            }
}; 

//16次循环左移对应的左移位数 
int CDES::MoveBit[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};

void CDES::ByteToBin(BYTE in[], BYTE out[], int len)
{ 
	int i;
	for(i=0;i<len;i++)
	{
		out[i] = (in[i << 3] << 7)  + (in[(i << 3) + 1] << 6) + 
			(in[(i << 3) + 2] << 5) + (in[(i << 3) + 3] << 4) + 
			(in[(i << 3) + 4] << 3) + (in[(i << 3) + 5] << 2) + 
			(in[(i << 3) + 6] << 1) + in[(i << 3) + 7];
	}
}

void CDES::BinToByte(BYTE in[], BYTE out[], int len)
{ 
	int i,j;
	for(i=0;i<len;i++)
		for(j=0;j<8;j++)
			out[(i<<3)+j] = (in[i]>>(7-j)) & 0x01;
}

/*  
*	DesEnter 入口函数
*		input 输入字节头指针
*		output 输出字节头指针
*		key 64bit密钥
*		datalen 数据长度(单位字节,解密时须为8的倍数)
*		mode 0加密,1解密,2验证密钥,3生成密钥
*	返回值:true成功,false失败
*/
bool CDES::DesEnter(const BYTE *input, BYTE *output, BYTE *key, int datalen, int mode)
{
	//检验基本参数是否正确
	if(((mode==1 || mode==2) && (input==0 || output==0 || key==0 || datalen<=0 )) || mode<0 || mode>3 || mode==1 && datalen%8!=0) return false;

	//mode==0 加密
	if(mode==0)
	{
		//if(!chkKey(key)) return false;

		int i;
		for(i=0;i<datalen;i++)
			output[i]=input[i];

		getRoundKey(key);

		AddTo64bit(datalen, output);
		datalen=datalen/8*8+8;

		for(i=0;i<datalen;i+=8)
		{
			Des_En(output+i);
		}

		return true;
	}
	else if(mode==1) //解密
	{
		//if(!chkKey(key)) return false;

		int i;
		for(i=0;i<datalen;i++)
			output[i]=input[i];

		getRoundKey(key);

		for(i=0;i<datalen;i+=8)
			Des_De(output+i);

		return true;
	}
	else if(mode==2)
	{
		if(!chkKey(key)) return false;
		else return true;
	}
	else if(mode==3)
	{
		int i;
		srand((unsigned int)time(NULL));
		BYTE newkey[64];
		do
		{
			for(i=0;i<64;i++)
				newkey[i]=rand()%2;
		}while(!chkKey(newkey));
		ByteToBin(newkey,output,8);
		return true;
	}
	else return false;
}


/*
*	chkKey 检验密钥正确性
*		key 64bit密钥
*	返回值:true 正确密钥,false 错误密钥
*/
bool CDES::chkKey(unsigned char *key)
{
	int i;
	for(i=0;i<8;i++)
	{
		bitset<8> OneKeyByte(key[i]);
		if(OneKeyByte.count()%2==0) return false;
	}
	return true;
}

/*
*	Des_En DES加密函数
*		output 输出字节流
*/
void CDES::Des_En(unsigned char output[8])
{
	//IP
	IPRound(output);
	
	BYTE L[17][4];
	BYTE R[17][4];
	BYTE tmp_after_E[6];
	BYTE tmp_after_sp[4];
	int i,j;

	//L0,R0
	for(i=0;i<4;i++)
	{
		L[0][i]=output[i];
		R[0][i]=output[i+4];
	}

	//16轮迭代
	for(i=1;i<=16;i++)
	{
		for(j=0;j<4;j++)
			L[i][j]=R[i-1][j];

		//E扩展
		Expand(R[i-1],tmp_after_E);

		//异或本轮子密钥
		for(j=0;j<6;j++)
		{
			tmp_after_E[j]^=nkey[i-1][j];
		}

		SPReplace(tmp_after_E,tmp_after_sp);

		for(j=0;j<4;j++)
			R[i][j]=L[i-1][j]^tmp_after_sp[j];
	}
	
	//合成一个串
	for(i=0;i<4;i++)
	{
		output[i]=R[16][i];
		output[i+4]=L[16][i];
	}

	//IP_1
	IPRound_1(output);
}

/*
*	Des_De DES解密函数
*		output 输出字节流
*/
void CDES::Des_De(unsigned char output[8])
{
	//IP
	IPRound(output);
	
	BYTE L[17][4];
	BYTE R[17][4];
	BYTE tmp_after_E[6];
	BYTE tmp_after_sp[4];
	int i,j;

	//L0,R0
	for(i=0;i<4;i++)
	{
		L[16][i]=output[i];
		R[16][i]=output[i+4];
	}

	//16轮迭代
	for(i=15;i>=0;i--)
	{
		for(j=0;j<4;j++)
			L[i][j]=R[i+1][j];

		//E扩展
		Expand(R[i+1],tmp_after_E);

		//异或本轮子密钥
		for(j=0;j<6;j++)
		{
			tmp_after_E[j]^=nkey[i][j];
		}

		SPReplace(tmp_after_E,tmp_after_sp);

		for(j=0;j<4;j++)
			R[i][j]=L[i+1][j]^tmp_after_sp[j];
	}
	
	//合成一个串
	for(i=0;i<4;i++)
	{
		output[i]=R[0][i];
		output[i+4]=L[0][i];
	}

	//IP_1
	IPRound_1(output);
}

/*
*	AdedTo64bit 将输入流复制到输出流,并补全最后一组为64bit
*		datalen 数据长度(字节)
*		input 输入字节流
*		output 输出字节流
*/
void CDES::AddTo64bit(int datalen, unsigned char *output)
{
	int i;

	//不足64bit补全
	if(datalen%8)
	{
		for(i=datalen;i<(datalen/8)*8+8;i++)
			output[i]=0x08-(datalen%8);
	}
	else
	{
		for(i=datalen;i<datalen+8;i++)
			output[i]=0x08;
	}
}

/*
*	IPRound 初始IP变换
*		output 变换内容
*/
void CDES::IPRound(unsigned char *output)
{
	BYTE tmp_old_byte[64];
	BYTE tmp_new_byte[64];
	int i;
	BinToByte(output,tmp_old_byte,8);
	for(i=0;i<64;i++)
	{
		tmp_new_byte[i]=tmp_old_byte[ip[i]-1];
	}
	ByteToBin(tmp_new_byte,output,8);
}

/*
*	IPRound_1 初始IP逆变换
*		output 变换内容
*/
void CDES::IPRound_1(unsigned char *output)
{
	BYTE tmp_old_byte[64];
	BYTE tmp_new_byte[64];
	int i;
	BinToByte(output,tmp_old_byte,8);
	for(i=0;i<64;i++)
	{
		tmp_new_byte[i]=tmp_old_byte[ip_1[i]-1];
	}
	ByteToBin(tmp_new_byte,output,8);
}

void CDES::getRoundKey(unsigned char *key)
{
	BYTE L[56]; //实际长度28,复制一倍
	BYTE R[56];
	int LS,RS; //L移位次数,R移位次数
	BYTE tmp_after_pc_1[56];

	//PC_1
	KeyPC_1(key,tmp_after_pc_1);

	//构造L0,R0
	int i;
	for(i=0;i<28;i++)
	{
		L[i+28]=L[i]=tmp_after_pc_1[i];
		R[i+28]=R[i]=tmp_after_pc_1[i+28];
	}

	//初始置0
	LS=RS=0;

	//16轮变换
	for(i=0;i<16;i++)
	{
		//换位
		LS+=MoveBit[i];
		RS+=MoveBit[i];

		KeyPC_2(LS,L,R,nkey[i]);
	}
}

/*
*	KeyPC_1 密钥转换选择PC_1
*		key 输入密钥64bit
*		out 输出密钥56bit,Byte表示
*/
void CDES::KeyPC_1(BYTE key[8], BYTE out[56])
{
	BYTE tmp_old_byte[64];
	BinToByte(key,tmp_old_byte,8);
	int i;
	for(i=0;i<56;i++)
	{
		out[i]=tmp_old_byte[pc_1[i]-1];
	}
}

/*
*	KeyPC_2 密钥转换选择PC_2
*		S 移位次数
*		L 左28位
*		R 右28位
*		out 输出48位bit
*/
void CDES::KeyPC_2(int S, unsigned char *L, unsigned char *R, unsigned char out[6])
{
	int i;
	BYTE tmp_after_pc_2[48];
	for(i=0;i<48;i++)
	{
		if(pc_2[i]>28)
			tmp_after_pc_2[i]=R[pc_2[i]-28-1+S];
		else
			tmp_after_pc_2[i]=L[pc_2[i]-1+S];
	}
	ByteToBin(tmp_after_pc_2,out,6);
}

/*
*	Expand 扩展转换E
*		in 输入32bit
*		out 输出48bit
*/
void CDES::Expand(BYTE *in, BYTE *out)
{
	BYTE tmp_before_E[32];
	BYTE tmp_after_E[48];

	BinToByte(in,tmp_before_E,4);

	int i;
	for(i=0;i<48;i++)
	{
		tmp_after_E[i]=tmp_before_E[e[i]-1];
	}

	ByteToBin(tmp_after_E,out,6);
}

void CDES::SPReplace(unsigned char in[6], unsigned char out[4])
{
	BYTE tmp_before_replace[48];
	BYTE tmp_sout_bit[8];
	BYTE tmp_before_p[32];
	BYTE tmp_after_p[32];

	BinToByte(in,tmp_before_replace,6);

	int i,row,col;
	for(i=0;i<8;i++) 
	{ 
		//0、5位为row,1、2、3、4位为col
		row=(tmp_before_replace[i*6]<<1)+(tmp_before_replace[i*6+5]);
		col=(tmp_before_replace[i*6+1]<<3)+(tmp_before_replace[i*6+2]<<2)+(tmp_before_replace[i*6+3]<<1)+(tmp_before_replace[i*6+4]); 
		tmp_sout_bit[i]=s[i][row][col];
	}
	tmp_sout_bit[0]=(tmp_sout_bit[0]<<4)+tmp_sout_bit[1]; 
	tmp_sout_bit[1]=(tmp_sout_bit[2]<<4)+tmp_sout_bit[3]; 
	tmp_sout_bit[2]=(tmp_sout_bit[4]<<4)+tmp_sout_bit[5];
	tmp_sout_bit[3]=(tmp_sout_bit[6]<<4)+tmp_sout_bit[7];
	
	BinToByte(tmp_sout_bit,tmp_before_p,4);
	for(i=0;i<32;i++)
	{
		tmp_after_p[i]=tmp_before_p[p[i]-1];
	}
	ByteToBin(tmp_after_p,out,4);
}



void about()
{
	system("CLS");
	printf("*************************************                      ZZ\n                                           ╭︿︿︿╮    ZZ\n");
	printf("    Copyright (C) 谢振哲 @2009.03          {/ ︿︿ /}  z\n");
	printf("        DES加解密算法-演示程序              ( (oo) )\n                                             ︶︶︶ \n");
	printf("*************************************\n\n\n");
}

int main()
{
	char input_key[20],output_cipher[20];
	int i;
	BYTE key[10],plain[20],cipher[20],decode[20];
	CDES des;

loop:
	about();
	memset(input_key,0,sizeof(input_key));
	memset(output_cipher,0,sizeof(output_cipher));
	memset(key,0,sizeof(key));
	memset(plain,0,sizeof(plain));
	memset(cipher,0,sizeof(cipher));
	memset(decode,0,sizeof(decode));

	printf("Input Key: ");
	for(i=0;i<8;i++)
		scanf("%2X",&key[i]);
	
	printf("Input PlainText: ");
	scanf(" %s",plain);

	printf("CipherText: ");
	des.DesEnter(plain,cipher,key,8,0);
	for(i=0;i<8;i++)
		printf("%02X",cipher[i]);
	printf("\n");

	printf("Decryption: ");
	des.DesEnter(cipher,decode,key,8,1);
	printf("%s\n",decode);

	printf("\n继续输入吗?[y继续]");
	scanf(" %s",plain);
	if(plain[0]=='y') goto loop;

	return 0;
}

⌨️ 快捷键说明

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