2020-10-16

AES加密解密

  1. ASE基本结构

AES为分组密码,分组密码也就是把明文分成一组一组的,每组长度相等,每次加密一组数据,直到加密完整个明文。在AES标准规范中,分组长度只能是128位,也就是说,每个分组为16个字节(每个字节8位)。**的长度可以使用128位、192位或256位。**的长度不同,推荐加密轮数也不同。AES的加密公式为C = E(K,P),在加密函数E中,会执行一个轮函数,并且执行10次这个轮函数,这个轮函数的前9次执行的操作是一样的,只有第10次有所不同。也就是说,一个明文分组会被加密10轮。AES的核心就是实现一轮中的所有操作。2020-10-16

AES的处理单位是字节,128位的输入明文分组P和输入**K都被分成16个字节,分别记为P = P0 P1 … P15 和 K = K0 K1 … K15。如,明文分组为P = abcdefghijklmnop,其中的字符a对应P0,p对应P15。一般地,明文分组用字节为单位的正方形矩阵描述,称为状态矩阵。在算法的每一轮中,状态矩阵的内容不断发生变化,最后的结果作为密文输出。该矩阵中字节的排列顺序为从上到下、从左至右依次排列。2020-10-16

 

  1. ASE加密

AES的整体结构如下图所示,其中的W[0,3]是指W[0]、W[1]、W[2]和W[3]串联组成的128位**。加密的第1轮到第9轮的轮函数一样,包括4个操作:字节代换、行位移、列混合和轮**加。最后一轮迭代不执行列混合。另外,在第一轮迭代之前,先将明文和原始**进行一次异或加密操作。

   2020-10-16

2020-10-16

其中字节代换就是将明文字节通过S盒查表将其一一代换,是将状态矩阵中的元素按照下面的方式映射为一个新的字节:把该字节的高4位作为行值,低4位作为列值,取出S盒中对应的行的元素作为输出。S盒为:

2020-10-16

行移位是一个简单的左循环移位操作。当**长度为128比特时,状态矩阵的第0行左移0字节,第1行左移1字节,第2行左移2字节,第3行左移3字节。

2020-10-16

列混合变换是通过矩阵相乘来实现的,经行移位后的状态矩阵与固定的矩阵相乘,得到混淆后的状态矩阵,如下图的公式所示:其中,矩阵元素的乘法和加法都是定义在基于GF(2^8)上的二元运算。 

2020-10-16

接下来进行轮**加运算:轮**加是将128位轮**Ki同状态矩阵中的数据进行逐位异或操作,轮**加过程可以看成是字逐位异或的结果,也可以看成字节级别或者位级别的操作。

2020-10-16

**扩展:4*4矩阵的每一列的4个字节组成一个字,矩阵4列的4个字依次命名为W[0]、W[1]、W[2]和W[3],它们构成一个以字为单位的数组W。例如,设**K为”abcdefghijklmnop”,则K0 = ‘a’,K1 = ‘b’, K2 = ‘c’,K3 = ‘d’,W[0] = “abcd”。接着,对W数组扩充40个新列,构成总共44列的扩展**数组。新列以如下的递归方式产生:1.如果i不是4的倍数,那么第i列由如下等式确定:W[i]=W[i-4]⨁W[i-1] 2.如果i是4的倍数,那么第i列由如下等式确定:W[i]=W[i-4]⨁T(W[i-1]) 其中,T是一个有点复杂的函数。 函数T由3部分组成:字循环、字节代换和轮常量异或,这3部分的作用分别如下。a.字循环:将1个字中的4个字节循环左移1个字节。即将输入字[b0, b1, b2, b3]变换成[b1,b2,b3,b0]。b.字节代换:对字循环的结果使用S盒进行字节代换。c.轮常量异或:将前两步的结果同轮常量Rcon[j]进行异或,其中j表示轮数。

 

  1. ASE解密

解密过程为加密的逆,流程如下:

2020-10-16

2020-10-16

 

  1. ASE算法实现

#include<stdio.h>

#include<string.h>

int  RoundKey[10][16];                      //!>  轮** ;

int  CipherKey[16]={0x2b,0x28,0xab,0x09,   // !>加***;

     0x7e,0xae,0xf7,0xcf,

     0x15,0xd2,0x15,0x4f,

     0x16,0xa6,0x88,0x3c};

int  State[16]={0x32,0x88,0x31,0xe0,     // !>明文;

    0x43,0x5a,0x31,0x37,

    0xf6,0x30,0x98,0x07,

    0xa8,0x8d,0xa2,0x34};

int sbox[256] = {

    //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F

    0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0

    0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1

    0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2

    0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3

    0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4

    0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5

    0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6

    0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7

    0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8

    0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9

    0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A

    0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B

    0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C

    0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D

    0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E

    0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F

 

int rsbox[256] = {

    //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F

    0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,

    0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,

    0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,

    0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,

    0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,

    0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,

    0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,

    0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,

    0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,

    0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,

    0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,

    0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,

    0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,

    0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,

    0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,

    0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };

 

int Rcon[40] = {

     0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

 };

 

/*------------------------------

          函数声明部分;

--------------------------------*/

void AddRoundKey(int * ex_state, int * RoundKey);    //**加函数;

void SubBytes(int * ex_state, int * S_box);      //字节代换函数;

void ShiftRows(int * ex_state);         //行移位函数;

void MixColumns(int * ex_state);        //列混合函数;

void Round(int * ex_state, int * S_box,int * RoundKey);   //轮函数;

void Final_Round(int * ex_state, int * S_box,int * RoundKey); //最后轮函数;

void Aes_Encrypt(int * ex_state, int * S_box,int *cphkey,int RoundKey[10][16]); //加密函数;

void Aes_Decrypt(int * ex_state, int * S_box,int *cphkey,int RoundKey[10][16]); //解密函数;

void RotByte(int * temp);              

void Key_Schedule(int * cphkey,int RoundKey[10][16]);   //生成轮**的函数;

void InvSubBytes(int * ex_state, int * RS_box);     //逆字节代换函数;

void InvShiftRows(int * ex_state);        //逆行移位函数;

void InvMixColumns(int * ex_state);        //逆列混合函数;

void InvRound(int * ex_state, int * RS_box,int * RoundKey);  //逆轮函数;

void InvFinal_Round(int * ex_state, int * RS_box,int * RoundKey);//逆最后轮函数;

/*---------------------------------*/

 

/*-----------------------------------

   函数功能具体实现

------------------------------------*/

void SubBytes(int * ex_state, int * S_box)                //字节代换函数;

{

 int i,k;

 for(i=0;i<16;i++)

 {

  k=ex_state[i];

  ex_state[i]=S_box[k];

 }

}

void ShiftRows(int * ex_state)     //行移位函数;

{

 int k,temp;

temp=ex_state[4];

 ex_state[4]=ex_state[5];

 ex_state[5]=ex_state[6];

 ex_state[6]=ex_state[7];

 ex_state[7]=temp;

 /*-------------------*第三行;

 temp=ex_state[8];

 ex_state[8]=ex_state[10];

 ex_state[10]=temp;

 temp=ex_state[9];

 ex_state[9]=ex_state[11];

 ex_state[11]=temp;

    temp=ex_state[13];

 ex_state[13]=ex_state[12];

 k=ex_state[14];

 ex_state[14]=temp;

 temp=ex_state[15];

 ex_state[15]=k;

 ex_state[12]=temp;

 /*--------------------*/

}

void AddRoundKey(int * ex_state, int * RoundKey)        //**加函数;

{

 int i;

 for(i=0;i<4;i++)

 {

  ex_state[i]^=RoundKey[i];

  ex_state[i+4]^=RoundKey[i+4];

  ex_state[i+8]^=RoundKey[i+8];

  ex_state[i+12]^=RoundKey[i+12];

 }

}

int xtime(int n)   //!> 用来把这个数*0x02;

{

 int temp;

 temp=n<<1;

 if(n&0x80)

 {

  temp=temp^0x1b;

 }

 return temp;

}

 

int mixcolumn(int m,int n)

{

 int temp;

 for(temp=m,m=0;n;n=n>>1)

 {

  if(n&1)

  {

   m=m^temp;

  }

  temp=xtime(temp);

 }

 return m&0xff;

}

void MixColumns(int * ex_state)     //列混合函数;还需要一个mix()函数来帮助这个函数;

{

 int i;

 int p_state[16]={0};             //定义一个新的p-state来记录输入的ex_state,以免ex_state发生变化时,p-state还能使用原先的那个ex_state;

 for(i=0;i<16;i++)

  p_state[i]=ex_state[i];

 for(i=0;i<16;i++)

 {

  if(i>=0&&i<4)

  {

   ex_state[i]=mixcolumn(ex_state[i],0x02)^mixcolumn(ex_state[i+1*4],0x03)^p_state[i+2*4]^p_state[i+3*4];

  }

  else if(i>=4&&i<8)

  {

   ex_state[i]=p_state[i-4]^mixcolumn(ex_state[i],0x02)^mixcolumn(ex_state[i+1*4],0x03)^p_state[i+2*4];

  }

  else if(i>=8&&i<12)

  {

   ex_state[i]=p_state[i-2*4]^p_state[i-1*4]^mixcolumn(ex_state[i],0x02)^mixcolumn(ex_state[i+1*4],0x03);

  }

  else if(i>=12&&i<16)

  {

   ex_state[i]=mixcolumn(p_state[i-3*4],0x03)^p_state[i-2*4]^p_state[i-1*4]^mixcolumn(ex_state[i],0x02);

  }

 }

}

void Round(int * ex_state, int * S_box,int * RoundKey)     //轮函数;

{

 SubBytes(ex_state,S_box);

 ShiftRows(ex_state);

 MixColumns(ex_state);

 AddRoundKey(ex_state,RoundKey);

}

void Final_Round(int * ex_state, int * S_box,int * RoundKey)     //最后轮函数;

{

 SubBytes(ex_state,S_box);

 ShiftRows(ex_state);

 AddRoundKey(ex_state,RoundKey);

}

void RotByte(int * temp)  

{

 int j=0;

 j=temp[0];         //!>对它进行移位;

 temp[0]=temp[1];

 temp[1]=temp[2];

 temp[2]=temp[3];

 temp[3]=j;

}

void Key_Schedule(int * cphkey,int RoundKey[10][16],int * S_box,int * Rcon) //生成轮**的函数;

{

 int i,j,m,n;

 int temp_first[4]={0};   

 int temp_last[4]={0};   

 for(i=0;i<4;i++)       

 {

  temp_last[i]=cphkey[i*4+3];

 }

 RotByte(temp_last); //!>移位

 for(i=0;i<4;i++)   //!>进行S_box转换;

 {

  temp_last[i]=S_box[temp_last[i]];

 }

 for(i=0;i<4;i++)    //!>把第一列的数据赋给temp_first数组;

 {

  temp_first[i]=cphkey[i*4];

 }

 for(i=0;i<4;i++)    //!>生成第一轮的RoundKey;

 {

  RoundKey[0][i*4]=temp_first[i]^temp_last[i]^Rcon[i*10];

 }

 for(i=1;i<4;i++)

 {

  for(j=0;j<4;j++)

  {

   RoundKey[0][j*4+i]=RoundKey[0][j*4+i-1]^cphkey[j*4+i];

  }

 }

 for(i=1;i<10;i++)

 {

  for(m=0;m<4;m++)          //!>把最后一列的数据赋给temp_last数组;

  {

   temp_last[m]=RoundKey[i-1][m*4+3];

  }

  RotByte(temp_last); //!>移位;

  for(m=0;m<4;m++)   //!>进行S_box转换;

  {

   temp_last[m]=S_box[temp_last[m]];

  }

  for(m=0;m<4;m++) 

  {

   RoundKey[i][m*4]=RoundKey[i-1][m*4]^temp_last[m]^Rcon[m*10+i];

  }

  for(j=1;j<4;j++)

  {

   for(n=0;n<4;n++)

   {

    RoundKey[i][n*4+j]=RoundKey[i][n*4+j-1]^RoundKey[i-1][n*4+j];

   }

  }

 }

}

void Aes_Encrypt(int * ex_state, int * S_box,int *cphkey,int RoundKey[10][16]) //加密函数;

{

 int i,j;

 Key_Schedule(cphkey,RoundKey,S_box,Rcon);

 AddRoundKey(ex_state, cphkey);

 for(i=0;i<9;i++)

 {

  Round(ex_state,S_box,RoundKey[i]);

  /*printf("**的中间状态为:\n");

  for(j=0;j<16;j++) 

  {

   printf("%x ",ex_state[j]);

  }

  printf("\n");*/

 }

 Final_Round(ex_state, S_box,RoundKey[9]);

}

void InvSubBytes(int * ex_state, int * RS_box)    //逆字节代换函数;

{

 int i,k;

 for(i=0;i<16;i++)

 {

  k=ex_state[i];

  ex_state[i]=RS_box[k];

 }

}

void InvShiftRows(int * ex_state)   //逆行移位函数;

{

 int k,temp;

temp=ex_state[5];

 ex_state[5]=ex_state[4];

 k=ex_state[6];

 ex_state[6]=temp;

 temp=ex_state[7];

 ex_state[7]=k;

 ex_state[4]=temp;

 /*-------------------*第三行移动两位;

 temp=ex_state[8];

 ex_state[8]=ex_state[10];

 ex_state[10]=temp;

 temp=ex_state[9];

 ex_state[9]=ex_state[11];

 ex_state[11]=temp;

    temp=ex_state[12];

 ex_state[12]=ex_state[13];

 ex_state[13]=ex_state[14];

 ex_state[14]=ex_state[15];

 ex_state[15]=temp;

 /*--------------------*/

 

}

void InvMixColumns(int * ex_state)     //逆列混合函数;

{

 int i;

 int p_state[16]={0};           

 for(i=0;i<16;i++)

  p_state[i]=ex_state[i];

 for(i=0;i<16;i++)

 {

  if(i>=0&&i<4)

  {

   ex_state[i]=mixcolumn(ex_state[i],0x0e)^mixcolumn(ex_state[i+1*4],0x0b)^mixcolumn(p_state[i+2*4],0x0d)^mixcolumn(p_state[i+3*4],0x09);

  }

  else if(i>=4&&i<8)

  {

   ex_state[i]=mixcolumn(p_state[i-4],0x09)^mixcolumn(ex_state[i],0x0e)^mixcolumn(ex_state[i+1*4],0x0b)^mixcolumn(p_state[i+2*4],0x0d);

  }

  else if(i>=8&&i<12)

  {

   ex_state[i]=mixcolumn(p_state[i-2*4],0x0d)^mixcolumn(p_state[i-1*4],0x09)^mixcolumn(ex_state[i],0x0e)^mixcolumn(ex_state[i+1*4],0x0b);

  }

  else if(i>=12&&i<16)

  {

   ex_state[i]=mixcolumn(p_state[i-3*4],0x0b)^mixcolumn(p_state[i-2*4],0x0d)^mixcolumn(p_state[i-1*4],0x09)^mixcolumn(ex_state[i],0x0e);

  }

 }

}

void InvRound(int * ex_state, int * RS_box,int * RoundKey)  //逆轮函数;

 InvSubBytes(ex_state, RS_box);

 InvShiftRows(ex_state);

 InvMixColumns(ex_state);

 AddRoundKey(ex_state,RoundKey);

}

void InvFinal_Round(int * ex_state, int * RS_box,int * RoundKey) //逆最后轮函数;

{

 InvSubBytes(ex_state, RS_box);

 InvShiftRows(ex_state);

 AddRoundKey(ex_state,RoundKey);

}

void Aes_Decrypt(int * ex_state, int * RS_box,int *cphkey,int RoundKey[10][16]) //解密函数;

{

 int i;

 AddRoundKey(ex_state,RoundKey[9]);

 for(i=8;i>=0;i--)

 {

  InvMixColumns(RoundKey[i]);

  InvRound(ex_state,RS_box,RoundKey[i]);

 }

 InvFinal_Round(ex_state,RS_box,cphkey);

}

/*----------------------------------------

    主函数部分

  ----------------------------------------*/

int main()

{

 int i,j;

 printf("AES加密解密\n");

 printf("初始的**为:\n");

 for(i=0;i<16;i++)

 {

  printf("%x ",CipherKey[i]);

 }

 printf("\n\n");

 printf("明文为:\n");  

 for(i=0;i<16;i++)

 {

  printf("%x ",State[i]);

 }

 printf("\n\n");

 Aes_Encrypt(State, sbox, CipherKey, RoundKey); //加密函数;

 printf("加密后的密文为:\n");

  for(j=0;j<16;j++)

  {

   printf("%x ",State[j]);

  }

  printf("\n\n");

 Aes_Decrypt(State, rsbox, CipherKey,RoundKey);

 printf("解密后的明文为:\n");

 for(i=0;i<16;i++)

 {

  printf("%x ",State[i]);

 }

 printf("\n\n");

 return 0;

}