今天得去上自习,这些日子不上自习的多。主要是因为做那个AES的设计了,不过终于是出来了,现提供代码等如下,以供大家参考:

1 /******************************************************

2 *

3 * 名称:AES加密

4 *

5 * 姓名:杜耀斐

6 * 学号:20045903031

7 * 专业:商用密码

8 *

9 *

10 * 说明:

11 * 因为本程序为演示程序,时间关系,没有做过多处理

12 * 请输入长度都为16的加密数据和密钥,THANKS~~

13 * 本程序所用编译器为GNU G++,如VC无法编译,请自行

14 * 更换为G++,或修改头文件处iostream为iostream.h,

15 * cstring为string.h,cstdio为stdio.h,并去掉文件头部名

16 * 称空间声明即可,特此说明

17 * 于南楼416

18 *

19 *

20 *

21 ******************************************************/

22

23 #include “iostream”

24 #include “cstring”

25 #include “cstdio”

26

27 using namespace std;

28

29 //定义自定义类型AESWord

30 typedef unsigned char AESWord;

31

32 //定义s盒数组

33 AESWord SBox[256]={

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

49 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16

50 };

51

52 //定义逆s盒数组

53 AESWord InvSBox[256]={

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

55 0x7c, 0xe3, 0x39, 0x82, 0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1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,

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

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

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

70 };

71

72 //类定义

73 class AES

74 {

75 public:

76 //变量声明

77 AESWord State[16];

78 AESWord Key[16];

79 AESWord RoundKey[11][16];

80 //加密部分函数声明

81 void Cipher();

82 void SubBytes();

83 void MixColumns();

84 void ShiftRows();

85 void AddRoundKey(AESWord AddState[16], AESWord Key[16]);

86 //密钥产生函数声明

87 void KeyExpansion();

88 //解密部分函数声明

89 void InvCipher();

90 void InvSubBytes();

91 void InvMixColumns();

92 void InvShiftRows();

93 //接口输入部分

94 void EnCrypt(AESWord inState[16], AESWord inKey[16]);

95 void DeCrypt(AESWord outState[16], AESWord outKey[16]);

96 };

97

98 //类实现部分

99

100 void AES::EnCrypt(AESWord inState[16], AESWord inKey[16]){

101 int i;

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

103 State[i]=inState[i];

104 Key[i]=inKey[i];

105 }

106 Cipher();

107 }

108

109 void AES::DeCrypt(AESWord outState[16], AESWord outKey[16]){

110 int i;

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

112 State[i]=outState[i];

113 Key[i]=outKey[i];

114 }

115 InvCipher();

116 }

117

118 void AES::Cipher(){

119 int i;

120 KeyExpansion();

121 AddRoundKey(State, RoundKey[0]);

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

123 SubBytes();

124 ShiftRows();

125 MixColumns();

126 AddRoundKey(State, RoundKey[i]);

127 }

128 SubBytes();

129 ShiftRows();

130 AddRoundKey(State, RoundKey[i]);

131 }

132

133 void AES::InvCipher(){

134 int i;

135 AddRoundKey(State, RoundKey[10]);

136 InvShiftRows();

137 InvSubBytes();

138 for(i=9; i>0; i–){

139 AddRoundKey(State, RoundKey[i]);

140 InvMixColumns();

141 InvShiftRows();

142 InvSubBytes();

143 }

144 AddRoundKey(State, RoundKey[0]);

145 }

146

147 void AES::SubBytes(){

148 int i;

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

150 State[i]=SBox[State[i]];

151 }

152 }

153

154 void AES::MixColumns(){

155 int i,j,k;

156 //定义列混合变换的c(x)数组

157 AESWord cx[4][4]={

158 {2, 3, 1, 1},

159 {1, 2, 3, 1},

160 {1, 1, 2, 3},

161 {3, 1, 1, 2}

162 };

163 //定义每列的临时数组LineTemp

164 AESWord LineTemp[4], Temp[4];

165 //定义临时数组AESTemp

166 AESWord AESTemp[16];

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

168 AESTemp[i]=State[i];

169 }

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

171 for(k=0; k<4; k++){

172 Temp[k]=AESTemp[i+4*k];

173 }

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

175 LineTemp[j]=Temp[j]*cx[i][j];

176 }

177 State[4*j+i]=((AESWord)LineTemp[0])^((AESWord)LineTemp[1])^((AESWord)LineTemp[2])^((AESWord)LineTemp[3]);

178 }

179 }

180

181 void AES::ShiftRows(){

182 int i,t;

183

184 //第一行保持不变

185 //第二行正序依次向前移

186 t=State[4];

187 for(i=4;i<7;i++){

188 State[i]=State[i+1];

189 }

190 State[i++]=t;

191

192 //第三行直接变换

193 t=State[i]; State[i]=State[i+2]; State[(i++)+2]=t;

194 t=State[i]; State[i]=State[i+2]; State[i+2]=t;

195

196 //第四行逆序依次向后移

197 t=State[15];

198 for(i=15;i>12;i–){

199 State[i]=State[i-1];

200 }

201 State[i]=t;

202 }

203

204 //密钥产生器

205 void AES::KeyExpansion(){

206 AESWord RC[10]={0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};

207

208 int i,j,l;

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

210 RoundKey[0][i]=Key[i];

211 }

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

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

214 AESWord f[4];

215 if(j==0){

216 for(l=0;l<4;l++)

217 f[l]=RoundKey[i-1][3+l*4];

218 AESWord t=f[0];

219 for(l=0;l<3;l++){

220 f[l]=f[l+1];

221 }

222 f[l]=t;

223 for(l=0;l<4;l++){

224 f[l]=SBox[f[l]];

225 }

226 RoundKey[i][0]=RoundKey[i-1][0]^f[0]^RC[i-1];

227 for(l=1;l<4;l++){

228 RoundKey[i][l*4]=RoundKey[i-1][l*4]^f[l];

229 }

230 }

231 else

232 for(int l=0;l<4;l++)

233 RoundKey[i][j+l*4]=RoundKey[i][j+l*41]^RoundKey[i-1][j+l*4];

234 }

235 }

236 }

237

238 void AES::AddRoundKey(AESWord AddState[16], AESWord AddKey[16]){

239 int i;

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

241 AddState[i]^=AddKey[i];

242 }

243 }

244

245 void AES::InvSubBytes(){

246 int i;

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

248 State[i]=InvSBox[State[i]];

249 }

250 }

251

252 void AES::InvMixColumns(){

253 int i,j,k;

254 //定义逆列混合变换的c(x)数组

255 AESWord cx[4][4]={

256 {0x0e, 0x0B, 0x0D, 0x09},

257 {0x09, 0x0E, 0x0B, 0x0D},

258 {0x0D, 0x09, 0x0E, 0x0B},

259 {0x0B, 0x0D, 0x09, 0x0E}

260 };

261 //定义每列的临时数组LineTemp

262 AESWord LineTemp[4], Temp[4];

263 //定义临时数组AESTemp

264 AESWord AESTemp[16];

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

266 AESTemp[i]=State[i];

267 }

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

269 for(k=0; k<4; k++){

270 Temp[k]=AESTemp[i+4*k];

271 }

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

273 LineTemp[j]=Temp[j]*cx[i][j];

274 }

275 State[4*j+i]=((AESWord)LineTemp[0])^((AESWord)LineTemp[1])^((AESWord)LineTemp[2])^((AESWord)LineTemp[3]);

276 }

277 }

278

279 void AES::InvShiftRows(){

280 int i,t;

281 //注释见ShiftRows()

282 t=State[7];

283 for(i=7; i>4; i–){

284 State[i]=State[i-1];

285 }

286

287 State[4]=t;

288 t=State[8]; State[8]=State[10]; State[10]=t;

289 t=State[9]; State[9]=State[11]; State[11]=t;

290 t=State[12];

291

292 for(i=12; i<15; i++){

293 State[i]=State[i+1];

294 }

295 State[15]=t;

296 }

297

298 //主函数

299 int main(){

300 int i;

301 AESWord inText[1000], outText[1000], inKey[16];

302 AESWord TempText[1000];

303 AES perAES;

304

305 cout << “姓名:杜耀斐” <<endl;

306 cout << “学号:20045903031” <<endl;

307 cout << “专业:商用密码” <<endl<<endl;

308

309 cout << ” 因为本程序为演示程序,时间关系,没有做过多处理 “ <<endl

310 << “请输入长度都为16的加密数据和密钥,THANKS~~” <<endl

311 << ” 本程序所用编译器为GNU G++,如VC无法编译,请自行” <<endl

312 << “更换为G++,或修改头文件处iostream为iostream.h, “ <<endl

313 << “cstring为string.h,并去掉文件头部名称空间声明即可,” <<endl

314 << “特此说明” <<endl

315 << ttt于南楼416″<<endl;

316

317 cout << “———————-AES演示———————-“ << endl;

318 cout << “请输入您要加密的数据:”;

319 cin >> inText;

320 cout << endl;

321 cout << “请输入您要加密的密钥:”;

322 cin >> inKey;

323 cout << endl;

324

325 perAES.EnCrypt(inText, inKey);

326 cout << “加密结果:”;

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

328 TempText[i]=perAES.State[i];

329 cout << perAES.State[i];

330 }

331 cout << endl;

332 cout << “十六进制加密结果:”;

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

334 printf(%x,perAES.State[i]);

335 }

336 cout << endl;

337

338 perAES.DeCrypt(TempText, inKey);

339 cout << “解密结果:”;

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

341 cout << perAES.State[i];

342 }

343 cout << endl;

344

345 cout << “———————-演示结束———————-“ << endl;

346 }

发表评论

电子邮件地址不会被公开。 必填项已用*标注