信息安全技术 || DES加密算法

算法原理:

DES算法是一种对称加密算法,以64位为分组对数据加密,加密和解密用的是同一个算法。它的密钥长度是56位(因为每个第8 位都用作奇偶校验),密钥可以是任意的56位的数,而且可以任意时候改变。其中有极少数被认为是易破解的弱密钥,但是很容易避开它们不用。所以保密性依赖于密钥。

其基本流程如下:

首先要生成一套加密密钥,从用户处取得一个64位长的密码口令,然后通过等分、移位、选取和迭代形成一套16个加密密钥,分别供每一轮运算中使用。

DES对64位(bit)的明文分组M进行操作,M经过一个初始置换IP,置换成m0。将m0明文分成左半部分和右半部分m0 = (L0,R0),各32位长。然后进行16轮完全相同的运算(迭代),这些运算被称为函数f,在每一轮运算过程中数据与相应的密钥结合。

在每一轮中,密钥位移位,然后再从密钥的56位中选出48位。通过一个扩展置换将数据的右半部分扩展成48位,并通过一个异或操作替代成新的48位数据,再将其压缩置换成32位。这四步运算构成了函数f。然后,通过另一个异或运算,函数f的输出与左半部分结合,其结果成为新的右半部分,原来的右半部分成为新的左半部分。将该操作重复16次。

经过16轮迭代后,左,右半部分合在一起经过一个末置换(数据整理),这样就完成了加密过程。

DES的解密和加密唯一的不同是密钥的次序相反。如果各轮加密密钥分别是K1,K2,K3…K16,那么解密密钥就是K16,K15,K14…K1。

总体结构:

Feistel结构:

在这里插入图片描述

模块分解:

由给定的密钥获取16个子密钥:

  • 对K 的56个非校验位实行置换PC-1,得到C0D0,其中C0 和D0 分别由PC-1 置换后的前28位和后28位组成。
//密钥置换表
const int 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
};
  • 计算Ci = LSi(Ci-1) 和Di = LSi(Di-1)
//密钥置换时每轮移动的位数
const int shift[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};

//将密钥的前后部分分别左移
void leftShift(unsigned char K[28], int shift) {
	unsigned char ar[28];
	memcpy(ar, K, 28);
	int i = 0;
	for(i = 27; i >= 0; i--)
	{
		if(i-shift < 0)
			K[i] = ar[i-shift+28];
		else
			K[i] = ar[i-shift];
	}
}
  • 对56位的CiDi 实行PC-2 压缩置换,得到48位的Ki 。i= i+1。
//压缩置换表
const int 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
};
  • 之后的16轮按照以上步骤重复得到16个子密钥

1540735921928

  • 总的算法实现:
void getMyKey(unsigned char key[64], unsigned char subKey[16][48]) {
	//去掉奇偶检验位的key
	unsigned char key_f[56];
	//左半部分
	unsigned char L[28];
	//右半部分
	unsigned char R[28];
	int i = 0, j = 0;
	unsigned char C_key[48];
	for (i = 0; i < 56; i++) {
		key_f[55-i] = key[64-PC_1[i]];
	}
	//生成左右子密钥
	for (i = 0; i < 16; i++) {
		for (j = 0; j < 28; j++) {
			L[j] = key_f[j+28];
		}
		for (j = 0; j < 28; j++) {
			R[j] = key_f[j];
		}
		//左移
		leftShift(L, shift[i]);
		leftShift(R, shift[i]);
		//压缩置换
		
		for (j = 0; j < 28; j++) {
			key_f[j+28] = L[j];
		}
		for (j = 0; j < 28; j++) {
			key_f[j] = R[j];
		}
		for (j = 0; j < 48; j++) {
			C_key[47-j] = key_f[56-PC_2[j]];
		}
		memcpy(subKey[i], C_key, 48);
	}
}

初始IP置换:

给定64位明文块M,通过一个固定的初始置换IP来重排M中的二进制位,得到二进制串M0 = IP(M) = L0 R0,这里L0 和R0分别是M0 的前32位和后32位。

//IP置换表
const int 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
	int i = 0;
	unsigned char FirstKey[64];
	for (i = 0; i < 64; i++) {
		FirstKey[63-i] = word[64-IP[i]];
	}

迭代T

  • 根据L0R0 按下述规则进行16次迭代,即

Li=Ri1,Ri=Li1(XOR)f(Ri1,Ki),i=1..16 Li= Ri-1, Ri= Li-1 (XOR) f(Ri-1, Ki), i= 1 .. 16

  • 这里 (XOR) 是32位二进制串按位异或运算,f 是输出32位的Feistel 轮函数;
  • 16个长度为48位的子密钥Ki(i= 1 … 16) 由密钥K生成;
  • 16次迭代后得到L16R16 ;
  • 左右交换输出R16L16 。

在这里插入图片描述

//16轮迭代
unsigned char L[32];
	unsigned char R[32];
	for(i = 0; i < 32; i++) {
		L[i] = FirstKey[i+32];
	}
	for(i = 0; i < 32; i++) {
		R[i] = FirstKey[i];
	}
	for (i = 0; i < 16; i++) {
		unsigned char tmp_R[32];
		unsigned char R1[32];
		memcpy(tmp_R, R, 32);
		f(R, subKey[i], R1);
		XOR_32(L, R1);
		memcpy(R, L, 32);
		memcpy(L, tmp_R, 32);
	}

轮询函数Feistel:

  • 将长度为32位的串Ri-1作E-扩展,成为48位的串E(Ri-1);
//扩展置换表
const int 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
};
  • 将E(Ri-1) 和长度为48位的子密钥Ki作48位二进制串按位异或运算,Ki 由密钥K生成;
void XOR_48(unsigned char ER[48], unsigned char K[48]) {
	int i = 0;
	for (i = 0; i < 48; i++) {
		ER[i] ^= K[i];
	}
}
  • 将上面得到的结果平均分成8个分组(每个分组长度6位),各个分 组分别经过8个不同的S-盒进行6-4 转换,得到8个长度分别为4 位的分组;
// S盒置换表,每个S盒是4x16的置换表
const int S_BOX[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}  
	} 
};
  • 将上面得到的分组结果顺序连接得到长度为32位的串;
  • 再将上面的32位串经过P-置换,得到的结果作为轮函数f(Ri-1, Ki) 的最终32位输出。
//P盒置换表
const int 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 
};
  • 整个函数的实现过程:
//轮询函数Feistel
void f(unsigned char R[32], unsigned char K[48], unsigned char R1[32]) {
	//将32位进行扩展成48位
	unsigned char ER[48];
	int i = 0, j = 0;
	for (i = 0; i < 48; i++) {
		ER[47-i] = R[32-E[i]];
	}
	//扩展后的R和K异或
	XOR_48(ER, K);
	//轮询S_Box,将6->4
	for (i = 0; i < 48; i+=6, j+=4) {
		int m = (ER[47-i]<<1) +ER[47-i-5];
		int n = (ER[47-i-1]<<3) + (ER[47-i-2]<<2) + (ER[47-i-3]<<1) + ER[47-i-4];
		int num = S_BOX[i/6][m][n];
		//printf("%d  %d", m, n);
		R1[31-j] = (num&0x08) >> 3;
		R1[31-j-1] = (num&0x04) >> 2;
		R1[31-j-2] = (num&0x02) >> 1;
		R1[31-j-3] = num&0x01;
	}
	//P置换, 32->32
	unsigned char ar[32];
	memcpy(ar, R1, 32);
	for (i = 0; i < 32; i++) {
		R1[31-i] = ar[32-P[i]];
	}
}

加密:

void EnCode(unsigned char word[64], unsigned char cipher[64], unsigned char subKey[16][48]) {
	//初始置换IP
	int i = 0;
	unsigned char FirstKey[64];
	for (i = 0; i < 64; i++) {
		FirstKey[63-i] = word[64-IP[i]];
	}
	//16轮迭代
	unsigned char L[32];
	unsigned char R[32];
	for(i = 0; i < 32; i++) {
		L[i] = FirstKey[i+32];
	}
	for(i = 0; i < 32; i++) {
		R[i] = FirstKey[i];
	}
	for (i = 0; i < 16; i++) {
		unsigned char tmp_R[32];
		unsigned char R1[32];
		memcpy(tmp_R, R, 32);
		f(R, subKey[i], R1);
		XOR_32(L, R1);
		memcpy(R, L, 32);
		memcpy(L, tmp_R, 32);
	}
	//合并迭代后的L和R,并进行IP-1的置换
	unsigned char LR[64];
	for (i = 0; i < 32; i++) {
		LR[i] = L[i];
	}
	for (i = 0; i < 32; i++) {
		LR[i+32] = R[i];
	}
	unsigned char tmp_LR[64];
	memcpy(tmp_LR, LR, 64);
	for (i = 0; i < 64; i++) {
		cipher[63-i] = tmp_LR[64-IP_1[i]];
	}
}

解密:

  • 分析所有的代替、置换、异或和循环移动过程,获得一个非常 有用的性质:DES 的加密和解密可使用相同的算法和密钥。
  • DES 的过程设计使得用相同的函数来加密或解密每个分组成为 可能。加解密过程中使用由同一个密钥K 经过相同的子密钥生 成算法得到的子密钥序列,唯一不同之处是加解密过程中子密 钥的调度次序恰好相反。
    • 加密过程的子密钥按(K1 K2 … K15 K16) 次序调度
    • 解密过程的子密钥按(K16 K15 … K2 K1) 次序调度

代码实现:

void DeCode(unsigned char cipher[64], unsigned char word[64], unsigned char subKey[16][48]) {
	//初始置换IP
	int i = 0;
	unsigned char FirstKey[64];
	for (i = 0; i < 64; i++) {
		FirstKey[63-i] = cipher[64-IP[i]];
	}
	//16轮迭代
	unsigned char L[32];
	unsigned char R[32];
	for(i = 0; i < 32; i++) {
		L[i] = FirstKey[i+32];
	}
	for(i = 0; i < 32; i++) {
		R[i] = FirstKey[i];
	}
	for (i = 0; i < 16; i++) {
		unsigned char tmp_R[32];
		unsigned char R1[32];
		memcpy(tmp_R, R, 32);
		f(R, subKey[15-i], R1);
		XOR_32(L, R1);
		memcpy(R, L, 32);
		memcpy(L, tmp_R, 32);
	}
	//合并迭代后的L和R,并进行IP-1的置换
	unsigned char LR[64];
	for (i = 0; i < 32; i++) {
		LR[i] = L[i];
	}
	for (i = 0; i < 32; i++) {
		LR[i+32] = R[i];
	}
	unsigned char tmp_LR[64];
	memcpy(tmp_LR, LR, 64);
	for (i = 0; i < 64; i++) {
		word[63-i] = tmp_LR[64-IP_1[i]];
	}
}

数据结构:

使用unsigned char数组来存储二进制位串

通过c语言的移位操作符实现二进制位串的逻辑运算。

通过以下代码实现字符串与二进制位串的转换:

//字符转换成二进制符号 
void CharToBit(unsigned char c, unsigned char bit[8]){  
    int i;  
    for(i = 0; i < 8; i++){  
        *(bit+i) = (c>>i) & 1;  
    }   
}  
    
  
//将长度为8的字符串转为64位二进制  
void StringToBits(unsigned char c[8], unsigned char bit[64]){  
    int i;  
    for(i = 0; i < 8; i++){          
        CharToBit(*(c + i), bit + (i<<3));  
    } 
}  
  
//二进制转换成字节  
void BitToChar(unsigned char bit[8], unsigned char *c){  
    int i;  
    for(i = 0; i < 8; i++){  
        *c |= *(bit + i) << i;  
    }   
}

//将二进制字节转为长度为8的字符串  
void BitsToString(unsigned char bit[64], unsigned char c[8]){  
    int i;
    memset(c,0,8);  
    for(i = 0; i < 8; i++){  
        BitToChar(bit + (i<<3), c + i);  
    }  
}

二进制位串的一些逻辑运算如下:

void XOR_32(unsigned char ER[32], unsigned char K[32]) {
	int i = 0;
	for (i = 0; i < 32; i++) {
		ER[i] ^= K[i];
	}
}
int m = (ER[47-i]<<1) +ER[47-i-5];
		int n = (ER[47-i-1]<<3) + (ER[47-i-2]<<2) + (ER[47-i-3]<<1) + ER[47-i-4];
		int num = S_BOX[i/6][m][n];
		//printf("%d  %d", m, n);
		R1[31-j] = (num&0x08) >> 3;
		R1[31-j-1] = (num&0x04) >> 2;
		R1[31-j-2] = (num&0x02) >> 1;
		R1[31-j-3] = num&0x01;

编译运行结果:

用于测试的main文件:

  • 将加密的文字存到一个记事本里,解密过程读取记事本中的位串进行解码。
#include <stdio.h>
#include <stdlib.h>
#include "desCode.h"
#include <time.h>
int main () {
	printf("please input the words you want to encode: \n\n");
	//加密的文字
	unsigned char word[8];
	memset(word, 0, 8);
	unsigned char *s_word = (unsigned char*)malloc(8*sizeof(unsigned char));
	gets(s_word);
	strcpy(word, s_word);
	free(s_word);
	unsigned char words[64];
	StringToBits(word, words);
	//密钥
	unsigned char c_key[8];
	//取1~9的随机数作为密钥
	srand(time(0));
	for (int i = 0; i < 8; i++) {
		int key_1 = rand() % 10;
		c_key[i] = key_1 + '0';
	}
	printf("\nthe key help us to encode is: \n");
	for (int i = 0; i < 8; i++) {
		printf("%c", c_key[i]);
	}
	printf("\n\n");
	//unsigned char *c_key = "12342a78";
	unsigned char key[64];
	StringToBits(c_key, key);
	//根据提供的密钥生成16个子密钥
	unsigned char subKey[16][48];
	getMyKey(key, subKey);
	//加密过程
	unsigned char cipher[64];
	EnCode(words, cipher, subKey);
	printf("After encode, your cipher is: \n");
	for (int i = 0; i < 64; i++) {
		printf("%c", cipher[i]);
	}
	printf("\n\n");
	//将密码写到密码本里面
	FILE *fp;
	fp = fopen("code.txt","w+t");
	fwrite(cipher, sizeof(char), 64, fp);
	fclose(fp);
	//将密码本的密码读取出
	unsigned char cipher_read[64];
	fp = fopen("code.txt","r+t");
	fread(cipher_read, sizeof(char), 64, fp);
	//fscanf(fp, "%s", cipher_read);
	fclose(fp);
	//解密过程
	unsigned char decode[64];
	unsigned char answer[8];
	DeCode(cipher_read, decode, subKey);
	BitsToString(decode, answer);
	printf("After decode, your answer is: \n");
	for (int i = 0; i < 8; i++) {
		printf("%c", answer[i]);
	}
	printf("\n");
	return 0;
}

运行结果:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章