整理几种aes加解密算法c++源程序

上传人:第*** 文档编号:32641580 上传时间:2018-02-12 格式:DOC 页数:38 大小:314.50KB
返回 下载 相关 举报
整理几种aes加解密算法c++源程序_第1页
第1页 / 共38页
整理几种aes加解密算法c++源程序_第2页
第2页 / 共38页
整理几种aes加解密算法c++源程序_第3页
第3页 / 共38页
整理几种aes加解密算法c++源程序_第4页
第4页 / 共38页
整理几种aes加解密算法c++源程序_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《整理几种aes加解密算法c++源程序》由会员分享,可在线阅读,更多相关《整理几种aes加解密算法c++源程序(38页珍藏版)》请在金锄头文库上搜索。

1、实验报告一、实验目的学习 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;

2、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 KeyE

3、xpansion();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(cons

4、t 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(); / 密

5、钥扩展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(); v

6、oid 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,

7、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 r

8、oundKeyroundIndex4*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);

9、 /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:swa

10、p(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

11、,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* arg

12、v)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;

13、/ 密钥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

14、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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号