四种加密算法之AES的源码-C++

AES.h文件:

[cpp]  view plain copy
  1. // Aes1.h: interface for the Aes class.  
  2. //! AES 动态链接库实现   H文件  
  3. /*! 
  4.  @author 朱孟斌 
  5.  @e-mail  zmb.tsubasa@gmail.com 
  6.  @version 1.0 
  7.  @date 2011-03 
  8.  @{ 
  9. */  
  10. //  
  11. #include <string.h>  
  12. #include <stdio.h>  
  13. #include <Windows.h>  
  14. //!   #能够进行动态链接库编译的AES类  
  15.  /*!   
  16.        @see class _declspec(dllexport) AES 
  17.        将AES算法写成动态链接库的形式方便调用,生成消息使用 
  18.  */  
  19. class _declspec(dllexport) AES  
  20. {  
  21. public:  
  22.     //! #定义ENUM保存AES密钥的长度  
  23.     /*! 
  24.         以下这种ENUM保存三个密钥长度的选择 
  25.         @see enum ENUM_KeySize_ 
  26.         @attention 不同长度的密钥对应的加密的字符串长度不同 
  27.     */  
  28.     typedef enum ENUM_KeySize_  
  29.     {  
  30.         BIT128 = 0,  
  31.         BIT192,  
  32.         BIT256  
  33.     }ENUM_KEYSIZE;  
  34. public:  
  35.     //! #AES的构造函数  
  36.     /*! 
  37.       @see AES( ENUM_KEYSIZE keysize, BYTE *key ) 
  38.       @param[ENUM_KEYSIZE] 对密钥长度进行初始化设置 
  39.       @param[BYTE] 输入相应的密钥 
  40.       其中应该对AES类中的一些变量进行相应的初始化 
  41.     */  
  42.     AES( ENUM_KEYSIZE keysize, BYTE *key );  
  43.     //! #AES的析构函数  
  44.     /*! 
  45.      @see ~AES(void) 
  46.       释放内存 
  47.     */  
  48.     ~AES(void);  
  49.     //! #AES算法中的加密函数模块  
  50.     /*! 
  51.       @see void Cipher( BYTE *input, BYTE *output ); 
  52.       @param[in]  input为输入的明文 
  53.       @param[out] output为加密之后的密文 
  54.       @return[void] 不返回任何值 
  55.     */  
  56.     void Cipher( BYTE *input, BYTE *output );  
  57.     //! #AES算法中的解密函数模块  
  58.     /*! 
  59.       @see void InvCipher( BYTE *input, BYTE *output ); 
  60.       @param[in]  input为输入的密文 
  61.       @param[in] output为解密之后的明文 
  62.       @return[void] 不返回任何值 
  63.     */  
  64.     void InvCipher( BYTE *input, BYTE *output );  
  65. protected:  
  66.     BYTE *RotWord( BYTE *word );  
  67.     BYTE *SubWord( BYTE *word );  
  68.     void AddRoundKey(int round);  
  69.     void SubBytes();  
  70.     void InvSubBytes();  
  71.     void ShiftRows();  
  72.     void InvShiftRows();  
  73.     void MixColumns();  
  74.     void InvMixColumns();  
  75.     static BYTE gfmultby01(BYTE b)  //乘1  
  76.     {  
  77.         return b;  
  78.     }  
  79.   
  80.     static BYTE gfmultby02(BYTE b) //乘2  
  81.     {  
  82.         if (b < 0x80)  
  83.             return (BYTE)(int)(b <<1);  
  84.         else  
  85.             return (BYTE)( (int)(b << 1) ^ (int)(0x1b) );  
  86.     }  
  87.   
  88.     static BYTE gfmultby03(BYTE b)  
  89.     {  
  90.         return (BYTE) ( (int)gfmultby02(b) ^ (int)b );//GF域的加法运算就是异或  
  91.     }  
  92.   
  93.     static BYTE gfmultby09(BYTE b)  
  94.     {  
  95.         return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^  
  96.             (int)b );  
  97.     }  
  98.   
  99.     static BYTE gfmultby0b(BYTE b)  
  100.     {  
  101.         return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^  
  102.             (int)gfmultby02(b) ^  
  103.             (int)b );  
  104.     }  
  105.   
  106.     static BYTE gfmultby0d(BYTE b)  
  107.     {  
  108.         return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^  
  109.             (int)gfmultby02(gfmultby02(b)) ^  
  110.             (int)(b) );  
  111.     }  
  112.   
  113.     static BYTE gfmultby0e(BYTE b)  
  114.     {  
  115.         return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^  
  116.             (int)gfmultby02(gfmultby02(b)) ^  
  117.             (int)gfmultby02(b) );  
  118.     }  
  119.     //! #代表以字为单位的块长  
  120.     /*! 
  121.     @Brief 代表以字为单位的块长 
  122.     */  
  123.     int Nb; //  
  124.     //! #代表以字为单位的密钥长度  
  125.     /*! 
  126.     @Brief 代表以字为单位的密钥长度 
  127.     */  
  128.     int Nk;//  
  129.     //! #轮数 ,轮数是10、12或14中的任意一个并且是基于密码分析学理论的  
  130.     /*! 
  131.     @Brief 轮数 ,轮数是10、12或14中的任意一个并且是基于密码分析学理论的 
  132.     @attention 它直接取决于密钥长度 
  133.     */  
  134.     int Nr;//轮数 ,轮数是10、12或14中的任意一个并且是基于密码分析学理论的。  
  135.     //  
  136.     //! #the seed key  
  137.     /*! 
  138.     @Brief   size will be 4 * keySize from ctor 
  139.     */  
  140.     BYTE *key;//  
  141.     typedef struct BYTE4_  
  142.     {  
  143.         BYTE w[4];  
  144.     }BYTE4;  
  145.     BYTE4 *w;  
  146.     LPBYTE State[4];  
  147.     /* 
  148.     private byte[,] iSbox;  // inverse Substitution box  
  149.     private byte[,] w;      // key schedule array.  
  150.     private byte[,] Rcon;   // Round constants. 
  151.     private byte[,] State;  // State matrix*/  
  152.   
  153. };  

AES.cpp文件内容:

[cpp]  view plain copy
  1. // Aes1.cpp: implementation of the Aes class.  
  2. //  
  3. //  
  4.   
  5. //#include "stdafx.h"  
  6. #include "AES.h"  
  7. #include <stdio.h>  
  8. #include <Windows.h>  
  9. #include <string.h>  
  10.   
  11. const BYTE Sbox[16][16] = {  // populate the Sbox matrix  
  12.     /* 0     1     2     3     4     5     6     7     8     9     a     b     c     d     e     f */  
  13.     /*0*/  {0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},  
  14.     /*1*/  {0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},  
  15.     /*2*/  {0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},  
  16.     /*3*/  {0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},  
  17.     /*4*/  {0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},  
  18.     /*5*/  {0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},  
  19.     /*6*/  {0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},  
  20.     /*7*/  {0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},  
  21.     /*8*/  {0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},  
  22.     /*9*/  {0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},  
  23.     /*a*/  {0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79},  
  24.     /*b*/  {0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08},  
  25.     /*c*/  {0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},  
  26.     /*d*/  {0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e},  
  27.     /*e*/  {0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},  
  28.     /*f*/  {0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}   
  29. };  
  30.   
  31. const BYTE iSbox[16][16] = {  // populate the iSbox matrix  
  32.     /* 0     1     2     3     4     5     6     7     8     9     a     b     c     d     e     f */  
  33.     /*0*/  {0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},  
  34.     /*1*/  {0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},  
  35.     /*2*/  {0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},  
  36.     /*3*/  {0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25},  
  37.     /*4*/  {0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},  
  38.     /*5*/  {0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},  
  39.     /*6*/  {0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},  
  40.     /*7*/  {0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},  
  41.     /*8*/  {0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},  
  42.     /*9*/  {0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e},  
  43.     /*a*/  {0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},  
  44.     /*b*/  {0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},  
  45.     /*c*/  {0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},  
  46.     /*d*/  {0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},  
  47.     /*e*/  {0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61},  
  48.     /*f*/  {0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d} };  
  49.     const BYTE Rcon[11][4] = { {0x00, 0x00, 0x00, 0x00},  //轮常数表  
  50.     {0x01, 0x00, 0x00, 0x00},  
  51.     {0x02, 0x00, 0x00, 0x00},  
  52.     {0x04, 0x00, 0x00, 0x00},  
  53.     {0x08, 0x00, 0x00, 0x00},  
  54.     {0x10, 0x00, 0x00, 0x00},  
  55.     {0x20, 0x00, 0x00, 0x00},  
  56.     {0x40, 0x00, 0x00, 0x00},  
  57.     {0x80, 0x00, 0x00, 0x00},  
  58.     {0x1b, 0x00, 0x00, 0x00},  
  59.     {0x36, 0x00, 0x00, 0x00} };  
  60.     BYTE * AES::RotWord( BYTE *word )//左旋一位  
  61.     {  
  62.         BYTE *result = new BYTE [4];  
  63.         result[0] = word[1];  
  64.         result[1] = word[2];  
  65.         result[2] = word[3];  
  66.         result[3] = word[0];  
  67.         delete[] word;  
  68.         return result;  
  69.     };  
  70.     BYTE *AES::SubWord( BYTE *word )  //用替换表 Sbox 对一给定的一行密钥调度表 w[] 进行逐字节替换。  
  71.     {  
  72.         BYTE *result = new BYTE[4];  
  73.         result[0] = Sbox[ word[0] >> 4][ word[0] & 0x0f ];  
  74.         result[1] = Sbox[ word[1] >> 4][ word[1] & 0x0f ];  
  75.         result[2] = Sbox[ word[2] >> 4][ word[2] & 0x0f ];  
  76.         result[3] = Sbox[ word[3] >> 4][ word[3] & 0x0f ];  
  77.         delete[] word;  
  78.         return result;  
  79.     }  
  80.     AES::AES( ENUM_KEYSIZE keysize, BYTE *key )  
  81.     {  
  82.   
  83.         this->Nb = 4;  
  84.         switch( keysize )  
  85.         {  
  86.         case AES::BIT128:  
  87.             this->Nk = 4;  
  88.             this->Nr = 10;  
  89.             break;  
  90.         case AES::BIT192:  
  91.             this->Nk = 6;  
  92.             this->Nr = 12;  
  93.             break;  
  94.         case AES::BIT256:  
  95.         default:  
  96.             this->Nk = 8;  
  97.             this->Nr = 14;  
  98.             break;  
  99.         }  
  100.         this->key = new BYTE[this->Nk * 4];  
  101.         memcpy( this->key, key, this->Nk * 4 );  
  102.         this->w = new BYTE4[Nb * (Nr+1)];   //w[]为密钥调度表  
  103.         //w[] 最初的 Nk (6) 行被作为种子,用原始密钥值   
  104.         int row;  
  105.         for(row = 0; row < Nk; ++row )  
  106.         {  
  107.             w[row].w[0] = this->key[4*row];  
  108.             w[row].w[1] = this->key[4*row+1];  
  109.             w[row].w[2] = this->key[4*row+2];  
  110.             w[row].w[3] = this->key[4*row+3];  
  111.         }  
  112.         BYTE *temp = new BYTE[4];  
  113.         for(  row = Nk; row < Nb *(Nr + 1); ++ row )  
  114.         {  
  115.             temp[0] = this->w[row-1].w[0];  
  116.             temp[1] = this->w[row-1].w[1];  
  117.             temp[2] = this->w[row-1].w[2];  
  118.             temp[3] = this->w[row-1].w[3];  
  119.             if (row % Nk == 0)  
  120.             {  
  121.                 temp = SubWord(RotWord(temp));//this change two size  
  122.                 temp[0] = (BYTE)( (int)temp[0] ^ (int)Rcon[row/Nk][0] );  
  123.                 temp[1] = (BYTE)( (int)temp[1] ^ (int)Rcon[row/Nk][1] );  
  124.                 temp[2] = (BYTE)( (int)temp[2] ^ (int)Rcon[row/Nk][2] );  
  125.                 temp[3] = (BYTE)( (int)temp[3] ^ (int)Rcon[row/Nk][3] );  
  126.             }  
  127.             else if ( Nk > 6 && (row % Nk == 4) )    
  128.             {  
  129.                 temp = SubWord(temp);  
  130.             }  
  131.   
  132.             // w[row] = w[row-Nk] xor temp  
  133.             this->w[row].w[0] = (BYTE) ( (int)this->w[row-Nk].w[0] ^ (int)temp[0] );  
  134.             this->w[row].w[1] = (BYTE) ( (int)this->w[row-Nk].w[1] ^ (int)temp[1] );  
  135.             this->w[row].w[2] = (BYTE) ( (int)this->w[row-Nk].w[2] ^ (int)temp[2] );  
  136.             this->w[row].w[3] = (BYTE) ( (int)this->w[row-Nk].w[3] ^ (int)temp[3] );  
  137.         }//loop  
  138.         delete[] temp;  
  139.         for(int i=0;i<4;i++)  
  140.         {  
  141.             this->State[i] = NULL;  
  142.         }  
  143.     }  
  144.   
  145.     AES::~AES(void)  
  146.     {  
  147.         for(int i=0;i<4;i++)  
  148.         {  
  149.             if (this->State[i] != NULL)  
  150.             {  
  151.                 delete []this->State[i];  
  152.             }  
  153.         }  
  154.     }  
  155.     void AES::Cipher( BYTE *input, BYTE *output )  
  156.     {  
  157.         if (this->State[0] == NULL)  
  158.         {  
  159.             for(int i=0;i<4;i++)  
  160.             {  
  161.                 this->State[i] = new BYTE[this->Nb];  
  162.             }  
  163.         }  
  164.         int i;  
  165.         for (i = 0; i < (4 * Nb); ++i)  
  166.         {  
  167.             this->State[i%4][i/4] = input[i];  
  168.         }  
  169.         AddRoundKey(0);  
  170.   
  171.         for (int round = 1; round <= (Nr - 1); ++round)  // main round loop  
  172.         {  
  173.             SubBytes();   
  174.             ShiftRows();    
  175.             MixColumns();   
  176.             AddRoundKey(round);  
  177.         }  // main round loop  
  178.   
  179.         SubBytes();  
  180.         ShiftRows();  
  181.         AddRoundKey(Nr);  
  182.   
  183.         // output = state  
  184.         for ( i = 0; i < (4 * Nb); ++i)  
  185.         {  
  186.             output[i] = this->State[i % 4][ i / 4];  
  187.         }  
  188.     }  
  189.   
  190.     void AES::InvCipher( BYTE *input, BYTE *output )  
  191.     {  
  192.         int i;  
  193.         if (this->State[0] == NULL)  
  194.         {  
  195.             for(i=0;i<4;i++)  
  196.             {  
  197.                 this->State[i] = new BYTE[this->Nb];  
  198.             }  
  199.         }  
  200.   
  201.         for (i = 0; i < (4 * Nb); ++i)  
  202.         {  
  203.             this->State[i % 4][ i / 4] = input[i];  
  204.         }  
  205.   
  206.         AddRoundKey(Nr);  
  207.   
  208.         for (int round = Nr-1; round >= 1; --round)  // main round loop  
  209.         {  
  210.             InvShiftRows();  
  211.             InvSubBytes();  
  212.             AddRoundKey(round);  
  213.             InvMixColumns();  
  214.         }  // end main round loop for InvCipher  
  215.   
  216.         InvShiftRows();  
  217.         InvSubBytes();  
  218.         AddRoundKey(0);  
  219.   
  220.         // output = state  
  221.         for ( i = 0; i < (4 * Nb); ++i)  
  222.         {  
  223.             output[i] = this->State[i % 4][ i / 4];  
  224.         }  
  225.     }  
  226.     void AES::AddRoundKey( int round )  
  227.     {  
  228.         for (int r = 0; r < 4; ++r)  
  229.         {  
  230.             for (int c = 0; c < 4; ++c)  
  231.             {  
  232.                 this->State[r][c] = (BYTE) ( (int)this->State[r][c] ^ (int)w[(round*4)+c].w[r] );  
  233.             }  
  234.         }  
  235.     }  
  236.   
  237.     void AES::SubBytes()  
  238.     {  
  239.         for (int r = 0; r < 4; ++r)  
  240.         {  
  241.             for (int c = 0; c < 4; ++c)  
  242.             {  
  243.                 this->State[r][c] = Sbox[ (this->State[r][c] >> 4)][ (this->State[r][c] & 0x0f) ];  
  244.             }  
  245.         }  
  246.     }  
  247.     void AES::InvSubBytes()  
  248.     {  
  249.         for (int r = 0; r < 4; ++r)  
  250.         {  
  251.             for (int c = 0; c < 4; ++c)  
  252.             {  
  253.                 this->State[r][c] = iSbox[ (this->State[r][c] >> 4)][ (this->State[r][c] & 0x0f) ];  
  254.             }  
  255.         }  
  256.     }  
  257.     void AES::ShiftRows()  
  258.     {  
  259.         BYTE4 temp[4];  
  260.         //  byte[,] temp = new byte[4,4];  
  261.         int r;  
  262.         for (r = 0; r < 4; ++r)  // copy State into temp[]  
  263.         {  
  264.             for (int c = 0; c < 4; ++c)  
  265.             {  
  266.                 temp[r].w[c] = this->State[r][c];  
  267.                 // temp[r,c] = this.State[r,c];  
  268.             }  
  269.         }  
  270.   
  271.         for ( r = 1; r < 4; ++r)  // shift temp into State  
  272.         {  
  273.             for (int c = 0; c < 4; ++c)  
  274.             {  
  275.                 this->State[r][c] = temp[ r].w[ (c + r) % Nb ];  
  276.             }  
  277.         }  
  278.     }  // ShiftRows()  
  279.     void AES::InvShiftRows()  
  280.     {  
  281.         int r;  
  282.         BYTE4 temp[4];  
  283.         for (r = 0; r < 4; ++r)  // copy State into temp[]  
  284.         {  
  285.             for (int c = 0; c < 4; ++c)  
  286.             {  
  287.                 temp[r].w[c] = this->State[r][c];  
  288.             }  
  289.         }  
  290.         for ( r = 1; r < 4; ++r)  // shift temp into State  
  291.         {  
  292.             for (int c = 0; c < 4; ++c)  
  293.             {  
  294.                 this->State[r][ (c + r) % Nb ] = temp[r].w[c];  
  295.             }  
  296.         }  
  297.     }  // InvShiftRows()  
  298.     void AES::MixColumns()  
  299.     {  
  300.         BYTE4 temp[4];  
  301.         for (int r = 0; r < 4; ++r)  // copy State into temp[]  
  302.         {  
  303.             for (int c = 0; c < 4; ++c)  
  304.             {  
  305.                 temp[r].w[c] = this->State[r][c];  
  306.             }  
  307.         }  
  308.   
  309.         for (int c = 0; c < 4; ++c)  
  310.         {  
  311.             this->State[0][c] = (BYTE) ( (int)gfmultby02(temp[0].w[c]) ^ (int)gfmultby03(temp[1].w[c]) ^  
  312.                 (int)gfmultby01(temp[2].w[c]) ^ (int)gfmultby01(temp[3].w[c]) );  
  313.             this->State[1][c] = (BYTE) ( (int)gfmultby01(temp[0].w[c]) ^ (int)gfmultby02(temp[1].w[c]) ^  
  314.                 (int)gfmultby03(temp[2].w[c]) ^ (int)gfmultby01(temp[3].w[c]) );  
  315.             this->State[2][c] = (BYTE) ( (int)gfmultby01(temp[0].w[c]) ^ (int)gfmultby01(temp[1].w[c]) ^  
  316.                 (int)gfmultby02(temp[2].w[c]) ^ (int)gfmultby03(temp[3].w[c]) );  
  317.             this->State[3][c] = (BYTE) ( (int)gfmultby03(temp[0].w[c]) ^ (int)gfmultby01(temp[1].w[c]) ^  
  318.                 (int)gfmultby01(temp[2].w[c]) ^ (int)gfmultby02(temp[3].w[c]) );  
  319.         }  
  320.     }  // MixColumns  
  321.     void AES::InvMixColumns()  
  322.     {  
  323.         BYTE4 temp[4];  
  324.         for (int r = 0; r < 4; ++r)  // copy State into temp[]  
  325.         {  
  326.             for (int c = 0; c < 4; ++c)  
  327.             {  
  328.                 temp[r].w[c] = this->State[r][c];  
  329.             }  
  330.         }  
  331.   
  332.         for (int c = 0; c < 4; ++c)  
  333.         {  
  334.             this->State[0][c] = (BYTE) ( (int)gfmultby0e(temp[0].w[c]) ^ (int)gfmultby0b(temp[1].w[c]) ^  
  335.                 (int)gfmultby0d(temp[2].w[c]) ^ (int)gfmultby09(temp[3].w[c]) );  
  336.             this->State[1][c] = (BYTE) ( (int)gfmultby09(temp[0].w[c]) ^ (int)gfmultby0e(temp[1].w[c]) ^  
  337.                 (int)gfmultby0b(temp[2].w[c]) ^ (int)gfmultby0d(temp[3].w[c]) );  
  338.             this->State[2][c] = (BYTE) ( (int)gfmultby0d(temp[0].w[c]) ^ (int)gfmultby09(temp[1].w[c]) ^  
  339.                 (int)gfmultby0e(temp[2].w[c]) ^ (int)gfmultby0b(temp[3].w[c]) );  
  340.             this->State[3][c] = (BYTE) ( (int)gfmultby0b(temp[0].w[c]) ^ (int)gfmultby0d(temp[1].w[c]) ^  
  341.                 (int)gfmultby09(temp[2].w[c]) ^ (int)gfmultby0e(temp[3].w[c]) );  
  342.         }  
  343.     }  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SummerMVP的gravatar头像 SummerMVP2020-04-16 17:32:30原c++语言实现AES加密算法代码分享 项目描述 AES的实现 1. 只要求实现块长为 128 位、密钥长为 128 位的 AES,分别实现 ECB、CBC、CFB、OFB 这四种操作 模式。每种操作模式都有一组对应的测试数据,以便检查程序的正确性。其中,CFB 操作模式为 8 位 CFB 操作模式,OFB 操作模式为 8 位 OFB 操作模式。 2. 要求以命令行的形式,指定明文文件、密钥文件、初始化向量文件的位置和名称、加密的操作模式以 及加密完成后密文文件的位置和名称。加密时先分别从指定的明文文件、密钥文件和初始化向量文件中 读取有关信息,然后按指定的操作模式进行加密,最后将密文(用 16 进制表示)写入指定的密文文件。 命令行的具体格式如下: e2aes -p plainfile -k keyfile [-v vifile] -m mode -c cipherfile 参数: -p plainfile 指定明文文件的位置和名称 -k keyfile 指定密钥文件的位置和名称 -v vifile 指定初始化向量文件的位置和名称 -m mode 指定加密的操作模式 -c cipherfile 指定密文文件的位置和名称。 3. 分别实现对每种操作模式下加密及解密速度的测试,要求在程序中生成 5MB 的随机测试数据(不要 求使用随机数发生器),连续加密、解密 20 次,记录并报告每种模式的加密和解密的总时间(毫秒)和 速度(MByte/秒)。 4. 用 C 和/或 C++语言完成程序。 运行环境 VS2012 项目技术(必填) C++AES算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值