完整AES加密算法实验报告.docx
- 文档编号:4383426
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:18
- 大小:895.97KB
完整AES加密算法实验报告.docx
《完整AES加密算法实验报告.docx》由会员分享,可在线阅读,更多相关《完整AES加密算法实验报告.docx(18页珍藏版)》请在冰点文库上搜索。
完整AES加密算法实验报告
四川大学计算机学院、软件学院
实验报告
学号:
姓名:
专业:
班级:
第10周
课程名称
密码学与网络安全
实验课时
2
实验项目
AES加密算法
实验时间
2015。
5.7
实验目的
完成AES加密算法,实现图片加密与解密,并将加密后的结果以图片格式保存。
实验环境
PC机,Windows7操作系统,VisualC++6。
0
实验内容(算法、程序、步骤和方法)
一、简介
美国国家标准技术研究所在2001年发布了高级加密标准(AES)。
AES是一个对称加密算法,旨在取代DES成为广泛使用的标准。
AES中的所有运算都是在8为的字节上运行的.特别饿,加减乘除算术都是在有限域GF(28)上运行的.
二、程序特点
本次试验中要求对图片进行加密与解密,并将加密结果以图片格式进行保存。
因此为了实现对图片的调度及保存,使用头文件atlimage.h进行对图片的操作,实现对图片的像素读取,图片的保存。
在程序运行读取需要加密的图片时,需要进行图片的选取,本次实验中使用在弹窗中选取文件的方式,使用头文件commdlg.h来实现在文件夹中选择需要的文件的选取。
三、加密算法流程
AES加密算法流程如下
字节代替:
用一个S盒完成分组的字节到字节的代替;
行移位:
进行一次行上的置换;
列混合:
利用有限域GF(28)上的运算特性的一个代替;
轮密钥加:
当前分组和扩展密钥的一部分进行按位异或.
四、代码实现
cryptograph。
h
#include #include〈iostream〉 classplaintext { public: plaintext(); staticvoidcreateplaintext(unsignedchara[]); staticvoidSubBytes(unsignedcharp[16]); staticvoidinSubBytes(unsignedcharp[16]); staticvoidShiftRows(unsignedchare[]); staticvoidinShiftRows(unsignedchare[]); staticvoidMatrixToByte(unsignedchare[]); staticvoidinMatrixToByte(unsignedchare[]); staticunsignedcharFFmul(unsignedchara,unsignedcharb); staticvoidKeyAdding(unsignedcharstate[16],unsignedchark[][4]); staticvoidKeyExpansion(unsignedchar*key,unsignedcharw[][4][4]); ~plaintext(); private: }; cryptograph.cpp #include”cryptography。 h" usingnamespacestd; staticunsignedcharsBox[]={};/定义加密S盒/ unsignedcharinsBox[256]={};//定义解密S盒 plaintext: : plaintext() { } voidplaintext: : createplaintext(unsignedchara[])//创建明文 { inti=0; unsignedintp[16]; for(intj=0;j〈200;j++) { if(a[j]==0) { break; } } for(;i<16;i++) { p[i]=a[i]; a[i]=a[i+16]; } } voidplaintext: : SubBytes(unsignedcharp[16])//字节变换函数 { unsignedcharb[16]; for(inti=0;i<16;i++) { b[i]=sBox[(int)p[i]]; } } voidplaintext: : inSubBytes(unsignedcharp[16])//逆字节变换函数 { unsignedcharb[16]; for(inti=0;i〈16;i++) { b[i]=insBox[(int)p[i]]; } } voidplaintext: : ShiftRows(unsignedchare[])//行移位变换函数 { unsignedchart[4]; for(inti=1;i<4;i++) { for(intx=0;x〈4;x++) t[x]=e[x+i*4]; for(inty=0;y〈4;y++) e[(y+4—i)%4+i*4]=t[y]; } } voidplaintext: : inShiftRows(unsignedchare[])//逆行移位变换函数 { unsignedchart[4]; for(inti=1;i〈4;i++) { for(intx=0;x〈4;x++) t[x]=e[x+i*4]; for(inty=0;y<4;y++) e[(y+i)%4+i*4]=t[y]; } } voidplaintext: : MatrixToByte(unsignedchare[])//列混合变换函数 { unsignedchart[4]; intr,c; for(c=0;c<4;c++) { for(r=0;r〈4;r++) { t[r]=e[r*4+c]; } for(r=0;r〈4;r++) { e[r*4+c]=FFmul(0x02,t[r]) ^FFmul(0x03,t[(r+1)%4]) ^FFmul(0x01,t[(r+2)%4]) ^FFmul(0x01,t[(r+3)%4]); } } } voidplaintext: : inMatrixToByte(unsignedchare[])//逆列混合变换函数 { unsignedchart[4]; intr,c; for(c=0;c<4;c++) { for(r=0;r<4;r++) { t[r]=e[r*4+c]; } for(r=0;r<4;r++) { e[r*4+c]=FFmul(0x0e,t[r]) ^FFmul(0x0b,t[(r+1)%4]) ^FFmul(0x0d,t[(r+2)%4]) ^FFmul(0x09,t[(r+3)%4]); } } } unsignedcharplaintext: : FFmul(unsignedchara,unsignedcharb) { unsignedcharbw[4]; unsignedcharres=0; inti; bw[0]=b; for(i=1;i〈4;i++) { bw[i]=bw[i-1]<<1; if(bw[i—1]&0x80) { bw[i]^=0x1b; } } for(i=0;i〈4;i++) { if((a〉>i)&0x01) { res^=bw[i]; } } returnres; } voidplaintext: : KeyAdding(unsignedcharstate[16],unsignedchark[][4])//轮密钥加 { intr,c; for(c=0;c〈4;c++) { for(r=0;r〈4;r++) { state[r+c*4]^=k[r][c]; } } } voidplaintext: : KeyExpansion(unsignedchar*key,unsignedcharw[][4][4])//密钥扩展 { inti,j,r,c; unsignedcharrc[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36}; for(r=0;r<4;r++) { for(c=0;c<4;c++) { w[0][r][c]=key[r+c*4]; } } for(i=1;i<=10;i++) { for(j=0;j〈4;j++) { unsignedchart[4]; for(r=0;r<4;r++) { t[r]=j? w[i][r][j-1]: w[i—1][r][3]; } if(j==0) { unsignedchartemp=t[0]; for(r=0;r<3;r++) { t[r]=sBox[t[(r+1)%4]]; } t[3]=sBox[temp]; t[0]^=rc[i-1]; } for(r=0;r<4;r++) { w[i][r][j]=w[i—1][r][j]^t[r]; } } } } plaintext: : ~plaintext() { } main。 cpp #include〈iostream〉 #include h〉 #include〈commdlg.h>//使用文件选取功能 #include”cryptography。 h" usingnamespacestd; unsignedcharw[11][4][4]={0}; intlen=0;//图片每行需要加密的长度 voidCipher();//加密图片 voidinCipher();//解密图片 voidCipher(unsignedchara[]) { unsignedcharb[16]; for(inti=0;i〈(len/16);i++) { for(intj=0;j〈16;j++) b[j]=a[j+i*16]; plaintext: : KeyAdding(b,w[0]); for(intn=1;n〈=10;n++) { plaintext: : SubBytes(b); plaintext: : ShiftRows(b); if(n! =10)plaintext: : MatrixToByte(b); plaintext: : KeyAdding(b,w[n]); } for(intm=0;m〈16;m++) a[m+i*16]=b[m]; } } voidinCipher(unsignedchara[]){ unsignedcharb[16]; for(inti=0;i〈(len/16);i++) { for(intj=0;j〈16;j++) { b[j]=a[j+i*16]; } plaintext: : KeyAdding(b,w[10]); for(intn=9;n〉=0;n——) { plaintext: : inShiftRows(b); plaintext: : inSubBytes(b); plaintext: : KeyAdding(b,w[n]); if(n)plaintext: : inMatrixToByte(b); } for(intm=0;m〈16;m++) a[m+i*16]=b[m]; } } boolImageCopy(constCImage&srcImage,CImage&destImage) { inti,j;//循环变量 if(srcImage.IsNull()) returnFALSE; //源图像参数 BYTE*srcPtr=(BYTE*)srcImage。 GetBits(); intsrcBitsCount=srcImage.GetBPP(); intsrcWidth=srcImage.GetWidth(); intsrcHeight=srcImage。 GetHeight(); intsrcPitch=srcImage。 GetPitch(); //销毁原有图像 if(! destImage.IsNull()) { destImage。 Destroy(); } //创建新图像 if(srcBitsCount==32)//支持alpha通道 { destImage。 Create(srcWidth,srcHeight,srcBitsCount,1); } else { destImage.Create(srcWidth,srcHeight,srcBitsCount,0); } BYTE*destPtr=(BYTE*)destImage.GetBits(); intdestPitch=destImage.GetPitch(); len=abs(srcPitch); for(inti=0;i〈srcHeight;i++) Cipher(srcPtr+i*srcPitch); //复制图像数据 for(i=0;i〈srcHeight;i++) { memcpy(destPtr+i*destPitch,srcPtr+i*srcPitch,abs(srcPitch)); } returnTRUE; } boolinImageCopy(constCImage&srcImage,CImage&destImage) { inti,j;//循环变量 if(srcImage.IsNull()) returnFALSE; //源图像参数 BYTE*srcPtr=(BYTE*)srcImage.GetBits(); intsrcBitsCount=srcImage。 GetBPP(); intsrcWidth=srcImage.GetWidth(); intsrcHeight=srcImage.GetHeight(); intsrcPitch=srcImage。 GetPitch(); //销毁原有图像 if(! destImage.IsNull()) { destImage。 Destroy(); } //创建新图像 if(srcBitsCount==32)//支持alpha通道 { destImage.Create(srcWidth,srcHeight,srcBitsCount,1); } else { destImage。 Create(srcWidth,srcHeight,srcBitsCount,0); } BYTE*destPtr=(BYTE*)destImage.GetBits(); intdestPitch=destImage。 GetPitch(); len=abs(srcPitch); for(inti=0;i inCipher(srcPtr+i*srcPitch); //复制图像数据 for(i=0;i { memcpy(destPtr+i*destPitch,srcPtr+i*srcPitch,abs(srcPitch)); } returnTRUE; } intmain() { unsignedcharkey[16]={//固定密钥 0x77,0x59,0xc5,0xa4, 0x55,0x90,0xa4,0xa3, 0xb2,0xcc,0x01,0xa9, 0xcb,0xac,0x77,0x23}; plaintext: : KeyExpansion(key,w); TCHARszBuffer[MAX_PATH]={0};//使用文件选取功能 OPENFILENAMEofn={0}; ofn。 lStructSize=sizeof(ofn); //ofn.hwndOwner=m_hWnd; ofn。 lpstrFilter=_T(”");//要选择的文件后缀 ofn。 lpstrInitialDir=_T("D: \\");//默认的文件路径 ofn.lpstrFile=szBuffer;//存放文件的缓冲区 ofn。 nMaxFile=sizeof(szBuffer)/sizeof(*szBuffer); ofn.nFilterIndex=0; ofn。 Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;//标志如果是多选要加上OFN_ALLOWMULTISELECT BOOLbSel=GetOpenFileName(&ofn); CImageimage,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++中对图片调用的难度。 同时在文件读取方面使用了头文件commdlg。 h,使得程序在使用上更加方便. 指导老师评议 成绩评定: 指导教师签名:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 完整 AES 加密算法 实验 报告