资源预览内容
第1页 / 共38页
第2页 / 共38页
第3页 / 共38页
第4页 / 共38页
第5页 / 共38页
第6页 / 共38页
第7页 / 共38页
第8页 / 共38页
第9页 / 共38页
第10页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验报告一、实验目的学习 AES 加密的方法。二、实验内容与设计思想编写 AES 加密解密算法,并测试。三、实验使用环境操作系统: Microsoft Windows 7编程环境:Visual C+ 6.0四、实验步骤与调试过程/ AES.h#ifndef AES_H_#define AES_H_#include #include using namespace std;class AESpublic:typedef unsigned char byte;static const int KEY_SIZE = 16; / 密钥长度为位static const int N_ROUND = 11;byte plainText16; / 明文byte state16; / 当前分组。byte cipherKey16; / 密钥byte roundKeyN_ROUND16; /轮密钥byte cipherText16; /密文byte SBox1616; / S盒byte InvSBox1616; / 逆S盒 void EncryptionProcess(); void DecryptionProcess();void Round(const intvoid InvRound(const intvoid FinalRound();void InvFinalRound();void KeyExpansion();void AddRoundKey(const int void SubBytes(); void InvSubBytes(); void ShiftRows(); void InvShiftRows();void MixColumns(); void InvMixColumns();void BuildSBox();void BuildInvSBox();void InitialState(const byte* text);void InitialCipherText(); void InitialplainText(); byte GFMultplyByte(const byteconst byte* GFMultplyBytesMatrix(const byte* left, const byte* right);public: AES(); const byte* Cipher(const byte* text, const byte* key, const int const byte* InvCipher(const byte* text, const byte* key, const int;void AES:EncryptionProcess() / 加密过程InitialState(plainText);KeyExpansion(); / 密钥扩展AddRoundKey(0); / 轮密钥加for(int i = 1; i 0 ; -i) InvRound(i);AddRoundKey(0);InitialplainText();void AES:Round(const int& round) / 正常轮SubBytes();ShiftRows();MixColumns();AddRoundKey(round); void AES:InvRound(const int& round) / 正常轮的逆AddRoundKey(round); InvMixColumns();InvShiftRows();InvSubBytes(); void AES:FinalRound() / 最后轮SubBytes();ShiftRows();AddRoundKey(N_ROUND - 1);void AES:InvFinalRound() / 最后轮的逆AddRoundKey(N_ROUND - 1); InvShiftRows();InvSubBytes(); void AES:KeyExpansion() / 密钥扩展const byte rconN_ROUND4 = 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,0x02, 0x00, 0x00, 0x00,0x04, 0x00, 0x00, 0x00,0x08, 0x00, 0x00, 0x00,0x10, 0x00, 0x00, 0x00,0x20, 0x00, 0x00, 0x00,0x40, 0x00, 0x00, 0x00,0x80, 0x00, 0x00, 0x00,0x1b, 0x00, 0x00, 0x00,0x36, 0x00, 0x00, 0x00 ; for(int i = 0; i (rotWord0, rotWord1);std:swap(rotWord1, rotWord2);std:swap(rotWord2, rotWord3); for(int i = 0; i 4 rotWordi roundKeyroundIndex4*i = roundKeyroundIndex - 14*i rotWordi rconroundIndexi;for(int j = 1; j 4 statei void AES:InvSubBytes() / 逆字节代换for(int i = 0; i 4 statei void AES:ShiftRows() / 行变换/state第一行保持不变/ Do nothing./state第二行循环左移一个字节std:swap(state4, state5);std:swap(state5, state6);std:swap(state6, state7); /state第三行循环左移两个字节std:swap(state8, state10);std:swap(state9, state11);/state第三行循环左移三个字节std:swap(state14, state15);std:swap(state13, state14);std:swap(state12, state13); void AES:InvShiftRows() / 行变换反演/state第一行保持不变/ Do nothing./state第二行循环右移一个字节std:swap(state6, state7);std:swap(state5, state6);std:swap(state4, state5); /state第三行循环右移两个字节std:swap(state9, state11);std:swap(state8, state10);/state第三行循环右移三个字节std:swap(state12, state13); std:swap(state13, state14); std:swap(state14, state15); void AES:MixColumns() / 列混淆byte matrix44 = 0x02, 0x03, 0x01, 0x01,0x01, 0x02, 0x03, 0x01,0x01, 0x01, 0x02, 0x03,0x03, 0x01, 0x01, 0x02;const byte* temp = GFMultplyBytesMatrix(byte*)matrix, state);for(int i = 0; i bits(unsigned long)right); /把right化为个二进制位存放在bits中temp0 = left;for(int i = 1; i = 0x80) /若(tempi-1 首位为1tempi = tempi-1 #include #include #include AES.husing namespace std;int main(int argc, char* argv)const string USAGE = Usage: AES -E | -D destinationfile sourcefile keyfile ;if(argc != 5) cout #include using namespace std;class AESpublic:typedef unsigned char byte;static const int KEY_SIZE = 16; / 密钥长度为位static const int N_ROUND = 11;byte plainText16; / 明文byte state16; / 当前分组。byte cipherKey16; / 密钥byte roundKeyN_ROUND16; /轮密钥byte cipherText16; /密文byte SBox1616; / S盒byte InvSBox1616; / 逆 S盒 void EncryptionProcess(); void DecryptionProcess();void Round(const intvoid InvRound(const intvoid FinalRound();void InvFinalRound();void KeyExpansion();void AddRoundKey(const int void SubBytes(); void InvSubBytes();void ShiftRows(); void InvShiftRows();void MixColumns(); void InvMixColumns();void BuildSBox();void BuildInvSBox();void InitialState(const byte* text);void InitialCipherText(); void InitialplainText(); byte GFMultplyByte(const byteconst byte* GFMultplyBytesMatrix(const byte* left, const byte* right);public: AES(); const byte* Cipher(const byte* text, const byte* key, const int const byte* InvCiphe
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号