AES加密算法实验报告.doc

上传人:李司机 文档编号:1163770 上传时间:2022-07-11 格式:DOC 页数:16 大小:1.51MB
返回 下载 相关 举报
AES加密算法实验报告.doc_第1页
第1页 / 共16页
AES加密算法实验报告.doc_第2页
第2页 / 共16页
AES加密算法实验报告.doc_第3页
第3页 / 共16页
AES加密算法实验报告.doc_第4页
第4页 / 共16页
AES加密算法实验报告.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《AES加密算法实验报告.doc》由会员分享,可在线阅读,更多相关《AES加密算法实验报告.doc(16页珍藏版)》请在三一办公上搜索。

1、-大学计算机学院、软件学院实验报告课程名称密码学与网络平安实验课时2实验工程AES加密算法实验时间实验目的完成AES加密算法,实现图片加密与解密,并将加密后的结果以图片格式保存。实验环境PC机,Windows7操作系统,Visual C+ 6.0实验容算法、程序、步骤和方法一、 简介美国国家标准技术研究所在2001年发布了高级加密标准AES。AES是一个对称加密算法,旨在取代DES成为广泛使用的标准。AES中的所有运算都是在8为的字节上运行的。特别饿,加减乘除算术都是在有限域GF(28)上运行的。二、 程序特点本次试验中要求对图片进展加密与解密,并将加密结果以图片格式进展保存。因此为了实现对图

2、片的调度及保存,使用头文件atlimage.h进展对图片的操作,实现对图片的像素读取,图片的保存。在程序运行读取需要加密的图片时,需要进展图片的选取,本次实验中使用在弹窗中选取文件的方式,使用头文件mdlg.h来实现在文件夹中选择需要的文件的选取。三、 加密算法流程AES加密算法流程如下字节代替:用一个S盒完成分组的字节到字节的代替;行移位:进展一次行上的置换;列混合:利用有限域GF(28)上的运算特性的一个代替;轮密钥加:当前分组和扩展密钥的一局部进展按位异或。四、 代码实现cryptograph.h*include*includeclassplainte*tpublic:plainte*t

3、();staticvoid createplainte*t(unsignedchar a);staticvoid SubBytes(unsignedchar p16);staticvoid inSubBytes(unsignedchar p16);staticvoid ShiftRows(unsignedchar e);staticvoid inShiftRows(unsignedchar e);staticvoid Matri*ToByte(unsignedchar e);staticvoid inMatri*ToByte(unsignedchar e);staticunsignedchar

4、 FFmul(unsignedchar a, unsignedchar b);staticvoid KeyAdding(unsignedchar state16, unsignedchar k4);staticvoid KeyE*pansion(unsignedchar* key, unsignedchar w44);plainte*t();private:;cryptograph.cpp*includecryptography.husingnamespace std;staticunsignedchar sBo* = ;/定义加密S盒/unsignedchar insBo*256 =;/定义

5、解密S盒plainte*t:plainte*t()voidplainte*t:createplainte*t(unsignedchara)/创立明文int i = 0;unsignedint p16;for (int j = 0; j200; j+)if (aj = 0)break;for (; i16; i+)pi = ai;ai = ai + 16;voidplainte*t:SubBytes(unsignedcharp16)/字节变换函数unsignedchar b16;for (int i = 0; i16; i+)bi = sBo*(int)pi;voidplainte*t:inSu

6、bBytes(unsignedcharp16)/逆字节变换函数unsignedchar b16;for (int i = 0; i16; i+)bi = insBo*(int)pi;voidplainte*t:ShiftRows(unsignedchare)/行移位变换函数unsignedchar t4;for (int i = 1; i4; i+)for (int * = 0; *4; *+)t* = e* + i * 4;for (int y = 0; y4; y+)e(y + 4 - i) % 4 + i * 4 = ty;voidplainte*t:inShiftRows(unsign

7、edchare)/逆行移位变换函数unsignedchar t4;for (int i = 1; i4; i+)for (int * = 0; *4; *+)t* = e* + i * 4;for (int y = 0; y4; y+)e(y + i) % 4 + i * 4 = ty;voidplainte*t:Matri*ToByte(unsignedchare)/列混合变换函数unsignedchar t4;int r, c;for (c = 0; c 4; c+)for (r = 0; r4; r+)tr = er * 4 + c;for (r = 0; r4; r+)er * 4 +

8、 c = FFmul(0*02, tr) FFmul(0*03, t(r + 1) % 4) FFmul(0*01, t(r + 2) % 4) FFmul(0*01, t(r + 3) % 4);voidplainte*t:inMatri*ToByte(unsignedchare)/逆列混合变换函数unsignedchar t4;int r, c;for (c = 0; c 4; c+)for (r = 0; r4; r+)tr = er * 4 + c;for (r = 0; r4; r+)er * 4 + c = FFmul(0*0e, tr) FFmul(0*0b, t(r + 1)

9、% 4) FFmul(0*0d, t(r + 2) % 4) FFmul(0*09, t(r + 3) % 4);unsignedcharplainte*t:FFmul(unsignedchara, unsignedcharb)unsignedchar bw4;unsignedchar res = 0;int i;bw0 = b;for (i = 1; i4; i+)bwi = bwi - 1 1;if (bwi - 1 & 0*80)bwi = 0*1b;for (i = 0; i i) & 0*01)res = bwi;return res;voidplainte*t:KeyAdding(

10、unsignedcharstate16, unsignedchark4)/轮密钥加int r, c;for (c = 0; c4; c+)for (r = 0; r4; r+)stater + c * 4 = krc;voidplainte*t:KeyE*pansion(unsignedchar* key, unsignedcharw44)/密钥扩展int i, j, r, c;unsignedchar rc = 0*01, 0*02, 0*04, 0*08, 0*10, 0*20, 0*40, 0*80, 0*1b, 0*36 ;for (r = 0; r4; r+)for (c = 0;

11、c4; c+)w0rc = keyr + c * 4;for (i = 1; i = 10; i+)for (j = 0; j4; j+)unsignedchar t4;for (r = 0; r4; r+)tr = j wirj - 1 : wi - 1r3;if (j = 0)unsignedchar temp = t0;for (r = 0; r3; r+)tr = sBo*t(r + 1) % 4;t3 = sBo*temp;t0 = rci - 1;for (r = 0; r4; r+)wirj = wi - 1rj tr;plainte*t:plainte*t()main.cpp*

12、include*include*include/使用文件选取功能*includecryptography.husingnamespace std;unsignedchar w1144 = 0 ;int len = 0;/图片每行需要加密的长度void Cipher();/加密图片void inCipher();/解密图片void Cipher(unsignedchara)unsignedchar b16;for (int i = 0; i (len / 16); i+)for (int j = 0; j16; j+)bj = aj + i * 16;plainte*t:KeyAdding(b,

13、 w0);for (int n = 1; n = 10; n+)plainte*t:SubBytes(b);plainte*t:ShiftRows(b);if (n != 10)plainte*t:Matri*ToByte(b);plainte*t:KeyAdding(b, wn);for (int m = 0; m16; m+)am + i * 16 = bm;void inCipher(unsignedchara)unsignedchar b16;for (int i = 0; i (len / 16) ; i+)for (int j = 0; j= 0; n-)plainte*t:inS

14、hiftRows(b);plainte*t:inSubBytes(b);plainte*t:KeyAdding(b, wn);if (n)plainte*t:inMatri*ToByte(b);for (int m = 0; m16; m+)am + i * 16 = bm;bool ImageCopy(constCImage &srcImage, CImage &destImage)int i, j;/循环变量if (srcImage.IsNull()returnFALSE;/源图像参数BYTE* srcPtr = (BYTE*)srcImage.GetBits();int srcBitsC

15、ount = srcImage.GetBPP();int srcWidth = srcImage.GetWidth();int srcHeight = srcImage.GetHeight();int srcPitch = srcImage.GetPitch();/销毁原有图像if (!destImage.IsNull()destImage.Destroy();/创立新图像if (srcBitsCount = 32) /支持alpha通道destImage.Create(srcWidth, srcHeight, srcBitsCount, 1);elsedestImage.Create(src

16、Width, srcHeight, srcBitsCount, 0);BYTE *destPtr = (BYTE*)destImage.GetBits();int destPitch = destImage.GetPitch();len=abs(srcPitch);for (int i = 0; isrcHeight; i+)Cipher(srcPtr + i*srcPitch);/复制图像数据for (i = 0; isrcHeight; i+)memcpy(destPtr + i*destPitch, srcPtr + i*srcPitch, abs(srcPitch);returnTRU

17、E;bool inImageCopy(constCImage &srcImage, CImage &destImage)int i, j;/循环变量if (srcImage.IsNull()returnFALSE;/源图像参数BYTE* srcPtr = (BYTE*)srcImage.GetBits();int srcBitsCount = srcImage.GetBPP();int srcWidth = srcImage.GetWidth();int srcHeight = srcImage.GetHeight();int srcPitch = srcImage.GetPitch();/销

18、毁原有图像if (!destImage.IsNull()destImage.Destroy();/创立新图像if (srcBitsCount = 32) /支持alpha通道destImage.Create(srcWidth, srcHeight, srcBitsCount, 1);elsedestImage.Create(srcWidth, srcHeight, srcBitsCount, 0);BYTE *destPtr = (BYTE*)destImage.GetBits();int destPitch = destImage.GetPitch();len = abs(srcPitch)

19、;for (int i = 0; isrcHeight; i+)inCipher(srcPtr + i*srcPitch);/复制图像数据for (i = 0; isrcHeight; i+)memcpy(destPtr + i*destPitch, srcPtr + i*srcPitch, abs(srcPitch);returnTRUE;int main()unsignedchar key16 = /固定密钥0*77, 0*59, 0*c5, 0*a4,0*55, 0*90, 0*a4, 0*a3,0*b2, 0*cc, 0*01, 0*a9,0*cb, 0*ac, 0*77, 0*23

20、;plainte*t:KeyE*pansion(key, w);TCHAR szBufferMA*_PATH = 0 ;/使用文件选取功能OPENFILENAME ofn = 0 ;ofn.lStructSize = sizeof(ofn);/ofn.hwndOwner = m_hWnd;ofn.lpstrFilter = _T();/要选择的文件后缀ofn.lpstrInitialDir = _T(D:);/默认的文件路径ofn.lpstrFile = szBuffer;/存放文件的缓冲区ofn.nMa*File = sizeof(szBuffer) / sizeof(*szBuffer);

21、ofn.nFilterInde* = 0;ofn.Flags = OFN_PATHMUSTE*IST | OFN_FILEMUSTE*IST | OFN_E*PLORER;/标志如果是多项选择要加上OFN_ALLOWMULTISELECT BOOL bSel = GetOpenFileName(&ofn);CImage image, image2, image3;/读取图片image.Load(szBuffer);ImageCopy(image, image2);image2.Save(e:/encryption.jpg);inImageCopy(image2, image3);image3.Save(e:/reencryption.jpg);system(pause);本数据记录和计算运行程序,出现选择图片界面加密完毕原图片加密图片解密图片结论结果根据结果显示,程序成功的实现了对图片的加密,得到的加密后的结果仍然为图片,并成功地的解密得到了正确的解密后的图片。小结通过本次试验,成功的完成了对AES密码算法的初步编写,了解了AES算法的工作原理。对于图片加密方面,掌握了新的调用图片的方法,使用头文件atlimage.h大大简化了c+中对图片调用的难度。同时在文件读取方面使用了头文件mdlg.h,使得程序在使用上更加方便。指导教师评议成绩评定:指导教师签名:. z.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号