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

📄 sdeschange.cpp

📁 而SDES是简单的des算法版本
💻 CPP
字号:
// Sdeschange.cpp: implementation of the Sdeschange class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "sdesandrsa.h"
#include "Sdeschange.h"

#include<math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

int s0[4][4]={1,0,3,2,
3,2,1,0,
0,2,1,3,
3,1,3,2};
int s1[4][4]={0,1,2,3,
2,0,1,3,
3,0,1,0,
2,1,0,3};	


Sdeschange::Sdeschange()
{

}

Sdeschange::~Sdeschange()
{

}

void Sdeschange::changemes(int m, bool M[])
{
	for(int i=0;i<8;i++)
		M[i]=0;
	for( i=7;m!=0;i--)
	{
		if(m%2)
		{
			M[i]=1;
		}
		m=m/2;
	}

}

int Sdeschange::changemes1(int length, bool M[])
{
	int n=0;
	for(int i=length-1;i>-1;i--)
	{
		if(M[i])
		{
			n=n+M[i]*(int)pow(2,length-i-1);
		}
	}
	return n;
}

void Sdeschange::changekey(int key, bool k[])
{
	for(int i=0;i<10;i++)
		k[i]=0;
	for( i=9;key!=0;i--)
	{

		if(key%2)
		{
			k[i]=1;
		}
		key=key/2;
	}
}

void Sdeschange::k1fun(bool k[], bool k1[])
{
	bool pkey2[10]={0};
	bool temp[10]={0};
	pkey2[6]=k[0];pkey2[2]=k[1];
	pkey2[0]=k[2];pkey2[4]=k[3];
	pkey2[1]=k[4];pkey2[9]=k[5];
	pkey2[3]=k[6];pkey2[8]=k[7];
	pkey2[7]=k[8];pkey2[5]=k[9];//p10移位
	
	temp[4]=pkey2[0];temp[0]=pkey2[1];
	temp[1]=pkey2[2];temp[2]=pkey2[3];
	temp[3]=pkey2[4];temp[9]=pkey2[5];
	temp[5]=pkey2[6];temp[6]=pkey2[7];
	temp[7]=pkey2[8];temp[8]=pkey2[9];//LS1操作
	
	k1[0]=temp[5];
	k1[1]=temp[2];
	k1[2]=temp[6];
	k1[3]=temp[3];
	k1[4]=temp[7];
	k1[5]=temp[4];
	k1[6]=temp[9];
	k1[7]=temp[8];//产生k1
	

}

void Sdeschange::k2fun(bool k[],bool k2[])
{
	bool pkey2[10]={0};
	bool temp[10]={0};
	pkey2[6]=k[0];pkey2[2]=k[1];
	pkey2[0]=k[2];pkey2[4]=k[3];
	pkey2[1]=k[4];pkey2[9]=k[5];
	pkey2[3]=k[6];pkey2[8]=k[7];
	pkey2[7]=k[8];pkey2[5]=k[9];//p10移位
	
	temp[2]=pkey2[0];temp[3]=pkey2[1];
	temp[4]=pkey2[2];temp[0]=pkey2[3];
	temp[1]=pkey2[4];temp[7]=pkey2[5];
	temp[8]=pkey2[6];temp[9]=pkey2[7];
	temp[5]=pkey2[8];temp[6]=pkey2[9];//LS3操作
	
	k2[0]=temp[5];k2[1]=temp[2];
	k2[2]=temp[6];k2[3]=temp[3];
	k2[4]=temp[7];k2[5]=temp[4];
	k2[6]=temp[9];k2[7]=temp[8];//产生k2
	

}

void Sdeschange::fun(bool IP[], bool key[])
{
	int a,b,c,d;
	bool EP[8]={0};
	bool P4[4]={0};
	EP[0]=IP[7];EP[1]=IP[4];
	EP[2]=IP[5];EP[3]=IP[6];
	EP[4]=IP[5];EP[5]=IP[6];
	EP[6]=IP[7];EP[7]=IP[4];//置换EP
	
	for(int k=0;k<8;k++)
	{
		EP[k]=EP[k]^key[k];
	}
	
	if(0==EP[0])
	{
		if(0==EP[3])
			a=0;
		else
			a=1;
	}
	else
	{
		if(0==EP[3])
			a=2;
		else
			a=3;
	}
	if(0==EP[1])
	{
		if(0==EP[2])
			b=0;
		else
			b=1;
	}
	else
	{
		if(0==EP[2])
			b=2;
		else
			b=3;
	}//确定so的行号a,列号b
	
	
	
	if(0==EP[4])
	{
		if(0==EP[7])
			c=0;
		else
			c=1;
	}
	else
	{
		if(0==EP[7])
			c=2;
		else
			c=3;
	}
	if(0==EP[5])
	{
		if(0==EP[6])
			d=0;
		else
			d=1;
	}
	else
	{
		if(0==EP[6])
			d=2;
		else
			d=3;
	}//确定s1的行号c,列号d
	switch(s0[a][b])
	{
	case 0:P4[0]=0;P4[3]=0;break;
	case 1:P4[0]=1;P4[3]=0;break;
	case 2:P4[0]=0;P4[3]=1;break;
	default:P4[0]=1;P4[3]=1;
	}
	switch(s1[c][d])
	{
	case 0:P4[1]=0;P4[2]=0;break;
	case 1:P4[1]=1;P4[2]=0;break;
	case 2:P4[1]=0;P4[2]=1;break;
	default:P4[1]=1;P4[2]=1;
	}
	for(k=0;k<4;k++)
	{
		IP[k]=P4[k]^IP[k];
	}

}

⌨️ 快捷键说明

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