s-des密码算法实现

实验二 S-DES算法实现

 

一、S-DES算法分析

1、Simplified DES方案,简称S-DES方案。它是一个供教学而非安全的加密算法,它与DES的特性和结构类似,但参数小。

加密算法涉及五个函数:
(1)初始置换IP(initial permutation)
(2)复合函数fk1,它是由密钥K确定的,具有置换和代换的运算。      

(3)置换函数SW
(4)复合函数fk2
(5)初始置换IP的逆置换IP-1

下面是S-DES算法的框图。

 

 

 

 

 

 

(1) S-DES的密钥生成:
设10bit的密钥为( k1,k2,…,k10 )
几个重要的定义:

P10(k1,k2,…,k10)=(k3,k5,k2,k7,k4,k10,k1,k9,k8,k6)   

 

P8= (k1,k2,…,k10)=(k6,k3,k7,k4,k8,k5,k10,k9 ) 

LS-1为循环左移1位:abcdeàbcdea

LS-2为循环左移2位:  abcdeàcdeab

 

 

 

(2)    S-DES的加密运算:

  初始置换用IP函数:

   IP=      1 2 3 4 5 6 7 8 

     2 6 3 1 4 8 5 7

 

末端算法的置换为IP的逆置换:
IP-1=     1 2 3 4 5 6 7 8

           4 1 3 5 7 2 8 6

    

 

E/P为扩展置换,将4位1,2,3,4扩展置换如下:

E/P   4  1  2  3  2  3  4  1 

 

2个S盒的定义为:

 

 

S盒按下述规则运算:

将第1和第4的输入比特做为2- bit数,指示为S盒的一个行;将第2和第3的输入比特做为S盒的一个列。如此确定为S盒矩阵的(i,j)数。

例如:(P0,0, P0,3)=(00),并且(P0,1,P0,2)=(1 0)

确定了S0中的第0行2列(0,2)的系数为3,记为(1 1)输出。

由S0, S1输出4-bit经置换


 P4:      1        2      3    4

           2        4      3  1

它的输出就是F函数的输出。

 

 

总结如下: 

P10={3,5,2,7,4,10,1,9,8,6}; 

P8={6,3,7,4,8,5,10,9}; 

P4={2,4,3,1}; 

IP={2,6,3,1,4,8,5,7};

IP-1={4,1,3,5,7,2,8,6};

EP={4,1,2,3,2,3,4,1};

 

 

 

代码如下:

#include<iostream>
#include<string>
using namespace std;
int K1=0;
int K2=0;
static int P10MAX=10;
static int P8MAX=10;
static int P4MAX=4;
static int IPMAX=8;
static int IPIMAX=8;
static int EPMAX=4;
static int P10[]={3,5,2,7,4,10,1,9,8,6,'\0'};
static int P8[]={6,3,7,4,8,5,10,9,'\0'};
static int P4[]={2,4,3,1,'\0'};
static int IP[]={2,6,3,1,4,8,5,7,'\0'};
static int IPI[]={4,1,3,5,7,2,8,6,'\0'};
static int EP[]={4,1,2,3,2,3,4,1,'\0'};
static int S0[4][4]={
	{1,0,3,2},
	{3,2,1,0},
	{0,2,1,3},
	{3,1,3,2},
};
static int S1[4][4]={
	{0,1,2,3},
	{2,0,1,3},
	{3,0,1,0},
	{2,1,0,3},
};

int Getlength(int p[])
{
	int i=0;
	for(;p[i]!='\0';++i);
	return i;
}
int BinaryToDecimal(string binary){
	int result=0;
	for(int i=0;i<binary.length();i++){
		result=2*result+(binary[i]-'0');
	}
	return result;
}
void printBin(int x,int n)
{
	int mask=1<<(n-1);
	while(mask>0){
		((x&mask)==0)?printf("0"):printf("1");
		mask>>=1;
	}
	cout<<endl;
}
int Permute(int inum,int p[],int pmax)
{
	int result=0;
	int length=Getlength(p);
	for(int i=0;i<length;++i)
	{
		result<<=1;
		result=(inum>>(pmax-p[i]))&1;
	}
	return result;
}
int F(int R,int K){
	int t = Permute(R,EP,EPMAX)^K;
	int t0 = (t>>4)&0xf;
	int t1 = t&0xf;
	t0 = S0[((t0&0x8)>>2)|(t0&1)][(t0>>1)&0x3];
	t1 = S1[((t0&0x8)>>2)|(t1&1)][(t1>>1)&0x3];
	t = Permute((t0<<2)|t1,P4,P4MAX);
	return t;
}

int fk(int m,int k){
	int l = (m>>4)&0xf;
	int r = m&0xf;
	return((l^F(r,k))<<4)|r;
}

int SW(int x){
	return((x&0xf)<<4)|((x>>4)&0xf);
}
int encrypt(int m){
	m=Permute(m,IP,IPMAX);
	m=fk(m,K1);
	m=SW(m);
	m=fk(m,K2);
	m=Permute(m,IPI,IPMAX);
	return m;
}

int decrypt(int m){
	m=Permute(m,IP,IPMAX);
	m=fk(m,K2);
	m=SW(m);
	m=fk(m,K1);
	m=Permute(m,IPI,IPMAX);
	return m;
}

void SDES(int K){
	int t1=0,t2=0;
	K=Permute(K,P10,P10MAX);
	t1=(K>>5)&&0x1f;
	t2=K & 0x1f;
	t1=((t1&0xf)<<1)|((t1&0x10)>>4);
	t2=((t2&0xf)<<1)|((t2 & 0x10)>>4);
	K1=Permute((t1<<5)|t2,P8,P8MAX);
	t1=((t1 & 0x07)<<2)|((t1 & 0x18)>>3);
	t2=((t2 & 0x07)<<2)|((t2 & 0x18)>>3);
	K2=Permute((t1<<5)|t2,P8,P8MAX);
	cout<<"K1 为:"<<endl;
	cout<<K1<<endl;
	cout<<"K2 为:"<<endl;
	cout<<K2<<endl;
}

int main()
{   cout<<"***************输入均为二进制***************"<<endl;
loop: cout<<"***************请选择:加密为1 解密为0***************"<<endl;
	int i;
cin>>i;
if(i==0)
{

	cout<<"***************解密:***************"<<endl;
	 string plaintext;
 cout<<"***************请输入密文:***************"<<endl;
	 cin>>plaintext;

	 string key;
 cout<<"***************请输入密钥:***************"<<endl;
 cin>>key;
 SDES(BinaryToDecimal(key));
 cout<<"***************得到明文:***************"<<endl;
 printBin(encrypt(BinaryToDecimal(plaintext)),8);
 cout<<"***************是否跳出 跳出为0 继续为1***************"<<endl;
 int j;
		 cin>>j;
		 switch(j)
		 {
		 case 0: {cout<<"***************欢迎使用s-des程序***************"<<endl;
				 cout<<"***************czy***************";
				 return 0;}
		 case 1: goto loop;
		 }
}
  if (i==1)
	{
		cout<<"***************加密:***************"<<endl;
	 string ciphertext;
		cout<<"***************请输入明文:***************"<<endl;
		 cin>>ciphertext;
	 cout<<"***************请输入密钥:***************"<<endl;
	 string key;
	 cin>>key;
	 SDES(BinaryToDecimal(key));
		cout<<"***************得到密文:***************"<<endl;
		 printBin(decrypt(BinaryToDecimal(ciphertext)),8);
		 cout<<"***************是否跳出 跳出为0 继续为1***************"<<endl;
		 int j;
		 cin>>j;
		 switch(j)
		 {
		 case 0: {cout<<"***************欢迎使用s-des程序***************"<<endl;
			 cout<<"***************czy***************";return 0;}
		 case 1: goto loop;
		 }
	}
}



  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值