1、实验二 DES加解密算法的实现 实验二:DES的编程实现一、实验目的 在本实验中,用VC/ C/C+/Java实现DES的加解密算法。完成实验后将能够深入理解DES加解密算法及其在VC/ C/C+/Java中的实现过程。二、实验条件 熟悉VC/ C/C+/Java开发环境和有关DES算法的有关知识;一台安装有VC/ C/C+/Java的计算机。三、实验要求 本实验要求:输入十六位十六进制明文(占64Bit)和十六位十六进制密钥(占64Bit),输出十六位十六进制密文和六十四位二进制密文。四、实验步骤对数据进行加密传输能有效地保证数据的机密性,DES是一个保证数据机密性的经典算法,本实验拟在VC
2、/ C/C+/Java环境中实现DES算法。以下以VC为例: 主要内容:在VC中建立三个文件:控制台应用程序的入口点:testMain.cpp,DES2加密类cpp文件zlDES2.cpp,DES2加密类头文件zlDES2.h。操作步骤: (1)进入VC环境; (2)在VC环境中,执行“文件 | 新建”命令,在“新建”对话框中(如图1)选择“文件”页,在其左侧的列表框中选择“Win32 Console Application”,在右侧的“文件名”文本框中输入新文件名“DES”,在“位置”文本框中选择项目的存储路径(比如选择“e:des”)。然后单击“确定”按钮。图1 “New(新建)”对话框(
3、3)“Win32 Console Application-步骤1共1步”对话框中,选择“一个空工程”单选按钮,单击“完成”按钮,再单击“确定”按钮。图2 “Win32 Console Application-步骤1共1步”对话框(4)在VC环境中,在其左侧的工程工作区窗口下端中选择标签“FileView”,单击“DES files”前面的“+”号展开。图3 标签“FileView”(5)鼠标右击“Source Files”,选择“添加文件到目录”,将testMain.cpp和zlDES.cpp添加进“Source Files”。同样,鼠标右击“Header Files”,选择“添加文件到目录”
4、,将zlDES.h添加进“Header Files”。结果,如图4。图4 标签“FileView”(6)选择“组建|编译”,分别编译testMain.cpp和zlDES.cpp。然后再选择“组建|执行”,即可看到结果。五、编写源代码(1)DES算法详述(主要描述算法流程) DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位,整个算法的主流程图如下:其中每一轮轮结构为:(2)可以参照如下VC+代码。 DES算法代码: testMain.cpp/#include stdafx.h#include zlDES.hvoid main() zlDES* my_des = ne
5、w zlDES(); /set key char sz_16key17 = 3132333435363738; my_des-InitializeKey(sz_16key,0); printf(密钥 :n 3132333435363738 nn); /-stand test- /encrypt char sz_16txt17 = 3031323334353637; printf(明文 :n 3031323334353637 nn); my_des-EncryptData(sz_16txt,0); /Ciphertext in bin printf(Ciphertext in bin:n %sn
6、n,my_des-GetCiphertextInBinary(); /Ciphertext in Hex printf(Ciphertext in Hex:n %snn,my_des-GetCiphertextInHex(); zlDES.cpp/*-/ 文件名:zlDES.cpp/ 文件功能描述:DES2加密类 cpp文件/-*/#include stdafx.h#include zlDES.h/ permuted choice table (PC1)密钥置换Aconst static char PC1_Table56 = 57, 49, 41, 33, 25, 17, 9, 1, 58,
7、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;/ permuted choice key (PC2)密钥置换Bconst static char PC2_Table48 = 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12,
8、 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;/ number left rotations of pc1 循环左移位数const static char Shift_Table16 = 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1;/ initial permutation (IP)初始置换IPconst static char IP_Table64 = 58, 50
9、, 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;/ expansion operation matrix (E)扩展选位表E盒const static char E_T
10、able48 = 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;/ The (in)famous S-boxes S盒const static char S_Box8416 = / S1 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7
11、, 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
12、, 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
13、, 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,
14、 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,
15、 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
16、, 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-bit permutation function P used on the output of the S-boxes P盒const static char P_Ta
17、ble32 = 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;/ final permutation IP-1 逆置换 IP-1const static char IPR_Table64 = 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, 5
18、3, 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;zlDES:zlDES() memset(szCiphertextRaw,0,64); memset(szPlaintextRaw,0,64); memset(szCiphertextInBytes,0,8); memset(szPlaintextInBytes,0,8); memset(szCiphertextInBina
19、ry,0,65); memset(szCiphertextInHex,0,17); memset(szPlaintext,0,9); memset(szFCiphertextAnyLength,0,8192); memset(szCiphertextInHex,0,8192); memset(szPlaintextInHex,0,17); memset(szPlaintextInBinary,0,65);zlDES:zlDES()void zlDES:InitializeKey(char* srcHex,unsigned int keyN) /convert 8 char-bytes key
20、to 64 binary-bits char sz_64key64 = 0; Hex2Bits(srcHex,sz_64key,64); /PC 1 char sz_56key56 = 0; for(int k=0;k56;k+) sz_56keyk = sz_64keyPC1_Tablek-1; CreateSubKey(sz_56key,keyN);void zlDES:CreateSubKey(char* sz_56key,unsigned int keyN) char szTmpL28 = 0; char szTmpR28 = 0; char szCi28 = 0; char szDi
21、28 = 0; memcpy(szTmpL,sz_56key,28); memcpy(szTmpR,sz_56key + 28,28); for(int i=0;i16;i+) /shift to left /Left 28 bits memcpy(szCi,szTmpL + Shift_Tablei,28 - Shift_Tablei); memcpy(szCi + 28 - Shift_Tablei,szTmpL,Shift_Tablei); /Right 28 bits memcpy(szDi,szTmpR + Shift_Tablei,28 - Shift_Tablei); memcp
22、y(szDi + 28 - Shift_Tablei,szTmpR,Shift_Tablei); /permuted choice 48 bits key char szTmp5656 = 0; memcpy(szTmp56,szCi,28); memcpy(szTmp56 + 28,szDi,28); for(int j=0;j48;j+) szSubKeyskeyNij = szTmp56PC2_Tablej-1; /Evaluate new szTmpL and szTmpR memcpy(szTmpL,szCi,28); memcpy(szTmpR,szDi,28); void zlD
23、ES:EncryptData(char* _srcHex,unsigned int keyN) char szSrcBits64 = 0; char sz_IP64 = 0; char sz_Li32 = 0; char sz_Ri32 = 0; char sz_Final6464 = 0; Hex2Bits(_srcHex,szSrcBits,64); printf(二进制明文为:n %snn,szSrcBits); /IP InitialPermuteData(szSrcBits,sz_IP); memcpy(sz_Li,sz_IP,32); memcpy(sz_Ri,sz_IP + 32
24、,32); for(int i=0;i16;i+) FunctionF(sz_Li,sz_Ri,i,keyN); /so D=LR memcpy(sz_Final64,sz_Ri,32); memcpy(sz_Final64 + 32,sz_Li,32); /IP for(int j=0;j64;j+) szCiphertextRawj = sz_Final64IPR_Tablej-1; Bits2Hex(szCiphertextInHex,szCiphertextRaw,64);void zlDES:DecryptData(char* _srcHex,unsigned int keyN) c
25、har szSrcBits64 = 0; char sz_IP64 = 0; char sz_Li32 = 0; char sz_Ri32 = 0; char sz_Final6464 = 0; Hex2Bits(_srcHex,szSrcBits,64); /IP - return is sz_IP InitialPermuteData(szSrcBits,sz_IP); /divide the 64 bits data to two parts memcpy(sz_Ri,sz_IP,32); /exchange L to R memcpy(sz_Li,sz_IP + 32,32); /ex
26、change R to L /16 rounds F and xor and exchange for(int i=0;i16;i+) FunctionF(sz_Ri,sz_Li,15-i,keyN); memcpy(sz_Final64,sz_Li,32); memcpy(sz_Final64 + 32,sz_Ri,32); / IP for(int j=0;j64;j+) szPlaintextRawj = sz_Final64IPR_Tablej-1; Bits2Hex(szPlaintextInHex,szPlaintextRaw,64);void zlDES:FunctionF(ch
27、ar* sz_Li,char* sz_Ri,unsigned int iKey,unsigned int keyN) char sz_48R48 = 0; char sz_xor4848 = 0; char sz_P3232 = 0; char sz_Rii32 = 0; char sz_Key48 = 0; char s_Compress3232 = 0; memcpy(sz_Key,szSubKeyskeyNiKey,48); ExpansionR(sz_Ri,sz_48R); XOR(sz_48R,sz_Key,48,sz_xor48); CompressFuncS(sz_xor48,s
28、_Compress32); PermutationP(s_Compress32,sz_P32); XOR(sz_P32,sz_Li,32,sz_Rii); memcpy(sz_Li,sz_Ri,32); memcpy(sz_Ri,sz_Rii,32);void zlDES:InitialPermuteData(char* _src,char* _dst) /IP for(int i=0;i64;i+) _dsti = _srcIP_Tablei-1; void zlDES:ExpansionR(char* _src,char* _dst) for(int i=0;i48;i+) _dsti =
29、 _srcE_Tablei-1; void zlDES:XOR(char* szParam1,char* szParam2, unsigned int uiParamLength, char* szReturnValueBuffer) for(unsigned int i=0; iuiParamLength; i+) szReturnValueBufferi = szParam1i szParam2i; void zlDES:CompressFuncS(char* _src48, char* _dst32) char bTemp86=0; char dstBits4=0; for(int i=0;i8;i+) memcpy(bTempi,_src48+i*6,6); int iX
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1