密码学实验三—DES算法的实现(共20页).doc

上传人:飞****2 文档编号:14981146 上传时间:2022-05-10 格式:DOC 页数:20 大小:198.50KB
返回 下载 相关 举报
密码学实验三—DES算法的实现(共20页).doc_第1页
第1页 / 共20页
密码学实验三—DES算法的实现(共20页).doc_第2页
第2页 / 共20页
点击查看更多>>
资源描述

《密码学实验三—DES算法的实现(共20页).doc》由会员分享,可在线阅读,更多相关《密码学实验三—DES算法的实现(共20页).doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上实验三 用DES实现对数据的加解密一、实验目的1. 用DES加密方法实现对明文的加密2用DES解密加密后的密文二、实验内容1、给定八字节明文,输入8位密钥对明文加密2、用同一个密钥对加密的密文解密的到原来的明文三、实验原理实验步骤(包括流程图、功能模块)【实验原理】1、DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位,其功能是把输入的64位数据块按位重新组合,并把输出分为L0、R0两部分,每部分各长32位,其置换规则见下表:58,50,12,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,

2、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,2. 将输入的第58位换到第一位,第50位换到第2位,.,依此类推,最后一位是原来的第7位。L0、R0则是换位输出后的两部分,L0是输出的左32位,R0 是右32位,例:设置换前的输入值为D1D2D3.D64,则经过初始置换后的结果为:L0=D550.D8;R0=D57D49.D7。经过26次迭代运算后。得到L16、R16,将此作为输入,进行逆置

3、换,即得到密文输出。逆置换正好是初始置的逆运算,例如,第1位经过初始置换后,处于第40位,而通过逆置换,又将第40位换回到第1位,其逆置换规则如下表所示: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,3. DES算法的解密过程是一样的,区别仅仅在於第一次迭代时用子密钥K15,第

4、二次K14、.,最后一次用K0,算法本身并没有变化。【算法流程图】【功能模块】1. 输入明文: void DES:SetMsg(char* _msg, int _length)if (_length8)return;for (int i = 0; i 8)return;for (int i = 0; i _length; i+)keyi = _keyi;/转成二进制Char2Bit(key, bkey, 8);3. 各种置换: void DES:InitSwap(bool in64)/打乱for (int i = 0; i 32; i+)lmsgii = inipi - 1;rmsgii =

5、inipi + 32 - 1;/初始逆置换函数void DES:InitReSwap(bool out64)/组合成64数组bool temp64;for (int i = 0; i 32; i+)tempi = rmsgii;temp32 + i = lmsgii;/按照逆ip矩阵for (int i = 0; i 64; i+)outi = tempback_ipi - 1;/void DES:SubKeyOff(bool* _subkey, int _off)bool temp;for (int i = 0; i _off; i+)temp = _subkey0;for (int i =

6、 0; i 27; i+)_subkeyi = _subkeyi + 1;_subkey27 = temp;4. S盒处理: void DES:DealSBox(bool in48, bool out32)bool _in6, _out4;/8个盒子for (int i = 0; i 8; i+)/提取盒子for (int j = 0; j 6; j+)_inj = ini * 6 + j;/压缩_DealSBox(_in, _out, i);/放进out数组for (int jj = 0; jj 4; jj+)outi * 4 + jj = _outjj;5. 加密函数: void DES:

7、CrypteFunction(bool in32, int isubkey, bool out32)/e 操作bool temp148;EOperation(in, temp1);bool temp248;Mode2Add(temp1, (bool *)subkeyisubkey, temp2, 48);/ok/盒子压缩bool temp348;DealSBox(temp2, temp3);/置换运算pPOperation(temp3, out);6. 解密函数: void DES:Decipher()bool temp132, temp232;/初始置换ipInitSwap(bcrypted

8、msg);/16轮迭代加密for (int i = 0; i 16; i+)if (i % 2 = 0)/L1=R0CopyArray(rmsgi, lmsgi1, 32);/f(R0,k0)CrypteFunction(rmsgi, 15 - i, temp1);/L0+f(R0,k0)Mode2Add(lmsgi, temp1, temp2, 32);/R1=L0+f(R0,k0)CopyArray(temp2, rmsgi1, 32);else/L2=R1CopyArray(rmsgi1, lmsgi, 32);/f(R1,k1)CrypteFunction(rmsgi1, 15 - i

9、, temp1);/L1+f(R1,k1)Mode2Add(lmsgi1, temp1, temp2, 32);/R2=L1+f(R1,k1)CopyArray(temp2, rmsgi, 32);/逆初始置换ipInitReSwap(bdecipher);/转成字符Bit2Char(bdecipher, decipher);四、 软件使用说明(开发环境、参数使用详细说明、实验结果+相应截图等)【开发环境】Microsoft Visual Studio 2015Microsoft Windows 10【参数使用】 char* msg;char *key;【实验结果】实验数据:六、参考资料(书籍

10、或网络文章)密码编码学与网络安全原理与实践(第五版)百度七、实验心得和总结 做这次DES加解密实验前首先要充分了解其原理,在这个实验中,比较难的就是,要将明文和密钥经过初始变换和十六轮加密变换等一系列变换后才能得到置换,其中的变换步骤比较多,而且比较复杂,还要将十六进制转化为二进制,工作量比较大。附:程序源代码#includestdafx.h#include#includeusing namespace std;class DESprivate:/明文char msg8;bool bmsg64;/密钥char key8;bool bkey64;/16个子密钥bool subkey1648;/l

11、0 r0中间变量bool rmsgi32, lmsgi32;/第i个bool rmsgi132, lmsgi132;/第i+1个/密文bool bcryptedmsg64;char cryptedmsg8;/解密的结果bool bdecipher64;char decipher8;private:/静态常量/不允许在类内初始化/初始值换ipconst static int ip64;/子密钥/置换选择1const static int c028;const static int d028;/循环左移表const static int keyoff16;/置换选择2const static in

12、t di48;/加密函数/e运算const static int e_operate48;/sboxconst static int sbox864;/置换运算pconst static int p_operate32;/逆初始置换ipconst static int back_ip64;/位掩码const static char bitmask8;public:/设置明文和密钥/_length要小于或等于8void SetMsg(char* _msg, int _length);void SetKey(char* _msg, int _length);/生产子密钥void ProduceSu

13、bKey();/总的的加密流程void Crypte();/解密void Decipher();/输出密文void OutPutCryptedMsg();/二进制转成字符void Bit2Char(bool* _barray, char* _carray);/length=64/输出解密后的明文void OutPutDecipher();private:/字符转成二进制,并保存到64位bool数组中void Char2Bit(char* _carray, bool* _barray, int length);/二进制转成字符/void Bit2Char(bool* _barray,char*

14、_carray);/length=64/初始置换void InitSwap(bool in64);/初始逆置换void InitReSwap(bool out64);/循环左移void SubKeyOff(bool* _subkey, int _off);/e运算操作函数void EOperation(bool a32, bool b48);/模2相加/相同为0 不同为1void Mode2Add(bool a, bool b, bool c, int length);/sboxvoid DealSBox(bool in48, bool out32);void _DealSBox(bool i

15、n6, bool out4, int box);/p oprarationvoid POperation(bool temp32, bool result32);/加密函数void CrypteFunction(bool in32, int isubkey, bool out32);/数组之间赋值void CopyArray(bool array1, bool array2, int size);using namespace std;/静态常量const int DES:ip64 = 58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46

16、,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;const int DES:c028 = 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;const int DES:d028 = 63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,

17、53,45,37,29,21,13,5,28,20,12,4;const int DES:keyoff16 = 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1;const int DES:di48 = 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;const int DES:e_operate48 = 32,1,2,3,4,5,4,5,6,7,8,9,8,

18、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;const int DES:sbox864 = 14, 4, 13, 1, 1, 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,

19、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

20、, 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,

21、 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

22、, 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,

23、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, 13, 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

24、, 12, 9, 0, 3, 5, 6, 11;const int DES:p_operate32 = 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;const int DES:back_ip64 = 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,

25、27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25;const char DES:bitmask8 = 128,64,32,16,8,4,2,1 ;/实现函数/设置明文/void DES:SetMsg(char* _msg, int _length)if (_length8)return;for (int i = 0; i 8)return;for (int i = 0; i _length; i+)keyi = _keyi;/转成二进制Char2Bit(key, bkey, 8);/字符转成二进制void DES:Char2Bit(char* _c

26、array, bool* _barray, int length)/int index=0;for (int i = 0; i length; i+)for (int j = 0; j j) & 1;/二进制转成字符/void DES:Bit2Char(bool* _barray, char* _carray)char temp;for (int i = 0; i 8; i+)/数学方法转成字符temp = 0;for (int j = 0; j 8; j+)if (_barrayi * 8 + j = 1)temp |= bitmaskj;/couttemp;_carrayi = temp;

27、/初始置换函数/okvoid DES:InitSwap(bool in64)/打乱for (int i = 0; i 32; i+)lmsgii = inipi - 1;rmsgii = inipi + 32 - 1;/初始逆置换函数/okvoid DES:InitReSwap(bool out64)/组合成64数组bool temp64;for (int i = 0; i 32; i+)tempi = rmsgii;temp32 + i = lmsgii;/按照逆ip矩阵for (int i = 0; i 64; i+)outi = tempback_ipi - 1;/循环左移/okvoid

28、 DES:SubKeyOff(bool* _subkey, int _off)/有没有更好的办法?bool temp;for (int i = 0; i _off; i+)temp = _subkey0;for (int i = 0; i 27; i+)_subkeyi = _subkeyi + 1;_subkey27 = temp;/生产子密钥/okvoid DES:ProduceSubKey()/置换选择1bool ctemp28, dtemp28;for (int i = 0; i 28; i+)ctempi = bkeyc0i - 1;dtempi = bkeyd0i - 1;bool

29、 keytemp56;for (int i = 0; i 16; i+)/循环左移SubKeyOff(ctemp, keyoffi);SubKeyOff(dtemp, keyoffi);/合并成一个56数组for (int j = 0; j 28; j+)keytempj = ctempj;keytemp28 + j = dtempj;/置换选择2for (int j = 0; j 48; j+)subkeyij = keytempdij - 1;/e运算/okvoid DES:EOperation(bool a32, bool b48)for (int i = 0; i 48; i+)bi

30、= ae_operatei - 1;/模2想加/okvoid DES:Mode2Add(bool a, bool b, bool c, int length)for (int i = 0; i length; i+)if (ai = bi)ci = 0;elseci = 1;/sbox处理/okvoid DES:DealSBox(bool in48, bool out32)bool _in6, _out4;/8个盒子for (int i = 0; i 8; i+)/提取盒子for (int j = 0; j 6; j+)_inj = ini * 6 + j;/压缩_DealSBox(_in,

31、_out, i);/放进out数组for (int jj = 0; jj = 0; i-)outi = (result (3 - i) & 1;/p操作/okvoid DES:POperation(bool temp32, bool result32)for (int i = 0; i 32; i+)resulti = tempp_operatei - 1;/加密函数/isubkey表明用那个子密钥加密 okvoid DES:CrypteFunction(bool in32, int isubkey, bool out32)/e 操作bool temp148;EOperation(in, te

32、mp1);bool temp248;Mode2Add(temp1, (bool *)subkeyisubkey, temp2, 48);/ok/盒子压缩bool temp348;DealSBox(temp2, temp3);/置换运算pPOperation(temp3, out);/ des加密流程void DES:Crypte()/直接用bmsg明文/直接用cryptedmsg存放密文bool temp132, temp232;/初始置换ipInitSwap(bmsg);/16轮迭代for (int i = 0; i 16; i+)if (i % 2 = 0)/L1=R0CopyArray(

33、rmsgi, lmsgi1, 32);/f(R0,k0)CrypteFunction(rmsgi, i, temp1);/L0+f(R0,k0)Mode2Add(lmsgi, temp1, temp2, 32);/R1=L0+f(R0,k0)CopyArray(temp2, rmsgi1, 32);else/L2=R1CopyArray(rmsgi1, lmsgi, 32);/f(R1,k1)CrypteFunction(rmsgi1, i, temp1);/L1+f(R1,k1)Mode2Add(lmsgi1, temp1, temp2, 32);/R2=L1+f(R1,k1)CopyArr

34、ay(temp2, rmsgi, 32);/逆初始置换ipInitReSwap(bcryptedmsg);/转成字符Bit2Char(bcryptedmsg, cryptedmsg);/数组赋值void DES:CopyArray(bool content, bool empty, int size)for (int i = 0; i size; i+)emptyi = contenti;/解密void DES:Decipher()bool temp132, temp232;/初始置换ipInitSwap(bcryptedmsg);/16轮迭代加密for (int i = 0; i 16; i

35、+)if (i % 2 = 0)/L1=R0CopyArray(rmsgi, lmsgi1, 32);/f(R0,k0)CrypteFunction(rmsgi, 15 - i, temp1);/L0+f(R0,k0)Mode2Add(lmsgi, temp1, temp2, 32);/R1=L0+f(R0,k0)CopyArray(temp2, rmsgi1, 32);else/L2=R1CopyArray(rmsgi1, lmsgi, 32);/f(R1,k1)CrypteFunction(rmsgi1, 15 - i, temp1);/L1+f(R1,k1)Mode2Add(lmsgi1

36、, temp1, temp2, 32);/R2=L1+f(R1,k1)CopyArray(temp2, rmsgi, 32);/逆初始置换ipInitReSwap(bdecipher);/转成字符Bit2Char(bdecipher, decipher);/输出密文void DES:OutPutCryptedMsg()/Bit2Char(bcryptedmsg,cryptedmsg);cout endl 密文:;for (int i = 0; i 8; i+)cout cryptedmsgi ;/输出解密明文/void DES:OutPutDecipher()/Bit2Char(bdecipher,decipher);cout endl 解密:;for (int i = 0; i 8; i+)cout decipheri ;cout endl;int main()/测试数据char msg8;char key8;cout 输入8字节明文: msg;cout 明文:;for (int i = 0; i 8; i+)cout msgi

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁