ImageVerifierCode 换一换
格式:DOCX , 页数:32 ,大小:141.47KB ,
资源ID:2571085      下载积分:1 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2571085.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(des加密算法C++实现.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

des加密算法C++实现.docx

1、des加密算法C+实现DES加密算法的C+实现实验一1、实验题目 利用C/C+编程实现DES加密算法或MD5加密算法。我选择的是用C+语言实现 DES的加密算法。2、实验目的 通过编码实现DES算法或MD5算法,深入掌握算法的加密原理,理解其实际应用 价值,同时要求用C/C+语言实现该算法,让我们从底层开始熟悉该算法的实现过程3、实验环境操作系统:WIN7旗舰版开发工具:Visual Studio 2010旗舰版开发语言:C+4、实验原理 DES加密流程: 如上图所示为DES的加密流程,其中主要包含初始置换,压缩换位1,压缩换位2,扩展置换,S盒置换,异或运算、终结置换等过程。 初始置换是按照

2、初始置换表将64位明文重新排列次序 扩展置换是将原32为数据扩展为48位数据,它主要由三个目的: 1、产生与子密钥相同的长度 2、提供更长的结果,使其在加密过程中可以被压缩 3、产生雪崩效应,使得输入的一位将影响两个替换 S盒置换是DES算法中最核心的内容,在DES中,只有S盒置换是非线性的,它比DES 中其他任何一步都提供更好的安全性 终结置换与初始置换相对应,它们都不影响DES的安全性,主要目的是为了更容易将 明文与密文数据一字节大小放入DES的f算法中 DES解密流程与加密流程基本相同,只不过在进行16轮迭代元算时,将子密钥生成的K的次序倒过来进行迭代运算5、实验过程记录 在对DES算法

3、有了清晰的认识后,编码过程中我将其分为几个关键部分分别进行编码,最后将整个过程按顺序执行,即可完成DES的加密,代码的主要几个函数如下:/Byte转为BitByteToBit(ElemType ch,ElemType bit8) /Bit转为ByteBitToByte(ElemType bit8,ElemType &ch) /初始置换InitialEX(ElemType Inorder64,ElemType Disorder64)/终结置换AntiEx(ElemType Disorder64)/扩展置换ExpandEX(ElemType RightMsg32,ElemType ExpandMs

4、g48)/16轮迭代加密MoveLeft(ElemType C28,ElemType D28,ElemType L032,ElemType R032)/16轮迭代解密mMoveLeft(ElemType C28,ElemType D28,ElemType L032,ElemType R032)/生成48位子密钥GetCD48(ElemType C28,ElemType D28,ElemType Secret48)/48位明文与子密钥进行异或运算XOR(ElemType ExpandMsg48,ElemType Secret48,ElemType Result48)/S盒四位输出getSOut(

5、ElemType Result48,ElemType Sout32)/直接置换DirExchange(ElemType Sout32,ElemType DirOut32)/Li与Ri进行抑或运算XORLR(ElemType DirOut32,ElemType Left32,ElemType Result32)函数执行次序和调用关系关系如下:6、实验结果 1、根据提示,输入任意8字节的原文,并将其转换为64为二进制明文: 2、将64为二进制明文进行初始置换: 3、将原64位明文分成左右两半,并对右半边进行扩展置换: 4依次获得子密钥。按上述流程完成16轮迭代运算后进行终结置换,得到64位密文(整

6、个过程设计数据过多,没有一一输出,只给出最终截图内容) 5、按1开始进行解密,流程与加密相同,中间过程省去,只给出最终结果:7、实验总结 通过完成本实验,我详细了解了DES算法的实现过程和工作原理,相比较在课堂上学到的理论知识,我认为通过实践后掌握的更加深刻。在课堂上我们学习DES算法,很多都是基于理论上的知识,但并没有真正的使用过DES,而通过编码实现DES,我们更好地理解了该怎么用DES去解决实际问题,这对我们以后的学习是很有帮助的,掌握了DES的算法实现后,我们在以后写到相关程序时可以快速的将其利用,使其真正的为我们服务。8、源代码(见附录)附录(DES源代码)/ DES.cpp : 定

7、义控制台应用程序的入口点。/#include stdafx.h#include function.h#include #include#includeusing namespace std;/置换矩阵int IP_EX64= 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

8、, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7;/逆置换矩阵int IP_ANTEX64= 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, 41, 10, 50, 18, 58, 26, 33

9、, 1, 41, 9, 49, 17, 57, 25;/扩展矩阵int EXTEND48= 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, 1, 2;/S盒int S8416= 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7,

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

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

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

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

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

15、 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 DIREX32= 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, 1

16、1, 4, 25;/左移移位表int MOVELEFT16= 1,1,2,2,2,2,2,2, 1,2,2,2,2,2,2,1;/压缩换位表2int CutEX48= 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;typedef char ElemType; ElemType s

17、ubsec1648;/Byte转bitint ByteToBit(ElemType ch,ElemType bit8) for(int index = 7;index = 0;index-) bitindex = (chindex)&1; /cout(int)bitindex; return 0; /bit转Byteint BitToByte(ElemType bit8,ElemType &ch) ElemType tempch=0; ElemType tempbit8; for(int i=0;i=0;index-) tempch=tempch|(tempbitindex(index); c

18、h=tempch; /cout(char)tempchendl; return 0;/bit64转换为Byte8void Bit64ToByte8(ElemType bit64,ElemType ch8) ElemType tempbit8; for(int i=0;i8;i+) for(int j=0;j8;j+) tempbit7-j=biti*8+j; BitToByte(tempbit,chi); /划分为多少块int StringBlock(string msg) int MsgBlock=0; if(msg.length()%8=0) MsgBlock=msg.length()/8

19、; else MsgBlock=msg.length()/8+1; return MsgBlock;/按块取数据void DivideString(string msg,int BlockNum,ElemType ch8) int start=(BlockNum-1)*8; ElemType ch18= msgstart, msgstart+1,msgstart+2,msgstart+3, msgstart+4,msgstart+5,msgstart+6,msgstart+7 ; for(int i=0;i8;i+) chi=ch1i; /按64位分一组void Get64Bit(ElemTy

20、pe ch8,ElemType bit64) ElemType temp8; int count=0; for(int i=0;i8;i+) ByteToBit(chi,temp); for(int j=0;j8;j+) bitcount*8+j=temp7-j; count+; /初始置换void InitialEX(ElemType Inorder64,ElemType Disorder64) for(int i=0;i64;i+) Disorderi=InorderIP_EXi-1; /逆置换void AntiEx(ElemType Disorder64) ElemType temp64

21、; for(int i=0;i64;i+) tempi=Disorderi; for(int i=0;i64;i+) Disorderi=tempIP_ANTEXi-1; /扩展置换void ExpandEX(ElemType RightMsg32,ElemType ExpandMsg48) for(int i=0;i48;i+) ExpandMsgi=RightMsgEXTENDi-1; /16轮加密迭代void MoveLeft(ElemType C28,ElemType D28,ElemType L032,ElemType R032) ElemType Secret48; /子密钥 El

22、emType Result48; /每轮异或结果 ElemType Sout32; /每轮S盒输出 ElemType DirOut32; /直接置换输出 ElemType RResult32; ElemType LResult32; ElemType ExpandMsg48; ElemType temp32; for(int i=0;i32;i+) LResulti=L0i; RResulti=R0i; for(int i=0;i16;i+) if(MOVELEFTi=1) for(int j=0;j27;j+) Ci=Ci+1; C27=0; else for(int j=0;j26;j+)

23、 Ci=Ci+2; C26=0; C27=0; ExpandEX(RResult,ExpandMsg); GetCD48(C,D,Secret); for(int j=0;j48;j+) subsec15-ij=Secretj; /获取界面的子密钥 XOR(ExpandMsg,Secret,Result); /S盒置换 getSOut(Result,Sout); /直接置换 DirExchange(Sout,DirOut); /与L进行异或 XORLR(DirOut,LResult,temp); for(int i=0;i32;i+) LResulti=RResulti; for(int i=

24、0;i32;i+) RResulti=tempi; for(int i=0;i32;i+) L0i=LResulti; R0i=RResulti; /*cout左边endl; for(int i=0;i32;i+) cout(int)LResulti; if(i+1)%8=0) coutendl; cout右边endl; for(int i=0;i32;i+) cout(int)RResulti; if(i+1)%8=0) coutendl; */16轮解密迭代void mMoveLeft(ElemType C28,ElemType D28,ElemType L032,ElemType R03

25、2) ElemType Result48; /每轮异或结果 ElemType Sout32; /每轮S盒输出 ElemType DirOut32; /直接置换输出 ElemType RResult32; ElemType LResult32; ElemType ExpandMsg48; ElemType temp32; for(int i=0;i32;i+) LResulti=L0i; RResulti=R0i; for(int i=0;i16;i+) /*if(MOVELEFTi=1) for(int j=0;j27;j+) Ci=Ci+1; C27=0; else for(int j=0;

26、j26;j+) Ci=Ci+2; C26=0; C27=0; */ ExpandEX(RResult,ExpandMsg); /明文扩展 XOR(ExpandMsg,subseci,Result); /扩展明文与子密钥进行异或 getSOut(Result,Sout); /S盒置换 DirExchange(Sout,DirOut); /直接置换 XORLR(DirOut,LResult,temp); /与L进行异或 for(int j=0;j32;j+) LResultj=RResultj; for(int j=0;j32;j+) RResultj=tempj; for(int i=0;i32;i+) L0i=LResulti; R0i=RResulti; /*cout左边endl; for(int i=0;i32;i+) cout(int)LResulti; if(i+1)%8=0) coutendl; cout右边endl; for(int i=0;i32;i+) cout(int)RResulti; if(i+1)%8=

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2