aes-128
Posted zhu-xuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了aes-128相关的知识,希望对你有一定的参考价值。
1 #include<stdio.h> 2 void Sub_Bytes(); 3 void Shift_Rows(); 4 void Mix_Columns(); 5 unsigned char Gf_256(unsigned char a,unsigned char b); 6 void Add_Round_Key(); 7 void Key_Expansion(int time); 8 9 10 unsigned char sBox[256] = //S-box 11 { /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 12 /*0*/ 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, 13 /*1*/ 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, 14 /*2*/ 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, 15 /*3*/ 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, 16 /*4*/ 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, 17 /*5*/ 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, 18 /*6*/ 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, 19 /*7*/ 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, 20 /*8*/ 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, 21 /*9*/ 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, 22 /*a*/ 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, 23 /*b*/ 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, 24 /*c*/ 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, 25 /*d*/ 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, 26 /*e*/ 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, 27 /*f*/ 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 28 }; 29 30 31 unsigned char state[4][4] = //明文 32 { 33 0x32,0x88,0x31,0xe0, 34 0x43,0x5a,0x31,0x37, 35 0xf6,0x30,0x98,0x07, 36 0xa8,0x8d,0xa2,0x34 37 }; 38 39 40 unsigned char cipher_key[4][4] = //密钥 41 { 42 0x2b,0x28,0xab,0x09, 43 0x7e,0xae,0xf7,0xcf, 44 0x15,0xd2,0x15,0x4f, 45 0x16,0xa6,0x88,0x3c 46 }; 47 48 49 unsigned char Rcon[4][10] = //轮常量 50 { 51 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36, 52 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 53 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 54 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 55 }; 56 57 58 int main() 59 { int i,j,t; 60 for(i=0;i<4;i++) //打印明文 61 { 62 for(j=0;j<4;j++) 63 { 64 printf("%x ",state[i][j]); 65 } 66 printf(" "); 67 } 68 printf(" "); 69 70 Add_Round_Key(); //Round 0 71 72 for(t=0;t<9;t++) //Round 1-9 73 { 74 Sub_Bytes(); 75 Shift_Rows(); 76 Mix_Columns(); 77 Key_Expansion(t); 78 Add_Round_Key(); 79 } 80 81 Sub_Bytes(); //Round 10 82 Shift_Rows(); 83 Key_Expansion(t); 84 Add_Round_Key(); 85 86 for(i=0;i<4;i++) 87 { 88 for(j=0;j<4;j++) 89 { 90 printf("%x ",state[i][j]); 91 } 92 printf(" "); 93 } 94 return 0; 95 } 96 97 98 void Sub_Bytes() //字节替换 99 { 100 int i,j; 101 for(i=0;i<4;i++) 102 { 103 for(j=0;j<4;j++) 104 { 105 state[i][j]=sBox[state[i][j]]; 106 } 107 } 108 } 109 110 111 void Shift_Rows() //行移位变换 112 { 113 int i,j; 114 unsigned char temp[4]; 115 for(i=1;i<4;i++) 116 { 117 for(j=0;j<4;j++) 118 { 119 temp[j]=state[i][j]; 120 } 121 for(j=0;j<4;j++) 122 { 123 state[i][j]=temp[(j+i)%4]; 124 } 125 } 126 } 127 128 129 void Mix_Columns() //列混淆变换 130 { 131 int i,j; 132 unsigned char temp[4]; 133 for(j=0;j<4;j++) 134 { 135 for(i=0;i<4;i++) 136 { 137 temp[i]=state[i][j]; 138 } 139 for(i=0;i<4;i++) 140 { 141 state[i][j]=Gf_256(0x02,temp[i])^Gf_256(0x03,temp[(1+i)%4])^Gf_256(0x01,temp[(2+i)%4])^Gf_256(0x01,temp[(3+i)%4]); 142 } 143 } 144 } 145 146 147 unsigned char Gf_256(unsigned char a,unsigned char b) //有限域GF(28)上的乘法 148 { 149 unsigned char temp[3]; 150 unsigned char res; 151 temp[0]=b; //temp[0]=0x01*b 152 temp[1]=b<<1; //temp[1]=0x02*b 153 if(b&0x80) //0x80=1000 0000 154 { 155 temp[1]=temp[1]^0x1b; 156 } 157 temp[2]=temp[0]^temp[1]; //temp[2]=0x03*b 158 if(a==0x01) {res=temp[0];} 159 if(a==0x02) {res=temp[1];} 160 if(a==0x03) {res=temp[2];} 161 return res; 162 } 163 164 165 void Add_Round_Key() //轮密钥加变换 166 { 167 int i,j; 168 for(j=0;j<4;j++) 169 { 170 for(i=0;i<4;i++) 171 { 172 state[i][j]=state[i][j]^cipher_key[i][j]; 173 } 174 } 175 } 176 177 178 void Key_Expansion(int time) //aes-128为11组纶密钥,第0组为输入密钥本身 179 { 180 int i,j; 181 unsigned char temp[4]; 182 for(i=0;i<4;i++) //取出最后一列,同时循环左移一个字节 183 { 184 temp[i]=cipher_key[(i+1)%4][3]; 185 } 186 for(i=0;i<4;i++) //取出的一列进行subbytes 187 { 188 temp[i]=sBox[temp[i]]; 189 } 190 for(i=0;i<4;i++) //生成下一组密钥第一列 191 { 192 cipher_key[i][0]=cipher_key[i][0]^temp[i]^Rcon[i][time]; 193 } 194 for(j=1;j<4;j++) //生成下一组密钥的234列 195 { 196 for(i=0;i<4;i++) 197 { 198 cipher_key[i][j]=cipher_key[i][j]^cipher_key[i][j-1]; 199 } 200 } 201 }
以上是关于aes-128的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp Objective-C代码,用于AES-128加密的加密和解密。