版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、信息安全工程(gngchng)课程实验报告编程实现AES算法(sun f)的加密解密过程课程名称:信息安全工程(gngchng)学生姓名:学生学号:专业班级:任课教师:蔡忠闽 目录(ml)TOC o 1-3 h u HYPERLINK l _Toc373589926 1 背景(bijng)与意义 PAGEREF _Toc373589926 h 3 HYPERLINK l _Toc373589927 2 实验(shyn)要求 PAGEREF _Toc373589927 h 3 HYPERLINK l _Toc373589928 3.系统设计 PAGEREF _Toc373589928 h 4 H
2、YPERLINK l _Toc373589929 3.1系统主要目标 PAGEREF _Toc373589929 h 4 HYPERLINK l _Toc373589930 3.2主要软件需求(运行环境) PAGEREF _Toc373589930 h 4 HYPERLINK l _Toc373589931 4 AES算法加密和解密原理分析 PAGEREF _Toc373589931 h 4 HYPERLINK l _Toc373589932 4.1加密过程基本变换 PAGEREF _Toc373589932 h 6 HYPERLINK l _Toc373589933 4.2解密过程基本变换
3、PAGEREF _Toc373589933 h 6 HYPERLINK l _Toc373589934 4.3密钥扩展算法 PAGEREF _Toc373589934 h 7 HYPERLINK l _Toc373589935 5.系统功能程序设计 PAGEREF _Toc373589935 h 7 HYPERLINK l _Toc373589936 6. 测试报告 PAGEREF _Toc373589936 h 17 HYPERLINK l _Toc373589937 6.1 加密 PAGEREF _Toc373589937 h 17 HYPERLINK l _Toc373589938 6.
4、2 解密 PAGEREF _Toc373589938 h 19 HYPERLINK l _Toc373589939 7 实验总结 PAGEREF _Toc373589939 h 211 背景(bijng)与意义科技(kj)的发展使计算机深入到了我们生活的的方方面面,计算机在带来方便和提高了工作效率的同时却也带来了各种各样的新问题,其中信息安全问题最为突出,随着计算机信息安全要求的不断提高, 计算机保密系统(xtng)已变得越来越重要,所以我们需要一种较为可靠的加密算法来保护我们信息的安全。由于DES已经无法满足高保密性的要求,美国于1997年1月开始征集新一代数据加密标准(即高级数据加密标准,
5、Advanced Encryption Standard,AES)。2000年10月2日,正式宣布选择比利时密码学家所开发的Rijndael算法成为AES的最终算法。AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和政府数字信息的方法。AES 是一个新的可以用于保护电子数据的加密算法。明确地说,AES 是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据。与公共密钥密码使用密钥对不同,对称密钥密码使用相
6、同的密钥加密和解密数据。通过分组密码返回的加密数据的位数与输入 数据相同。迭代加密使用一个循环结构,在该循环中重复置换和替换输入数据。该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael之命名之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 Rhine doll。)AES在软体及硬件上都能快速地加解密,相对来说较易于实作,且只需要很少的记忆体。作为一个新的加密标准,目前正被部署应用到更广大的范围.2 实验要求(1)一人一组 (2)AES算法加密和解密原理分析 (3)提交完整的源程序(使用网络上的现成程序一定要注明出处
7、) (4)提交程序的设计,说明文档 (5)用ECB和分组链模式CBC,对给定内容进行加密和解密,提交解密结果,并提交实验心得,着重阐述对实验的理解(6)程序调试成功后,处理以下内容:A、AES 加密明文:各自的姓名+邮箱秘钥:各自的手机号B、AES解密密文:邮箱附件给出秘钥:(给出秘钥为ASCII码形式(xngsh),请将ASCII码转为二进制,不满128位在其后补零):130729810633.系统(xtng)设计3.1系统(xtng)主要目标1在深入理解AES加密/解密算法理论的基础上,设计一个AES加密/解密软件系统;2完成一个明文分组的加解密,明文和密钥是十六进制,输入明文和密钥,输出
8、密文,进行加密后,能够进行正确的解密;3. 按要求提供所设计系统的报告及完整的软件。3.2主要软件需求(运行环境)本软件适用VC语言编写,编译成功后的EXE文件可以在装有windows系统的任何计算机上使用。测试平台:Windows 8使用软件:Visual Studio 2012/C+4 AES算法加密和解密原理分析AES是一个迭代的、对称密钥分组的密码,它可以使用128、192和256位密钥长度,并且用128位分组长度加密和解密数据2。该算法输入分组、输出分组、状态长度均为128比特。对于AES算法的运算是在一个称为状态的二维字节数组上进行。一个状态由四行组成,每一行包括Nb个字节,Nb等
9、于分组长度除以32,AES分组长度为128位,因此,Nb=4,该值反应了状态中32-bit字的个数(列数);密钥长度128、192和256位可分别表示为Nk=4、6或8,反应了密钥中32-bit字的个数(列数)。而AES算法的轮数Nr仅依赖于密钥长度Nk,轮数和密钥长度的关系可以表示为:Nr=6+Nk。密钥长度分组长度轮数的关系如表1所示。表1 Key-Block-Round关系密钥长度(Nk words)分组长度(Nb words)轮数(Nr)441064128414对于加密和解密变换,AES算法使用的轮函数由4个不同的以字节为基本单位的变换复合而成,该过程由四个不同的阶段组成3:(1)S盒
10、变换,用一个S盒完成分组中的按字节代替;(2)行移位变换,一个简单的置换;(3)列混淆变换,一个利用在域GF(28) 上的算术性的代替;(4)轮密钥加变换,一个利用当前分组和扩展密钥的一个部分进行按位异或。AES对数据的加密过程是通过把输入的明文和密钥由轮函数经Nr轮迭代来实现的,结尾轮与前Nr-1轮不同。前Nr-1轮依次进行S盒变换、行移位(y wi)变换、列混淆变换和轮密钥加变换;结尾轮与前Nr-1轮相比去掉了列混淆变换。而解密过程与加密过程相反,通过把输入的密文和密钥由轮函数经Nr轮迭代(di di)来实现的,结尾轮与前Nr-1轮不同。前Nr-1轮依次进行逆行移位变换、逆S盒变换、轮密钥
11、加变换和逆列混淆变换;结尾轮与前Nr-1轮相比去掉了逆列混淆变换。AES算法的加密解密(ji m)过程如图1所示:图1 AES加密(ji m)解密流程4.1加密过程基本(jbn)变换(1) S盒变换(binhun)S盒变换(binhun)是一个基于S盒的非线性置换,它用于输入或中间态的每一个字节通过一个简单的查表操作,将其映射为另一个字节。映射方法是:把输入字节的高4位作为S盒的行值,低4位作为列值,然后取出S盒中对应行和列的元素作为输出。例如,输入为“89”(十六进制(sh li jn zh)的值所对应的S盒的行值为“8”,列值为“9”,S盒中相应(xingyng)位置的值为“a7”,就说明
12、“89”被映射为“87”。 (2) 列混淆变换列混淆变换实现逐列混淆,其方法是: 其中,内的数表示是字节。用矩阵表示为(3) 行移位变换行移位变换完成基于行的循环移位操作,即行移位变换的作用在中间态的行上,第0行不动,第1行循环左移1个字节,第2行循环左移2个字节,第3行循环左移3个字节。(4) 轮密钥加变换轮密钥加变换用于将输入或中间态S的每一列与一个密钥字Wi进行按位异或,其中, 由原始密钥通过密钥扩展算法产生。4.2解密过程基本变换解密过程是加密的逆过程,S盒变换、行移位变换、列混淆变换都要进行求逆变换,即逆S盒变换、逆行移位变换、逆列混淆变换。而轮密钥加变换与加密过程相同。(1) 逆S
13、盒变换与S盒变换类似,逆S盒变换是基于逆S盒实现的。(2) 逆行移位变换与行移位变换相反,逆行移位变换将态State的后三行按相反的方向进行移位操作,即第0行保持不变,第1行向右移1个字节,第2行向右移2个字节,第3行向右移3个字节。(3) 逆列混淆变换逆列混淆变换的处理方法与列混淆变换类似,每一列都通过与一个固定的多项相乘进行变换。写为矩阵乘法的形式,逆列混淆变换按以下方式对列进行变换:4.3密钥扩展(kuzhn)算法AES加密解密(ji m)过程中,每一轮都需要一个与输入分组具有相同长度的扩展密钥Wi的参与。由于外部输入的加密密钥长度有限,所以在算法中要用一个密钥扩展程序把外部密钥扩展成更
14、长的比特串,以生成各轮的加密和解密密钥。通过生成器产生(chnshng)Nr+1轮轮密钥,每个轮密钥由Nb个字组成,共有Nb(Nr+1)个字Wi,i=0,1,Nb(Nr+1)-1。5.系统功能程序设计(代码参考自CSDN论坛)#include StdAfx.h#include (AES加解密).h#include#include#includebyte in4Nb; /in数组用于读入需要或解密的数据byte key4Nk; /key数组用于读入初始密钥 byte wNr+1Nk4; /w数组用于保存所有扩展后的密钥 /S盒byte S_box256=0 x63,0 x7c,0 x77,0 x
15、7b,0 xf2,0 x6b,0 x6f,0 xc5,0 x30,0 x01,0 x67,0 x2b,0 xfe,0 xd7,0 xab,0 x76,0 xca,0 x82,0 xc9,0 x7d,0 xfa,0 x59,0 x47,0 xf0,0 xad,0 xd4,0 xa2,0 xaf,0 x9c,0 xa4,0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x48,0 x03,0 xf6,0 x0e,0 x61,0 x35,0 x57,0 xb9,0 x86,0 xc1,0 x1d,0 x9e,0 xe1,0 xf8,0 x98,0 x11,0 x69,0 xd9,0 x8e,0 x94,0 x9b,0 x1e,0 x87,0 xe9,0 xce,0 x55,0 x28,0 xdf,0 x8c,0 xa1,0 x89,0 x0d,0 xbf,0 xe6,0 x42,0 x68,0 x41,0 x99,0 x2d,0 x0f,0 xb0,0 x
20、54,0 xbb,0 x16;/逆S盒byte InvS_box256 =0 x52,0 x09,0 x6a,0 xd5,0 x30,0 x36,0 xa5,0 x38,0 xbf,0 x40,0 xa3,0 x9e,0 x81,0 xf3,0 xd7,0 xfb,0 x7c,0 xe3,0 x39,0 x82,0 x9b,0 x2f,0 xff,0 x87,0 x34,0 x8e,0 x43,0 x44,0 xc4,0 xde,0 xe9,0 xcb,0 x54,0 x7b,0 x94,0 x32,0 xa6,0 xc2,0 x23,0 x3d,0 xee,0 x4c,0 x95,0 x0b
21、,0 x42,0 xfa,0 xc3,0 x4e,0 x08,0 x2e,0 xa1,0 x66,0 x28,0 xd9,0 x24,0 xb2,0 x76,0 x5b,0 xa2,0 x49,0 x6d,0 x8b,0 xd1,0 x25,0 x72,0 xf8,0 xf6,0 x64,0 x86,0 x68,0 x98,0 x16,0 xd4,0 xa4,0 x5c,0 xcc,0 x5d,0 x65,0 xb6,0 x92,0 x6c,0 x70,0 x48,0 x50,0 xfd,0 xed,0 xb9,0 xda,0 x5e,0 x15,0 x46,0 x57,0 xa7,0 x8d
22、,0 x9d,0 x84,0 x90,0 xd8,0 xab,0 x00,0 x8c,0 xbc,0 xd3,0 x0a,0 xf7,0 xe4,0 x58,0 x05,0 xb8,0 xb3,0 x45,0 x06,0 xd0,0 x2c,0 x1e,0 x8f,0 xca,0 x3f,0 x0f,0 x02,0 xc1,0 xaf,0 xbd,0 x03,0 x01,0 x13,0 x8a,0 x6b,0 x3a,0 x91,0 x11,0 x41,0 x4f,0 x67,0 xdc,0 xea,0 x97,0 xf2,0 xcf,0 xce,0 xf0,0 xb4,0 xe6,0 x73
23、,0 x96,0 xac,0 x74,0 x22,0 xe7,0 xad,0 x35,0 x85,0 xe2,0 xf9,0 x37,0 xe8,0 x1c,0 x75,0 xdf,0 x6e,0 x47,0 xf1,0 x1a,0 x71,0 x1d,0 x29,0 xc5,0 x89,0 x6f,0 xb7,0 x62,0 x0e,0 xaa,0 x18,0 xbe,0 x1b,0 xfc,0 x56,0 x3e,0 x4b,0 xc6,0 xd2,0 x79,0 x20,0 x9a,0 xdb,0 xc0,0 xfe,0 x78,0 xcd,0 x5a,0 xf4,0 x1f,0 xdd
24、,0 xa8,0 x33,0 x88,0 x07,0 xc7,0 x31,0 xb1,0 x12,0 x10,0 x59,0 x27,0 x80,0 xec,0 x5f,0 x60,0 x51,0 x7f,0 xa9,0 x19,0 xb5,0 x4a,0 x0d,0 x2d,0 xe5,0 x7a,0 x9f,0 x93,0 xc9,0 x9c,0 xef,0 xa0,0 xe0,0 x3b,0 x4d,0 xae,0 x2a,0 xf5,0 xb0,0 xc8,0 xeb,0 xbb,0 x3c,0 x83,0 x53,0 x99,0 x61,0 x17,0 x2b,0 x04,0 x7e
25、,0 xba,0 x77,0 xd6,0 x26,0 xe1,0 x69,0 x14,0 x63,0 x55,0 x21,0 x0c,0 x7d;/表alog256是生成元表示(biosh)到多项式的映射表。使用的生成元为x+1byte alog256 =0 x01,0 x03,0 x05,0 x0f,0 x11,0 x33,0 x55,0 xff,0 x1a,0 x2e,0 x72,0 x96,0 xa1,0 xf8,0 x13,0 x35,0 x5f,0 xe1,0 x38,0 x48,0 xd8,0 x73,0 x95,0 xa4,0 xf7,0 x02,0 x06,0 x0a,0 x1
26、e,0 x22,0 x66,0 xaa,0 xe5,0 x34,0 x5c,0 xe4,0 x37,0 x59,0 xeb,0 x26,0 x6a,0 xbe,0 xd9,0 x70,0 x90,0 xab,0 xe6,0 x31,0 x53,0 xf5,0 x04,0 x0c,0 x14,0 x3c,0 x44,0 xcc,0 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f2,0 x0d,0 x17,0 x39,0 x4b,0 xdd,0 x7c,0 x84,0 x97,0 xa2,0 xfd,0 x1c,0 x24,0 x6c,0 xb4,0 xc7,0 x52,0 xf6,0 x01;/log256是多项式表示(biosh)到生成元的映射表。使用的生成元为x+1byte log256 = 0 x00,0 x00,0 x19,0 x01,0 x32,0 x02,0 x1a,0 xc6,0 x4b,0 xc7,0 x1b,0 x68,0 x33,0 xee
31、,0 xdf,0 x03,0 x64,0 x04,0 xe0,0 x0e,0 x34,0 x8d,0 x81,0 xef,0 x4c,0 x71,0 x08,0 xc8,0 xf8,0 x69,0 x1c,0 xc1,0 x7d,0 xc2,0 x1d,0 xb5,0 xf9,0 xb9,0 x27,0 x6a,0 x4d,0 xe4,0 xa6,0 x72,0 x9a,0 xc9,0 x09,0 x78,0 x65,0 x2f,0 x8a,0 x05,0 x21,0 x0f,0 xe1,0 x24,0 x12,0 xf0,0 x82,0 x45,0 x35,0 x93,0 xda,0 x8e
32、,0 x96,0 x8f,0 xdb,0 xbd,0 x36,0 xd0,0 xce,0 x94,0 x13,0 x5c,0 xd2,0 xf1,0 x40,0 x46,0 x83,0 x38,0 x66,0 xdd,0 xfd,0 x30,0 xbf,0 x06,0 x8b,0 x62,0 xb3,0 x25,0 xe2,0 x98,0 x22,0 x88,0 x91,0 x10,0 x7e,0 x6e,0 x48,0 xc3,0 xa3,0 xb6,0 x1e,0 x42,0 x3a,0 x6b,0 x28,0 x54,0 xfa,0 x85,0 x3d,0 xba,0 x2b,0 x79
33、,0 x0a,0 x15,0 x9b,0 x9f,0 x5e,0 xca,0 x4e,0 xd4,0 xac,0 xe5,0 xf3,0 x73,0 xa7,0 x57,0 xaf,0 x58,0 xa8,0 x50,0 xf4,0 xea,0 xd6,0 x74,0 x4f,0 xae,0 xe9,0 xd5,0 xe7,0 xe6,0 xad,0 xe8,0 x2c,0 xd7,0 x75,0 x7a,0 xeb,0 x16,0 x0b,0 xf5,0 x59,0 xcb,0 x5f,0 xb0,0 x9c,0 xa9,0 x51,0 xa0,0 x7f,0 x0c,0 xf6,0 x6f
34、,0 x17,0 xc4,0 x49,0 xec,0 xd8,0 x43,0 x1f,0 x2d,0 xa4,0 x76,0 x7b,0 xb7,0 xcc,0 xbb,0 x3e,0 x5a,0 xfb,0 x60,0 xb1,0 x86,0 x3b,0 x52,0 xa1,0 x6c,0 xaa,0 x55,0 x29,0 x9d,0 x97,0 xb2,0 x87,0 x90,0 x61,0 xbe,0 xdc,0 xfc,0 xbc,0 x95,0 xcf,0 xcd,0 x37,0 x3f,0 x5b,0 xd1,0 x53,0 x39,0 x84,0 x3c,0 x41,0 xa2
35、,0 x6d,0 x47,0 x14,0 x2a,0 x9e,0 x5d,0 x56,0 xf2,0 xd3,0 xab,0 x44,0 x11,0 x92,0 xd9,0 x23,0 x20,0 x2e,0 x89,0 xb4,0 x7c,0 xb8,0 x26,0 x77,0 x99,0 xe3,0 xa5,0 x67,0 x4a,0 xed,0 xde,0 xc5,0 x31,0 xfe,0 x18,0 x0d,0 x63,0 x8c,0 x80,0 xc0,0 xf7,0 x70,0 x07;/密钥扩展(kuzhn)中使用的异或轮常数byte Rcon104=0 x01,0 x00,0
36、 x00,0 x00,0 x02,0 x00,0 x00,0 x00,0 x04,0 x00,0 x00,0 x00,0 x08,0 x00,0 x00,0 x00,0 x10,0 x00,0 x00,0 x00,0 x20,0 x00,0 x00,0 x00,0 x40,0 x00,0 x00,0 x00,0 x80,0 x00,0 x00,0 x00,0 x1b,0 x00,0 x00,0 x00,0 x36,0 x00,0 x00,0 x00;/轮密钥加void AddRoundKey(byte state4Nb,byte p4Nb)int i, j;for(i = 0; i 4; i+
37、)for(j = 0; j Nb; j+)stateij = stateijpij;/状态(zhungti)数组与每一轮子密钥异或/字节(z ji)替换void SubBytes(byte state4Nb)int i, j;for(i = 0; i 4; i+)for(j = 0; j Nb; j+)stateij = S_boxstateij;/行移位(y wi)void ShiftRows(byte state4Nb) /void memcpy(void *destin,void *source,unsigned n) 操作为从source复制一个n个字节的块到destinbyte te
38、mp4;int time; /向左循环移位的位数for(time = 1; time 4; time+) memcpy(temp, statetime, time);/先存储需要左移的位到temp中memcpy(statetime, statetime+time, 4-time);/将余下的块左移memcpy(statetime+4-time, temp, time);/将事先存于temp中的位放到状态数组中 /列混合中使用的mul函数byte mul(byte a, byte b)if(a != 0 & b != 0)return alog(loga + logb) % 255;/参考代码,
39、不太懂else return 0;/列混合void MixColumns(byte state4Nb)byte temp4Nb;int i, j;/将state函数存入temp中for(i = 0; i 4; i+)for(j = 0; j Nb; j+)tempij = stateij;/按列做调用mul函数做列混合,以状态的每一列作为(zuwi)输入与混合每一列的数据得到新的列for(i = 0; i 4; i+)state0i = mul(0 x02, temp0i) mul(0 x03, temp1i) temp2i temp3i;state1i = mul(0 x02, temp1i
40、) mul(0 x03, temp2i) temp0i temp3i; state2i = mul(0 x02, temp2i) mul(0 x03, temp3i) temp1i temp0i;state3i = mul(0 x02, temp3i) mul(0 x03, temp0i) temp2i temp1i;/字节(z ji)变换 密钥扩展中使用void SubWords(byte aNk)int i;for(i = 0; i Nk; i+)ai = S_boxai;/左循环移位(y wi)(只移一位) 密钥扩展中使用void RotWord(byte tempNb)byte tem
41、p1;int i;temp1 = temp0;for(i = 1; i Nb; i+)tempi-1 = tempi;tempNb-1 = temp1;/密钥扩展void KeyExpansion(byte key4Nk,byte wNr+1Nk4)byte kNb * (Nr + 1)4; /计算是用二维数组(便于计算),最后存于三维数组w中byte tempNk;byte b 44;int i,j,l; /存储初始密钥for(i = 0; i 4; i+)for(j = 0; j Nk; j+)kij = keyij;/计算扩展密钥for(i = Nk; i Nb * (Nr + 1);
42、i+)for(j = 0; j 4; j+)tempj = ki - 1j;if(i%Nk = 0)RotWord(temp);SubWords(temp);for(j = 0; j 4 ; j+)tempj = tempjRconi/Nk-1j;for(j = 0; j 4; j+) kij = ki-Nkjtempj;printf(%x , kij);printf(n);for (i = 0; i Nr + 1; i+)for (j = 0; j Nk; j+)for (l = 0; l 4; l+)wijl = k4 * i + jl;for (j = 0; j Nk; j+)for (
43、l = 0; l 4; l+)bjl = wilj;for (j = 0; j Nk; j+)for (l = 0; l 4; l+)wijl = bjl;/加密(ji m)模块void Cipher(char file164, char file264)byte state4Nb;int i, j, k;int round = 0;byte wNr+1Nk4;byte out4Nb;FILE *fp;OpenFile(file1, file2); /从文件中读取初始(ch sh)密钥和明文 KeyExpansion(key,w); /调用密钥扩展(kuzhn)函数将所有的扩展密钥存于w数组中
44、/将读入的明文进行转秩for(i = 0; i 4; i+)for(j = 0; j Nb; j+)stateji = inij; /将初始(ch sh)明文和初始密钥存于文件中并在程序中输出 if (fp = fopen(加密要求(yoqi)输出结果.txt, w) = NULL)AfxMessageBox(打开文件(wnjin)错误!);exit(0);fprintf(fp,初始明文:n);for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j Nb; j+)fprintf(fp,%0 x ,stateij);fprintf(fp, nn初始密钥:n);
45、for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j Nb; j+)fprintf(fp,%0 x ,w0ji);AddRoundKey(state,wround); /初始轮密钥加 /开始循环Nr轮 for(round = 1; round Nr; round+)SubBytes(state);ShiftRows(state);MixColumns(state);AddRoundKey(state, wround);if (round = 8)fprintf(fp, nn第九轮轮密钥:n);for (j = 0; j 4; j+)fprintf(fp,
46、nw%d= , (round+1)*4+j);for (k = 0; k 4; k+)fprintf(fp, %x , wround+1kj);fprintf(fp, nn明文加密到第九轮:n);for (j = 0; j 4; j+)fprintf(fp, n);for (k = 0; k 4; k+)fprintf(fp, %x , statejk); SubBytes(state); ShiftRows(state); AddRoundKey(state, wround); fprintf(fp, nn第十轮轮密钥:n);for (j = 0; j 4; j+) fprintf(fp,
47、nw%d= ,round*4+j);for (k = 0; k 4; k+)fprintf(fp, %x , wroundkj);fprintf(fp, nn加密(ji m)完成后输出:n);for (j = 0; j 4; j+)fprintf(fp, n);for (k = 0; k 4; k+)fprintf(fp, %x , statejk);fclose(fp);if (fp = fopen(CipherMessage.txt, w) = NULL)AfxMessageBox(打开(d ki)文件错误!);exit(0); for(i = 0; i 4; i+)for(j = 0;
48、j Nb; j+)outji = stateij;for(i = 0; i 4; i+)for(j = 0; j 0, position 4; time-, position+)memcpy(temp, stateposition, time);memcpy(stateposition, stateposition+time, 4-time);memcpy(stateposition+4-time, temp, time);/逆字节(z ji)替代void InvSubBytes(byte state4Nb)int i, j;for(i = 0; i 4; i+)for(j = 0; j Nb
49、; j+)stateij = InvS_boxstateij;/逆列混合(hnh)void InvMixColumns(byte state4Nb) byte temp4Nb;int i, j;for(i = 0; i 4; i+)for(j = 0; j Nb; j+)tempij = stateij;for(i = 0; i 4; i+)state0i = mul(0 x0e, temp0i) mul(0 x0b, temp1i) mul(0 x0d, temp2i) mul(0 x09, temp3i);state1i = mul(0 x0e, temp1i) mul(0 x0b, te
50、mp2i) mul(0 x09, temp0i) mul(0 x0d, temp3i); state2i = mul(0 x0e, temp2i) mul(0 x0b, temp3i) mul(0 x09, temp1i) mul(0 x0d, temp0i);state3i = mul(0 x0e, temp3i) mul(0 x0b, temp0i) mul(0 x09, temp2i) mul(0 x0d, temp1i);/解密(ji m)模块void InvCipher(char file164, char file264)byte state4Nb;byte wNr+1Nk4;by
51、te out4Nb;int i, j, k;FILE *fp;int round = 10;OpenFile(file1, file2);KeyExpansion(key,w);for(i = 0; i 4; i+)for(j = 0; j Nb; j+)stateji = inij;/将初始密文和初始密钥存入文件(wnjin)中并在程序中输出 if (fp = fopen(解密(ji m)要求输出结果.txt, w) = NULL)AfxMessageBox(打开(d ki)文件错误!);exit(0);fprintf(fp,初始密文:n);for(i = 0; i 4; i+)fprintf(fp,n);for(j = 0; j Nb; j+)fprintf(fp,%0 x ,stateij);fprintf(fp, nn初始密钥:n);for(i = 0; i 4; i+)fprintf(fp,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 艺术节的开幕词(范文9篇)
- 小学语文经典诵读教学方法的探索与实践
- 知识竞赛活动方案模板合集6篇
- 2025年度股权转让及管理咨询合同3篇
- 2025年度纺织设备转让与工艺优化服务协议2篇
- 2024版标准化质量保证协议样本版B版
- 2024版房屋买卖合同详细条款
- 二零二五年度离婚协议书律师见证合同3篇
- 家用智能健身设备的应用与推广
- 2024版购房协议范本下载2篇
- 育婴师服务合同协议书(2024版)
- 情侣分手经济协议书范本
- 心理健康教育国内外研究现状
- 伟大的《红楼梦》智慧树知到期末考试答案章节答案2024年北京大学
- 广州社会保险退款申请表
- 2024年知识竞赛-竞彩知识笔试参考题库含答案
- 高效协同-培训课件
- 20XX年市场洞察模板
- 2023-2024学年《Web开发基础》试卷及答案解析
- 标准化考场方案
- 遥感技术在地表水源地水体监测中的应用研究
评论
0/150
提交评论