




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验1 DES算法的实现实验目的:验证DES算法的加密、解密计算过程和效果实验内容:使用VC+6.0编制一个C+程序(控制台界面),该程序可以对单一64比特分组进行DES加密或者解密。算法的接口设计中包括密钥编排(子密钥生成),单一分组的加密变换和解密变换。报告要求:(1)在学校统一印刷的实验报告纸上填写实验题目、实验目的和实验内容,以及总结一下编程中遇到的问题和重要的解决方法。()将源程序和程序运行实例打印,装订在实验报告纸后面. 其中程序运行实例中要求加密时的明文为ffffffffffffffff,密钥为0000000000000000; 解密时的密文为0000000000000000,密钥为ffffffffffffffff.提示:参考如下的程序结构和数据(算法中需要用到的各种表格可以从本文件中拷贝)以及程序运行实例,调试程序时为了确保自己程序的正确性,可以采用该程序运行实例中出现的明文、密文和密钥作为测试数据。#include #include class DESpublic:DES() void setkey(const unsigned char *);void enctransform(const unsigned char *,unsigned char *);void dectransform(const unsigned char *,unsigned char *);private:bool enckey1648;/存放加密子密钥bool deckey1648;/存放解密子密钥/ initial permutation IPconst static unsigned char IP_Table64;/ final permutation IP-1 const static unsigned char IPR_Table64;/ expansion operation matrixconst static unsigned char E_Table48;/ 32-bit permutation function P used on the output of the S-boxes const static unsigned char P_Table32;/ permuted choice table (key) const static unsigned char PC1_Table56;/ permuted choice key (table) const static unsigned char PC2_Table48;/ number left rotations of pc1 const static unsigned char LOOP_Table16;/ The S-boxes const static unsigned char S_Box8416;/加入其他私有成员函数;const unsigned char DES:IP_Table64 = 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;/ final permutation IP-1 const unsigned char DES: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, 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;/ expansion operation matrixconst unsigned char DES:E_Table48 = 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;/ 32-bit permutation function P used on the output of the S-boxes const unsigned char DES:P_Table32 = 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;/ permuted choice table (key) const unsigned char DES:PC1_Table56 = 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;/ permuted choice key (table) const unsigned char DES:PC2_Table48 = 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;/ number left rotations of pc1 const unsigned char DES:LOOP_Table16 = 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1;/ The S-boxes const unsigned char DES:S_Box8416 = / 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;void DES:setkey( unsigned char const *userkey/*用户输入的64位密钥首地址*/ ) /密钥扩展算法,即由主密钥生成子密钥,参见教材44-45页void DES:enctransform( const unsigned char *In /*待解密的64位数据首地址*/,unsigned char *Out ) /加密变换算法,参见教材42、43、46页void DES:dectransform( const unsigned char *In /*待加密的64位数据首地址*/, unsigned char *Out)/解密变换算法,参见教材48页int main()unsigned int buf8;char choice= ;char buffer17;unsigned char key9, plaintext9,ciphertext9;DES mydes;int ii;while(true)doprintf(Please make a choice:n 1.encryptionn 2.decryptionn 3.exitn);scanf(n%c,&choice);while(choice!=1&choice!=2&choice!=3);switch(choice)case 1:printf(Please input key(length=16):n*n);scanf(%s,buffer);sscanf(buffer,%2x%2x%2x%2x%2x%2x%2x%2x,&buf0,&buf1,&buf2,&buf3,&buf4,&buf5,&buf6,&buf7);for(ii=0;ii8;ii+)keyii=bufii;printf(Please input plaintext(length=16):n);scanf(%s,buffer);sscanf(buffer,%2x%2x%2x%2x%2x%2x%2x%2x,&buf0,&buf1,&buf2,&buf3,&buf4,&buf5,&buf6,&buf7);for(ii=0;ii8;ii+)plaintextii=bufii;mydes.setkey(key);mydes.enctransform(plaintext,ciphertext);for(ii=0;ii8;ii+)bufii=ciphertextii;printf(ciphertext:n%02x%02x%02x%02x%02x%02x%02x%02xn,buf0,buf1,bu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 自动化销售预测创新创业项目商业计划书
- 电商数据加密系统创新创业项目商业计划书
- 海外汽车设计工作室探访创新创业项目商业计划书
- 海外汽车市场动态创新创业项目商业计划书
- 2025年甘肃培黎职业学院招聘考试笔试试题(含答案)
- 自动化医学影像分析创新创业项目商业计划书
- 2025年游戏化营销助力餐饮品牌传播:案例分析与效果评估报告
- 2025年新能源微电网在能源互联网中的稳定性风险评估报告
- 2025年职业教育创新模式深度分析报告
- 辽宁省大连市庄河高级中学2026届高一化学第一学期期中联考模拟试题含解析
- 无人机原理课件
- 1.1 精微广大-绘画的功能和种类 教学设计-2023-2024学年高中美术人美版(2019)选择性必修1 绘画
- 2025年河北高考生物试卷真题答案详解及备考指导
- 宁德新能源verify测试题库
- 2023年甘肃公交建集团清傅项目收费运营工作人员招聘笔试真题
- 2022年红河产业投资集团有限公司招聘笔试题库及答案解析
- 肺心病(课)课件
- 中国烟草PPT模板
- 急性淋巴管炎的护理查房
- 江苏省综合评标专家库题库
- 农作物品种试验技术规程玉米
评论
0/150
提交评论