精华内容
下载资源
问答
  • 算法实现过程分析 来源 中国论文下载中心?[?03-03-18?14:30:00?]?作者本站会员?编辑丢?oO?丢?oO 1?处理密钥: 1.1?从用户处获得?64?位密钥(每第?8?位为校验位,为使密钥有正确奇偶校验,每个密钥要有 奇?数个1位(本文...
  • DES算法的实现

    2017-11-16 10:17:50
    从原理上分析了DES算法的整体过程,并给出了部分代码。
  • DES算法实现过程分析

    2009-06-08 14:28:49
    DES算法实现过程分析,仅供参考,适合对密码感兴趣
  • 介绍了DES算法原理,详细分析了子密钥生成、S盒和轮函数的设计。将DES算法采用资源优先方案,在轮函数内部设置流水线架构,...运用硬件描述语言Verilog,采用自顶向下的设计思想,在FPGA平台上实现了改进DES算法的功能。
  • DES算法c语言实现

    2017-10-07 21:25:17
    按照DES算法的过程,将它加密和解密的过程完全用c语言描述出来。
  • 未指定书签 3.AES 加密解密算法的组成部分 错误 !未指定书签 3.1 密钥部分 错误 !未指定书签 3.1.1AES 的 S 盒 错误 !未指定书签 3.1.2AES 的逆 S 盒 错误 !未指定书签 3.1.3 轮常量 错误 !未指定书签 3.1.4 密钥...
  • DES算法的Verilog实现

    2010-06-08 13:36:24
    DES算法的Verilog实现实现了DES的加密过程
  • DES算法 C语言实现

    千次阅读 2018-11-09 11:31:04
    DES算法C语言实现 算法概述 DES算法是一种对称块加密方法,加密和解密都使用一个长度为64位密钥。以64位为一个分组长度,对于每个分组,通过置换、Feistel轮函数、子密钥生成等一系列操作,输出一段64位密文。在...

    DES算法C语言实现

    算法概述

    DES算法是一种对称块加密方法,加密和解密都使用一个长度为64位的密钥。以64位为一个分组长度,对于每个分组,通过置换、Feistel轮函数、子密钥生成等一系列操作,输出一段64位的密文。在解密过程中,更换子密钥输入的顺序,即可解出原本的明文。

    总体结构

    1. 对输入的64位密文进行一次IP置换,得到L0R0L_0R_0
    2. 对IP置换的结果进行16次迭代,迭代的规则为:
      Li=R(i1),Ri=L(i1)f(R(i1),Ki) L_i = R_(i-1), R_i = L_(i-1) \bigoplus f(R_(i-1),K_i)
    3. 子密钥的生成以输入的64密钥为基础,经过PC1置换、循环移位和PC2置换等过程生成16个48位的字密钥,依次输入到Feistel函数中。
    4. 16次迭代完成后,将前32位与后32位交换位置,进行一次IP逆置换,得到最终的密文。

    代码实现

    • tables.h:定义了DES算法过程用到的所有数组
    • utility.h:所有模块函数的定义与实现
    • main.cpp:处理输入输出。
    #pragma once
    #include <stdio.h>
    
    int IP_Table[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 };
    
    int IP_Reverse_Table[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 };
    
    int Extension_Table[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 };
    
    int S_Box[8][64] = {
    	{
    		14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
    		0, 15, 7, 4, 15, 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,
    		12, 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
    	}
    };
    
    
    int PC1_Table[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 };
    
    int PC2_Table[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 P_Table[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
    };
    
    
    #pragma once
    #include "tables.h"
    
    void charToBit(char * msg, int* msgBit) {
    	for (int i = 0; i < 8; i++) {
    		for (int j = 0; j < 8; j++) {
    			msgBit[(i + 1) * 8 - j - 1] = (msg[i] >> j) & 1;
    		}
    	}
    }
    
    void BitToChar(int* msgBit, char* msg) {
    	int sum = 0;
    	int count = 0;
    	for (int i = 0; i < 64; i++) {
    		sum = sum * 2 + msgBit[i];
    		if (i % 8 == 7) {
    			msg[count++] = sum;
    			sum = 0;
    		}
    	}
    }
    
    void IP_Permutation(int* input, int* output) {
    	for (int i = 0; i < 64; i++) {
    		output[i] = input[IP_Table[i] - 1];
    	}
    }
    
    void IP_Reverse_Permutation(int* input, int* output) {
    	for (int i = 0; i < 64; i++) {
    		output[i] = input[IP_Reverse_Table[i] - 1];
    	}
    }
    
    void devision(int* input, int *L, int* R) {
    	for (int i = 0; i < 32; i++) {
    		L[i] = input[i];
    		R[i] = input[i + 32];
    	}
    }
    
    void Extension(int* R, int* extendedR) {
    	for (int i = 0; i < 48; i++) {
    		extendedR[i] = R[Extension_Table[i] - 1];
    	}
    }
    
    void Xor48(int* R, int* subkey) {
    	for (int i = 0; i < 48; i++) {
    		if (R[i] != subkey[i]) {
    			R[i] = 1;
    		}
    		else {
    			R[i] = 0;
    		}
    	}
    }
    
    void Xor32(int* R, int* subkey) {
    	for (int i = 0; i < 32; i++) {
    		if (R[i] != subkey[i]) {
    			R[i] = 1;
    		}
    		else {
    			R[i] = 0;
    		}
    	}
    }
    
    void SBox(int* input, int start, int* output, int* SBox) {
    	int row = input[start] * 2 + input[start + 5];
    	int col = 0;
    	for (int i = 1; i <= 4; i++) {
    		col = col * 2 + input[start + i];
    	}
    	int result = SBox[row * 16 + col];
    	for (int i = 0; i < 4; i++) {
    		output[3 - i] = (result >> i) & 1;
    	}
    }
    
    void P_Permutation(int* input, int* output) {
    	for (int i = 0; i < 32; i++) {
    		output[i] = input[P_Table[i] - 1];
    	}
    }
    
    void Feistel(int* R, int* subkey, int* output) {
    	int extendedR[48] = { 0 };
    	Extension(R, extendedR);
    	Xor48(extendedR, subkey);
    
    	int SBoxResult[8][4] = { 0 };
    	for (int i = 0; i < 8; i++) {
    		SBox(extendedR, i * 6, SBoxResult[i], S_Box[i]);
    	}
    
    	//将SBox结果连接起来
    	int totalSBox[32] = { 0 };
    	for (int i = 0; i < 32; i++) {
    		totalSBox[i] = SBoxResult[i / 4][i % 4];
    	}
    	P_Permutation(totalSBox, output);
    }
    
    void leftShift(int* input) {
    	int first = input[0];
    	for (int i = 0; i < 27; i++) {
    		input[i] = input[i + 1];
    	}
    	input[27] = first;
    }
    
    void PC1(int* input, int* key) {
    	for (int i = 0; i < 56; i++) {
    		input[i] = key[PC1_Table[i] - 1];
    	}
    }
    
    void PC2(int* input, int* subkey) {
    	for (int i = 0; i < 48; i++) {
    		subkey[i] = input[PC2_Table[i] - 1];
    	}
    }
    
    void generateSubkeys(int key[64], int subkeys[16][48]) {
    	int pc1[56] = { 0 };
    	int pc2[56] = { 0 };
    	int C[28] = { 0 };
    	int D[28] = { 0 };
    
    	PC1(pc1, key);
    
    	for (int i = 0; i < 28; i++) {
    		C[i] = pc1[i];
    		D[i] = pc1[i + 28];
    	}
    
    	for (int i = 1; i < 17; i++) {
    		if (i == 1 || i == 2 || i == 9 || i == 16) {
    			leftShift(C);
    			leftShift(D);
    		}
    		else {
    			leftShift(C);
    			leftShift(C);
    			leftShift(D);
    			leftShift(D);
    		}
    
    		for (int i = 0; i < 28; i++) {
    			pc2[i] = C[i];
    			pc2[i + 28] = D[i];
    		}
    
    		PC2(pc2, subkeys[i - 1]);
    	}
    }
    
    void Swap(int* L, int * R) {
    	int* temp = L;
    	L = R;
    	R = temp;
    }
    
    void DES(int* msgBit, char* key) {
    
    	int keyBit[64] = { 0 };
    	int permutationBit[64] = { 0 };        //IP置换后的矩阵
    	int L[32] = { 0 };                     //前32位
    	int R[32] = { 0 };                     //后32位
    	int subkeys[16][48] = { 0 };
    	int iterationL[32] = { 0 };            //迭代后的L和R
    	int iterationR[32] = { 0 };
    	int cryptedMsg[64] = { 0 };
    
    	charToBit(key, keyBit);
    
    	IP_Permutation(msgBit, permutationBit);
    
    	devision(permutationBit, L, R);
    
    	generateSubkeys(keyBit, subkeys);
    
    	//迭代T
    	int FeistelResult[32] = { 0 };
    
    	for (int i = 0; i < 16; i++) {
    
    		for (int j = 0; j < 32; j++) {
    			iterationL[j] = R[j];
    			iterationR[j] = L[j];
    		}
    
    		Feistel(R, subkeys[i], FeistelResult);
    		Xor32(iterationR, FeistelResult);
    
    		for (int j = 0; j < 32; j++) {
    			L[j] = iterationL[j];
    			R[j] = iterationR[j];
    		}
    	}
    
    	//交换L和R的顺序并合并
    	int iterationTotal[64] = { 0 }; //迭代后的二进制
    	for (int i = 0; i < 32; i++) {
    		iterationTotal[i] = iterationR[i];
    		iterationTotal[i + 32] = iterationL[i];
    	}
    	IP_Reverse_Permutation(iterationTotal, cryptedMsg);
    
    	printf("\nThe crypeted message is :\n");
    	for (int i = 0; i < 64; i++) {
    		printf("%d", cryptedMsg[i]);
    	}
    }
    
    void Decrypt(int* msgBit, char* key) {
    	int keyBit[64] = { 0 };
    	int permutationBit[64] = { 0 };        //IP置换后的矩阵
    	int L[32] = { 0 };                     //前32位
    	int R[32] = { 0 };                     //后32位
    	int subkeys[16][48] = { 0 };
    	int iterationL[32] = { 0 };            //迭代后的L和R
    	int iterationR[32] = { 0 };
    	int decryptedBit[64] = { 0 };
    
    	charToBit(key, keyBit);
    
    	IP_Permutation(msgBit, permutationBit);
    
    	devision(permutationBit, L, R);
    
    	generateSubkeys(keyBit, subkeys);
    
    	//迭代T
    	int FeistelResult[32] = { 0 };
    
    	for (int i = 0; i < 16; i++) {
    
    		for (int j = 0; j < 32; j++) {
    			iterationL[j] = R[j];
    			iterationR[j] = L[j];
    		}
    
    		Feistel(R, subkeys[15 - i], FeistelResult);
    		Xor32(iterationR, FeistelResult);
    
    		for (int j = 0; j < 32; j++) {
    			L[j] = iterationL[j];
    			R[j] = iterationR[j];
    		}
    	}
    
    	//交换L和R的顺序并合并。
    	int iterationTotal[64] = { 0 };
    	for (int i = 0; i < 32; i++) {
    		iterationTotal[i] = iterationR[i];
    		iterationTotal[i + 32] = iterationL[i];
    	}
    	IP_Reverse_Permutation(iterationTotal, decryptedBit);
    
    	char decryptedMsg[9] = { 0 };
    	BitToChar(decryptedBit, decryptedMsg);
    	printf("\nThe decrypeted message is :\n");
    	for (int i = 0; i < 8; i++) {
    		printf("%c", decryptedMsg[i]);
    	}
    	printf("\n");
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "utility.h"
    
    int main() {
    	char msg[9] = { 0 };
    	char key[9] = { 0 };
    
    	int msgBit[64] = { 0 };
    	
    	char cryptedMsg[65] = { 0 };
    	int cryptedMsgBit[64] = { 0 };
    
    	printf("Encrypting process\n\n");
    	printf("Please enter the message:");
    	gets_s(msg);
    	printf("Please enter the key:");
    	gets_s(key);
    	charToBit(msg, msgBit);
    	DES(msgBit, key);
    
    	printf("\n\nDecrypting process\n\n");
    	printf("Please enter the crypted message:\n");
    	gets_s(cryptedMsg);
    	for (int i = 0; i < 64; i++) {
    		cryptedMsgBit[i] = cryptedMsg[i] - '0';
    	}
    	printf("Please enter the key:");
    	gets_s(key);
    	Decrypt(cryptedMsgBit,key);
    	system("pause");
    	return 0;
    }
    
    展开全文
  • DES 算法广泛应用于信用卡持卡人的PIN 的加密传输、IC 卡与POS 间的双向认证、金融交易数据包的MAC 校验等 领域,用以实现关键数据的保密。本文以一实例详析DES 算法的加密过程,并讨论其安全性
  • DES算法实现Java

    2017-05-01 23:46:57
    密码学基础之DES算法的详细过程 体会加密算法的置乱和混沌
  • DES加密算法实现过程

    千次阅读 2013-01-19 20:53:54
    过程:明文经IP变换后分高低32位l0,r0,再经过16次循环加密得到r0l0,再IP逆变换得到密文。 其中1、IP变换和IP逆变换都是数组对应 2、16次循环每次输入l0,r0输出l1,r1,本次输出是下一次输入。每一次l1=r0,r1=...
    /*64位明文A,64位密钥key,64位密文B(本程序A加密10000次,每次密钥都是随机生成的,看是否能得到B)
    总过程:明文经IP变换后分高低32位l0,r0,再经过16次循环加密得到r0l0,再IP逆变换得到密文。
    其中1、IP变换和IP逆变换都是数组对应
    2、16次循环的每次输入l0,r0输出l1,r1,本次输出是下一次的输入。每一次l1=r0,r1=l0^f(r0,ki)
    而f(r0,ki)的过程是:r032to48^ki48,48位分成8组,每组6位,经过s压缩每组变为4位。得到的32位就是r1
    另外16个ki的形成过程是:key64to56,分成两个28位c0,d0。接着16次循环,每次循环c0,d0分别按照一定的位数循环左移过后得到c1,d1再组合成一个key,而c1,d1成为下次的c0,d0
    */
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    #define   RAND_MAX  0x7fffffffffffffffu
    
    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};
    int 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};
    int r32to48[48]={31,0,1,2,3,4,3,4,5,6,7,8,7,8,9,10,11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20,21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0};
    int s[8][64]={{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}};
    
    int key64to56[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};
    int key56to48[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 left[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
    
    int main()
    {
    	freopen("des.txt","w+",stdout);
    	unsigned __int64 A=0,B=1206425,rB,KEY,sum,kkey;
    	int a[64]={0},b[64]={0},key[64],keyk[16][48],c0[28],d0[28],c0d0[56],IPa[64],after16[64],IP_1a[64],l0[32],r0[32],r048[48],r032[32],l1[32],r1[32];
    	int num,i,j,k,group,everyGroup,lleft[10],x,y,fb[6];
    	bool flag=false;
    	
    	//将所有的下标从0开始
    	for(i=0;i<64;i++)
    		IP[i]=IP[i]-1,IP_1[i]=IP_1[i]-1;
    	for(i=0;i<48;i++)
    		key56to48[i]=key56to48[i]-1;
    	for(i=0;i<56;i++)
    		key64to56[i]=key64to56[i]-1;
    	//将A分成64位
    	for(i=0;i<64;i++)
    		a[i]=0;
    
    	for(i=0;A;i++)
    	{
    		a[i]=A&1;
    		A=A>>1;
    	}
    	//IP变换
    	for(i=0;i<64;i++)
    		IPa[i]=a[IP[i]];
    	//左右各32位
    	for(i=0;i<32;i++)
    		l0[i]=IPa[i],r0[i]=IPa[i+32];
    
    	//sum=0;
    	//for(i=0;i<64;i++)
    	//	sum=sum+((unsigned __int64)1<<i);
    	//暴力枚举密钥
    	for(num=0;num<10000;num++)
    	{
    		KEY=rand();
    		//将KEY分解为64位
    		for(i=0;i<64;i++)
    			key[i]=0;
    		i=0,kkey=KEY;
    		while(kkey)
    		{
    			key[i++]=kkey&1;
    			kkey=kkey>>1;
    		}
    		//64位降为56位
    		for(i=0;i<28;i++)
    			c0[i]=key[key64to56[i]],d0[i]=key[key64to56[i+28]];
    		//key分为16个子密钥
    		for(i=0;i<16;i++)
    		{
    			//c0,d0左移left[i]位
    			for(j=0;j<left[i];j++)
    				lleft[j]=c0[j];
    			for(j=0;j<28-left[i];j++)
    				c0[j]=c0[j+left[i]];
    			for(j=28-left[i];j<28;j++)
    				c0[j]=lleft[j+left[i]-28];
    
    			for(j=0;j<left[i];j++)
    				lleft[j]=d0[j];
    			for(j=0;j<28-left[i];j++)
    				d0[j]=d0[j+left[i]];
    			for(j=28-left[i];j<28;j++)
    				d0[j]=lleft[j+left[i]-28];
    
    			//c0d0合起来56位
    			for(j=0;j<28;j++)
    				c0d0[j]=c0[j],c0d0[j+28]=d0[j];
    			//将56位降为48位生成一个子密钥
    			for(j=0;j<48;j++)
    				keyk[i][j]=c0d0[key56to48[j]];
    		}
    
    		//经过16轮运算
    		for(i=0;i<16;i++)
    		{
    			//l1=r0
    			for(j=0;j<32;j++)
    				l1[j]=r0[j];
    			//r0 32位变成48位
    			for(j=0;j<48;j++)
    				r048[j]=r0[r32to48[j]];
    			//分成8组,每组6bit,压缩至4bit
    			for(group=0;group<8;group++)
    			{
    				k=0;
    				for(everyGroup=0;everyGroup<6;everyGroup++)//每组6个,每位是r048^keyk[i]
    					fb[everyGroup]=r048[group*6+everyGroup]^keyk[i][group*6+everyGroup];
    				x=fb[0]*2+fb[5];
    				y=fb[4]+fb[3]*2+fb[2]*4+fb[1]*8;
    				r032[4*group]=(s[group][x*16+y]>>3)&1;
    				r032[4*group+1]=(s[group][x*16+y]>>2)&1;
    				r032[4*group+2]=(s[group][x*16+y]>>1)&1;
    				r032[4*group+3]=s[group][x*16+y]&1;
    			}
    			//r1=l0^f(r0,ki)
    			for(j=0;j<32;j++)
    				r1[j]=l0[j]^r032[j];
    
    			//此次的l1,r1变成下一次的l0,r0
    			for(j=0;j<32;j++)
    				l0[j]=l1[j],r0[j]=r1[j];
    		}
    
    		//16轮变换后,r16,l16组成新数
    		for(i=0;i<32;i++)
    		{
    			after16[i]=r0[i];
    			after16[i+32]=l0[i];
    		}
    		//然后再逆变换,得到密文
    		for(i=0;i<64;i++)
    			IP_1a[i]=after16[IP_1[i]];
    		//比较得到的密文是否与B相同
    		for(rB=0,i=63;i>=0;i--)
    		{
    			rB=(rB<<1)+IP_1a[i];
    		}
    		//如果相同,则输出结果
    		if(rB==B)
    		{
    			flag=true;
    			printf("(0,1206425)的密钥为%I64u\n",key);
    			break;
    		}
    		else
    		{
    			printf("0经过密钥%I64u加密后的密文是:%I64u\n",KEY,rB);
    		}
    	}
    	if(flag==false)
    	{
    		printf("\n随机生成了10000个密钥,密文都不是1206425。所以解密失败\n");
    	}
    	return 0;
    }


    
    

    详细过程参照这两篇博客

    http://simplesource.blog.163.com/blog/static/103414062007221112947879/

    http://blog.csdn.net/boksic/article/details/6896402


    展开全文
  • 之前介绍了DES算法的原理和实现过程,现在介绍一下3DES的原理和实现过程DES算法的密钥长度为64位(实际有效长度为56位,因为每隔8位中有1位为校验位,使用的是奇偶校验法)。 其实3DES就是DES的升级版,所利用的...

    之前介绍了DES算法的原理和实现过程,现在介绍一下3DES的原理和实现过程。
    DES算法的密钥长度为64位(实际有效长度为56位,因为每隔8位中有1位为校验位,使用的是奇偶校验法)。
    其实3DES就是DES的升级版,所利用的技术原理是一样的。

    3DES原理: 其实就是对明文进行三次DES算法运算,主要是通过增加DES密钥的长度来使破解更难。

    下图为3DES算法的加密和解密过程:

    在这里插入图片描述
    3DES算法使用的密钥有效长度为112位,虽然进行了三次DES算法运算,但是只使用了两种不同密钥(由于效率问题),一种密钥的有效长度为56位,两种就为56*2=112位,其中密钥K1=K3。

    加密过程: 第一次为明文M与密钥K1进行DES 加密 得到的输出M’,第二次为第一次的输出M’与密钥K2进行DES 解密 得到的输出M’’,第三次为第二次的输出M’'与密钥K3进行DES 加密 最终得到密文C;

    解密过程: 第一次为密文C与密钥K3进行DES 解密 得到的输出M’’,第二次为第一次的输出M’‘与密钥K2进行DES 加密 得到的输出M’,第三次为第二次的输出M’与密钥K3进行DES 解密 最终得到明文M;

    可以实现K1、K2、K3都不相等,这样安全性会更高,但是效率会降低。

    如果K1=K2或者K2=K3,那么就相当于使用了一个56位的密钥单DES。
    怎么理解这句话呢?
    我们知道DES的基本原理是:明文M两次与密钥K进行异或运算就能得出它本身,看下图式子,⊕表示异或运算。
    例如:明文M,密钥K
    则在加密过程中:密文C=M⊕K;
    解密过程中:明文M=C⊕K;
    即M=M⊕K⊕K。
    那么如果K1=K2,那么不就相当于对明文M进行加密,再进行解密,那得到的不就还是原来的明文M吗?整个过程不就相当于对明文M加密了一次(因为前两次进行了抵消,相当于没有进行操作),3DES解密过程同理。
    若K2=K3道理与K1=K2相同,故不做过多解释。

    3DES加密算法特点:
    **相对于DES来说,由于密钥长度的增加,安全性更高,还可以实现4DES算法等等;
    **但是由于以DES为基本设计出来的,效率至少低于DES算法的1/3,所以处理速度并不高。

    后面又出现了AES算法,相比于3DES算法,安全性更高,速度更快,后面会介绍。

    展开全文
  • DES加密算法的实现

    2008-09-03 15:40:18
    DES算法简介 加密过程设计 DES加密与解密算法的实现代码
  • DES算法代码实现

    千次阅读 2019-06-05 15:46:08
    DES是一个16轮Feistel型结构密码,它分组长度为64比特,用一个56比特密钥来加密一个64比特明文串,输出一个64比特密文串。其中,使用密钥为64比特,实用56比特,另8位用作奇偶校验。加密的过程是先对64位...

    DES是一个16轮的Feistel型结构密码,它的分组长度为64比特,用一个56比特的密钥来加密一个64比特的明文串,输出一个64比特的密文串。其中,使用密钥为64比特,实用56比特,另8位用作奇偶校验。加密的过程是先对64位明文分组进行初始置换,然后分左、右两部分分别经过16轮迭代,然后再进行循环移位与变换,最后进行逆变换得出密文。加密与解密使用相同的密钥,因而它属于对称密码体制。假设输入的明文数据是64比特。首先经过初始置换IP后把其左半部分32比特记为L0,右半部分32比特记为R0,即成了置换后的输入;然后把R0与密钥产生器产生的子密钥k1进行运算,其结果计为f (R0,k1);再与L0进行摸2加得到L0+f (R0 , k1), 把R0记为L1放在左边,而把L0+f (R0 , k1)记为R1放在右边,从而完成了第一轮迭代运算。在此基础上,重复上述的迭代过程,一直迭代至第16轮。所得的第16轮迭代结果左右不交换,即L15+f (R15 , k16)记为R16,放在左边,而R15记为L16放在右边,成为预输出,最后经过初始置换的逆置换IP-1运算后得到密文。详细的算法解析在网上有很多,笔者在这里就不一一介绍,核心处理在于f函数,编程过程中谨慎一点儿别抄错数字之类的,一般都没问题。解密过程是加密的逆向,只需要在加密过程中把十六轮子秘钥全部记录下来,在解密时反过来使用一轮,其他的处理算法如出一辙,就能正确得出结果。直接在下面贴出代码(代码有局限,只能接受十六位十六进制数作为明文,十六位十六进制数作为初始秘钥,对于中文的加密以及任意长度的字符串的加密,有需要的朋友可以自行修改代码实现)。初始秘钥和明文从文件中读取,会在后面附上截图。

    // DES.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    //记录所有的子秘钥
    int eachKey[16][48];
    
    int boxS[8][4][16] = { //S1
    	{   {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}
    	},
    	//S2
    	{   {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}
    	},
    	//S3
    	{   {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}
    	},
    	//S4
    	{   {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}
    	},
    	//S5
    	{   {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}
    	},
    	//S6
    	{   {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}
    	},
    	//S7
    	{   {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}
    	},
    	//S8
    	{   {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}
    	}
    };
    
    //记录左移次数
    int leftMove[16] = { 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1 };
    
    int C[28] = { 0 }, D[28] = { 0 };
    int L[32] = { 1 }, R[32] = { 1 };
    
    //p置换
    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};
    
    //E扩展
    int biteChoiceLabel[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};
    
    //秘钥置换
    int keySwap[8][6] = { {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 IP[8][8] = { {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 }};
    
    //密文逆置换
    int IP_1[8][8] = { {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} };
    
    //64->56选择
    int PC_1[8][7] = { {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} };
    
    
    //初始置换IP
    void InitIP(int **input) {
    	int s[64];
    	int c = 0;
    	for(int i = 0;i<8;i++)
    		for (int j = 0; j < 8; j++) {
    			s[c++] = input[i][j];
    		}
    	int **newIP = new int *[8];
    	for (int i = 0; i < 8; i++)
    		newIP[i] = new int[8];
    
    	for (int i = 0; i < 8; i++) {
    		for (int j = 0; j < 8; j++) {
    			int loc = IP[i][j] - 1; //因为从1开始,这里下标减一
    			newIP[i][j] = s[loc];
    		}
    	}
    	int l = 0, m = 0;
    	for(int i = 0;i < 8;i++)
    		for (int j = 0; j < 8; j++) {
    			if (i * 8 + j < 32)
    				L[l++] = newIP[i][j];
    			else
    				R[m++] = newIP[i][j];
    		}
    }
    
    //逆初始置换
    void reverse() {
    	int temp[64];
    	int a[64];
    	for (int i = 0; i < 32; i++) {
    		temp[i] = L[i];
    		temp[i + 32] = R[i];
    	}
    	
    	int l = 0, m = 0;
    	for (int i = 0; i < 8; i++) {
    		for (int j = 0; j < 8; j++) {
    			a[l++] = temp[IP_1[i][j] - 1];
    		}
    	}
    	l = 0;
    	for (int i = 0; i < 64; i++)
    		if (i < 32)
    			L[l++] = a[i];
    		else
    			R[m++] = a[i];
    }
    
    //操作函数f
    int *f(int n, int flag) {
    	int *result = new int[32];
    	int a[48];
    	int temp[56];
    	int newKey[48];
    	for (int i = 0; i < 28; i++) {
    		temp[i] = C[i];
    		temp[i + 28] = D[i];
    	}
    	int l = 0;
    	if (flag == 0) {
    		for (int i = 0; i < 8; i++) {
    			for (int j = 0; j < 6; j++) {
    				int loc = keySwap[i][j];
    				newKey[l++] = temp[loc - 1];
    				eachKey[15-n][l - 1] = newKey[l - 1];   //记录加密时的全部子秘钥
    			}
    		}
    	}
    	else {
    		for (int i = 0; i < 48; i++)
    			newKey[i] = eachKey[n][i];
    	}
    	int newR[48];
    	for (int i = 0; i < 48; i++)
    		newR[i] = R[biteChoiceLabel[i]-1];
    	for (int i = 0; i < 48; i++)
    		a[i] = (newKey[i]+newR[i])%2;
    	int b[8][6];
    	int r[8];
    	l = 0;
    	int m = 0;
    	int k = 0;
    	for (int i = 0; i < 48; i++) {
    		b[l][m] = a[i];
    		if ((i + 1) % 6 == 0) {
    			int first = b[l][0] * 2 + b[l][5];
    			int second = b[l][1] * 8 + b[l][2] * 4 + b[l][3] * 2 + b[l][4];
    			r[l] = boxS[l][first][second];
    			l++;
    			m = 0;
    		}
    		else
    			m++;
    	}
    	int c[8][4];
    	l = 0;
    	for (int i = 0; i < 8; i++) {
    		int ti = 0;
    		int aa = r[i] / 8;
    		int bb = (r[i] - aa * 8) / 4;
    		int cc = (r[i] - aa * 8 - bb * 4) / 2;
    		int dd = r[i] - aa * 8 - bb * 4 - cc * 2;
    		c[i][0] = aa;
    		c[i][1] = bb;
    		c[i][2] = cc;
    		c[i][3] = dd;
    	}
    	int tempP[32];
    	for (int i = 0; i < 8; i++)
    		for (int j = 0; j < 4; j++) {
    			tempP[l++] = c[i][j];
    		}
    	for (int i = 0; i < 32; i++)
    		result[i] = tempP[P[i] - 1];
    	return result;
    }
    
    //交换处理
    void swap(int n, int flag) {
    	int tempL[32], tempR[32];
    	int *Result = new int[32];
    	//数据备份
    	for (int i = 0; i < 32; i++) {
    		tempR[i] = R[i];
    		tempL[i] = L[i];
    	}
    	Result = f(n, flag);
    	//数据更新计
    	for (int i = 0; i < 32; i++) {
    		L[i] = tempR[i];
    		R[i] = (tempL[i] + Result[i]) % 2;
    	}
    	if (n == 15) {
    		for (int i = 0; i < 32; i++) {
    			int temp = L[i];
    			L[i] = R[i];
    			R[i] = temp;
    		}
    	}
    }
    
    
    
    //产生第一轮秘钥(64位->56位)
    int **firstKey(int **key) {
    	int **fk = new int *[8];
    	for (int i = 0; i < 8; i++)
    		fk[i] = new int[7];
    	int nk[64];
    	int s = 0;
    	int l = 0, m = 0;
    	for (int i = 0; i < 8; i++)
    		for (int j = 0; j < 8; j++)
    			nk[s++] = key[i][j];
    	for (int i = 0; i < 8; i++) {
    		for (int j = 0; j < 7; j++) {
    			int loc = PC_1[i][j] - 1;
    			fk[i][j] = nk[loc];
    			if (l < 28)
    				C[l++] = nk[loc];
    			else
    				D[m++] = nk[loc];
    		}
    	}
    	return fk;
    }
    
    //循环产生子秘钥
    void nextKeyLeft(int n) {
    	int times = leftMove[n];
    	for (int i = 0; i < times; i++) {
    		int C0 = C[0];
    		int D0 = D[0];
    		for (int j = 1; j < 28; j++) {
    			C[j - 1] = C[j];
    			D[j - 1] = D[j];
    		}
    		C[27] = C0;
    		D[27] = D0;
    	}
    }
    
    
    //十六进制输入转二进制
    int **hxTobin(string input) {
    	int a[64];
    	int x[16];
    	int **init_letters = new int *[8];
    	for (int i = 0; i < 8; i++)
    		init_letters[i] = new int[8];
    	int j = 0;
    	for (int i = 0; i < 16; i++) {
    		if (input[i] >= '0' && input[i] <= '9')
    			x[i] = input[i] - 48;
    		else if (input[i] >= 'A' && input[i] <= 'Z')
    			x[i] = input[i] - 55;
    		else
    			x[i] = input[i] - 87;
    	}
    	for (int i = 0; i < 16; i++) {
    		int aa = x[i] / 8;
    		int bb = (x[i] - aa * 8) / 4;
    		int cc = (x[i] - aa * 8 - bb * 4) / 2;
    		int dd = x[i] - aa * 8 - bb * 4 - cc * 2;
    		a[j] = aa;
    		a[j + 1] = bb;
    		a[j + 2] = cc;
    		a[j + 3] = dd;
    		j = j + 4;
    	}
    	
    	int l = 0;
    	for (int i = 0; i < 8; i++) {
    		for (j = 0; j < 8; j++) {
    			init_letters[i][j] = a[l++];
    		}
    	}
    	return init_letters;
    }
    
    //二进制密文转十六进制密文
    string binTohx(int a[]) {
    	string res;
    	res.resize(16);
    	int j = 0;
    	for (int i = 0; i < 64; i+=4) {
    		int b = a[i] * 8 + a[i + 1] * 4 + a[i + 2] * 2 + a[i + 3];
    		if (b < 10)
    			res[j] = b+'0';
    		else{
    			switch (b) {
    			case 10:
    				res[j] = 'a';
    				break;
    			case 11:
    				res[j] = 'b';
    				break;
    			case 12:
    				res[j] = 'c';
    				break;
    			case 13:
    				res[j] = 'd';
    				break;
    			case 14:
    				res[j] = 'e';
    				break;
    			case 15:
    				res[j] = 'f';
    				break;
    			}
    		}
    		j++;
    	}
    	return res;
    }
    
    //解密过程
    void decode(string res) {
    	int **re = new int *[8];
    	for (int i = 0; i < 8; i++)
    		re[i] = new int[8];
    	re = hxTobin(res);
    	InitIP(re);
    	for (int i = 0; i < 16; i++) {
    		swap(i, 1);
    	}
    	reverse();
    	int a[64];
    	for (int i = 0; i < 32; i++) {
    		a[i] = L[i];
    		a[i + 32] = R[i];
    	}
    	string input;
    	input = binTohx(a);
    	std::cout << input << endl << endl;;
    }
    
    
    int main()
    {
    	string letters;
    	string keys;
    	string words;
    	char flag='n';
    	int miwen[64];
    	ifstream file;
    	file.open("test.txt");
    	int **fir = new int *[8];
    	for (int i = 0; i < 8; i++)
    		fir[i] = new int[8];
    	cout << "--------- Begin(y/n)? ----------" << endl;
    	cin >> flag;
    	while (flag == 'y' &&!file.eof()) {
    		std::cout << "---------- 读入密文: ----------" << endl;
    		if (!file) {
    			std::cout << "---------- 读取失败 ----------" << endl;
    			exit(0);
    		}
    		getline(file, letters);
    		std::cout << "明文: " << letters << endl << endl;
    		std::cout << "---------- 读入秘钥: ----------" << endl;
    		getline(file, keys);
    		std::cout << "秘钥: " << keys << endl << endl;
    		fir = hxTobin(keys);
    		int **getLetters = new int *[8];
    		for (int i = 0; i < 8; i++)
    			getLetters[i] = new int[8];
    		getLetters = hxTobin(letters);
    		InitIP(getLetters);
    		int **fk = new int *[8];
    		for (int i = 0; i < 8; i++)
    			fk[i] = new int[7];
    		for (int i = 0; i <= 16; i++) {
    			if (i == 0) {
    				fk = firstKey(fir);
    			}
    			else {
    				nextKeyLeft(i - 1);
    				swap(i - 1, 0);
    			}
    		}
    		reverse();
    		int k = 0;
    		for (int i = 0; i < 32; i++) {
    			miwen[k++] = L[i];
    		}
    		for (int i = 0; i < 32; i++) {
    			miwen[k++] = R[i];
    		}
    		words = binTohx(miwen);
    		std::cout << "密文: " << words << endl;
    		std::cout << "----------  解密中  ----------" << endl;
    		decode(words);
    		std::cout << "-----------  继续(y/n) ---------" << endl;
    		std::cin >> flag;
    	}
    	std::cout << "---------- End! ----------" << endl;
    	return 0;
    }
    

    测试数据如下图:奇数行是明文,偶数行是对应的测试秘钥

    运行截图:

    尊重原作,转载请注明,转载自:https://blog.csdn.net/kr2563

    展开全文
  • des算法代码实现输出matlab LeetCodePractice algorithm training log Build personal C code lib teshi Linux 中 initcall 机制 【嵌入式】基于makefileSTM32编译方法探索 keil编译链接过程以及ARMCC、ARMASM、...
  • des算法实现过程分析

    2008-02-01 14:54:00
    1. 处理密钥:1.1 从用户处获得64位密钥.(每第8位为校验位,为使密钥有正确奇偶校验,每个密钥要有奇 数个”1”位.(本文如未特指,均指二进制位)1.2 具体过程:1.2.1 对密钥实施变换,使得变换以后密钥各个位...
  • 结合嵌入式系统的特点及3DES加密算法,实现了基于ARM的3DES的加密算法的系统设计,详细阐述了3DES算法原理及加密系统的实现过程,并给出了演示结果。此系统具有方便、灵活、体积小,可定制等特点,因此在实际应用中...
  • DES加密算法原理和实现过程

    千次阅读 2020-09-11 22:01:48
    由于DES算法是一个典型对称加密算法,故首先介绍一下对称加密算法。 对称加密算法概念: 顾名思义就是加密和解密过程使用加密算法和解密算法是一样,并且所需要密钥也是一样,这就要求接受方事先知道发送...
  • DES算法流水实现

    2010-12-12 20:57:00
    这两天完成了DES算法的Pipeline实现,在Xilinx的ISE中用verilog语言完成。首先完成了8个时钟周期完成一次的Pipeline实现,再次完成了4时钟周期的Pipeline。通过ISE自带的仿真器仿真通过。由于这只是一个项目中的一...
  • DES算法的python3实现

    千次阅读 2018-10-19 19:49:22
    DES原理 DES原理 这里不予以复述, 有很多优秀博客 原理可以参考这篇博客 ... DES实现 1. 主函数框架 ...DES 函数 传入参数为 ...key (解密key) ...# DES 算法实现 flag是标志位 当为-1时, 是D...
  • S—DES算法分析及实现

    2011-06-02 13:53:21
    S—DES算法分析及实现论文,精确而细致秒数了DES算法实现的过程
  • 将置换输出 64 位明文分成左右凉拌左一半为 L0右一半称为 R0各 32 位 * 3 计算函数 16 轮迭代 * a 第一轮加密迭代左半边输入 L0右半边输入 R0由轮函数 f 实现子密钥 K1 对 R0 加密 * 结果为 32 位数据组 f(R0...
  • DES算法代码实现(C语言)

    千次阅读 2020-08-16 16:34:41
    通过C语言模拟DES算法的整个加密过程 初始明文(64位),首先通过IP置换表进行置换,然后将置换后的结果分成左半部分L0(32位)和右半部分R0(32位),右半部分R0直接进行交换为下一轮的L1,左半部分L0与作为输入...
  • 线 JIANGSU JIANGSU 网络安全课程设计报告 网络安全课程设计报告 学 院 计算机工程学院 专 业 计算机科学与技术 班 级 09计3W 学 号 09141307 姓 名 张 赛 指导老师 古春生 一DES算法的简介 DES算法的加密解密过程是...
  • DES算法 课程设计

    2012-03-11 18:01:29
    此为信息工程的一个课程设计 des算法的实现过程以及部分代码

空空如也

空空如也

1 2 3 4 5 ... 19
收藏数 376
精华内容 150
关键字:

des算法的实现过程