标签:
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。本文将介绍AES的具体流程,以及c++实现,并且实现了CBC和CTR模式的解密函数。
1.关于AES(高级加密标准):在这里一个分组为128bit(16byte),密钥也是128bit(16byte),密钥要先通过密钥扩展,具体过程如图:
然后加轮密钥(第一轮之前),之后再经过每一轮的比特代换,行移位,列混合和加轮密钥,每个分组一共10轮,如下图所示的加密过程:
解密则是相逆的过程,如上图所示,首先是密钥扩展,先加轮密钥,然后在经过每一轮的行移位反演,位代换反演,加轮密钥和列混合反演,每个分组一共进行10轮。还要注意的一点是在每一轮里的结构是状态(state),是4*4的矩阵。
下面是c++代码:
AES.h
#ifndef __SymmetricKeyCipher__AES__ #define __SymmetricKeyCipher__AES__ #include <stdio.h> typedef unsigned char Byte; // Each word is 4 Bytes #define BYTES_IN_WORD (4) // Each round of key is 4 words #define WORD_IN_ROUND (4) // Each byte has 8 bits #define BIT_IN_BYTE (8) // Each round of key is 4 words, say 4 * 4 = 16 Bytes #define BYTES_IN_ROUND (16) // Expended key length is 11 round, say 11 * 16 = 176 Bytes #define BYTES_IN_EXPANDED_KEY (176) // In AES128 has 10 rounds except the initialization round #define NUM_OF_ROUNDS (10) static Byte sBox[16][16] = { {0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76}, {0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0}, {0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15}, {0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 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x47, 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} }; static Byte constantMatrix[4][4] = { {0x02, 0x03, 0x01, 0x01}, {0x01, 0x02, 0x03, 0x01}, {0x01, 0x01, 0x02, 0x03}, {0x03, 0x01, 0x01, 0x02} }; static Byte constantMatrixInv[4][4] = { {0x0E, 0x0B, 0x0D, 0x09}, {0x09, 0x0E, 0x0B, 0x0D}, {0x0D, 0x09, 0x0E, 0x0B}, {0x0B, 0x0D, 0x09, 0x0E} }; static Byte GF_constant[8] = {0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A, 0x2F}; static Byte roundConstant[10] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36}; Byte *keyExpansion(Byte *cipherKey); void AES_Encryption(Byte state[][BYTES_IN_WORD], Byte* key); void AES_Decryption(Byte state[][BYTES_IN_WORD], Byte* key); void rotateWord(Byte *word, int offset); void substitutionWord(Byte *word); void substitutionWord(Byte state[][BYTES_IN_WORD]); void substitutionWordInv(Byte state[][BYTES_IN_WORD]); void shiftRow(Byte state[][BYTES_IN_WORD]); void shiftRowInv(Byte state[][BYTES_IN_WORD]); void mixColumn(Byte state[][BYTES_IN_WORD]); void mixColumnInv(Byte state[][BYTES_IN_WORD]); Byte GF_Multiplication(Byte a, Byte b); void addRoundKey(Byte state[][BYTES_IN_WORD], Byte* key, int round); void printState(Byte state[][BYTES_IN_WORD]); #endif
AES.cpp
#include <stdio.h> #include <stdlib.h> #include "AES.h" Byte *keyExpansion(Byte *cipherKey) { Byte *expandedKey = (Byte *)malloc(sizeof(Byte) * BYTES_IN_EXPANDED_KEY); // get the key of the first round for(int i = 0; i < BYTES_IN_ROUND; i++) expandedKey[i] = cipherKey[i]; // get the key of other rounds Byte *temporary_word = (Byte *)malloc(sizeof(Byte) * BYTES_IN_WORD); for(int i = 1; i <= NUM_OF_ROUNDS; i++) { // calculate the temporary word for(int j = 0; j < BYTES_IN_WORD; j++) temporary_word[j] = expandedKey[i * BYTES_IN_ROUND - BYTES_IN_WORD + j]; rotateWord(temporary_word, 1); substitutionWord(temporary_word); temporary_word[0] = (temporary_word[0] ^ roundConstant[i - 1]); // get the key of this round for(int j = 0; j < BYTES_IN_WORD; j++) expandedKey[i * BYTES_IN_ROUND + j] = temporary_word[j] ^ expandedKey[(i - 1) * BYTES_IN_ROUND + j]; for(int j = 1; j < WORD_IN_ROUND; j++) { for(int k = 0; k < BYTES_IN_WORD; k++) { expandedKey[i * BYTES_IN_ROUND + j * BYTES_IN_WORD + k] = expandedKey[i * BYTES_IN_ROUND + (j - 1) * BYTES_IN_WORD + k] ^ expandedKey[(i - 1) * BYTES_IN_ROUND + j * BYTES_IN_WORD + k]; } } } free(temporary_word); return expandedKey; } void AES_Encryption(Byte state[][BYTES_IN_WORD], Byte* key) { // Round 0: addRoundKey addRoundKey(state, key, 0); // Round 1~9: substitutionWord + shiftRow + mixColumn + addRoundKey for(int i = 1; i < 10; i++) { substitutionWord(state); shiftRow(state); mixColumn(state); addRoundKey(state, key, i); } // Round 10: substitutionWord + shiftRow + addRoundKey substitutionWord(state); shiftRow(state); addRoundKey(state, key, 10); } void AES_Decryption(Byte state[][BYTES_IN_WORD], Byte* key) { // Inv round 10: addRoundKey + shiftRowInv + substitutionWordInv addRoundKey(state, key, 10); shiftRowInv(state); substitutionWordInv(state); // Inv round 9~1: addRoundKey + mixColumnInv + shiftRowInv + substitutionWordInv for(int i = 9; i > 0; i--) { addRoundKey(state, key, i); mixColumnInv(state); shiftRowInv(state); substitutionWordInv(state); } // Inv round 0: addRoundKey addRoundKey(state, key, 0); } void rotateWord(Byte *word, int offset) { Byte *temp = (Byte *)malloc(sizeof(Byte) * BYTES_IN_WORD); for(int i = 0; i < BYTES_IN_WORD; i++) temp[(i + BYTES_IN_WORD - offset) % 4] = word[i]; for(int i = 0; i < BYTES_IN_WORD; i++) word[i] = temp[i]; free(temp); } void substitutionWord(Byte *word) { for(int i = 0; i < BYTES_IN_WORD; i++) word[i] = sBox[word[i] / 16][word[i] % 16]; } void substitutionWord(Byte state[][BYTES_IN_WORD]) { for(int i = 0; i < BYTES_IN_WORD; i++) for(int j = 0; j < BYTES_IN_WORD; j++) state[i][j] = sBox[state[i][j] / 16][state[i][j] % 16]; } void substitutionWordInv(Byte state[][BYTES_IN_WORD]) { for(int i = 0; i < BYTES_IN_WORD; i++) for(int j = 0; j < BYTES_IN_WORD; j++) state[i][j] = sBoxInv[state[i][j] / 16][state[i][j] % 16]; } void shiftRow(Byte state[][BYTES_IN_WORD]) { for(int i = 0; i < BYTES_IN_WORD; i++) rotateWord(state[i], i); } void shiftRowInv(Byte state[][BYTES_IN_WORD]) { for(int i = 1; i < BYTES_IN_WORD; i++) rotateWord(state[i], 4 - i); } void mixColumn(Byte state[][BYTES_IN_WORD]) { Byte *temp = (Byte *)malloc(sizeof(Byte) * BYTES_IN_WORD); for(int i = 0; i < BYTES_IN_WORD; i++) { for(int j = 0; j < BYTES_IN_WORD; j++) temp[j] = state[j][i]; for(int j = 0; j < BYTES_IN_WORD; j++) { state[j][i] = 0; for(int k = 0; k < BYTES_IN_WORD; k++) state[j][i] = (state[j][i] ^ GF_Multiplication(constantMatrix[j][k], temp[k])); } } free(temp); } void mixColumnInv(Byte state[][BYTES_IN_WORD]) { Byte *temp = (Byte *)malloc(sizeof(Byte) * BYTES_IN_WORD); for(int i = 0; i < BYTES_IN_WORD; i++) { for(int j = 0; j < BYTES_IN_WORD; j++) temp[j] = state[j][i]; for(int j = 0; j < BYTES_IN_WORD; j++) { state[j][i] = 0; for(int k = 0; k < BYTES_IN_WORD; k++) state[j][i] = (state[j][i] ^ GF_Multiplication(constantMatrixInv[j][k], temp[k])); } } free(temp); } Byte GF_Multiplication(Byte a, Byte b) { bool *temp = (bool *)malloc(sizeof(bool) * BIT_IN_BYTE * 2); for(int i = 0; i < BIT_IN_BYTE; i++) { temp[i] = b % 2; b /= 2; } short result = 0; for(int i = 0; i < BIT_IN_BYTE; i++) { result = result ^ ((temp[i] * a) << i); } int count = 0; int temp_result = result; for(int i = 0; i < BIT_IN_BYTE * 2; i++) { temp[count++] = temp_result % 2; temp_result /= 2; } for(int i = BIT_IN_BYTE; i < BIT_IN_BYTE * 2; i++) if(temp[i] == 1) result = result ^ GF_constant[i - BIT_IN_BYTE]; free(temp); return (Byte)result; } void addRoundKey(Byte state[][BYTES_IN_WORD], Byte* key, int round) { for(int i = 0; i < BYTES_IN_WORD; i++) for(int j = 0; j < BYTES_IN_WORD; j++) state[j][i] = (state[j][i] ^ key[round * BYTES_IN_ROUND + i * 4 + j]); } void printState(Byte state[][BYTES_IN_WORD]) { for(int i = 0; i < BYTES_IN_WORD; i++) { for(int j = 0; j < BYTES_IN_WORD; j++) printf("%02X ", state[i][j]); printf("\n"); } printf("\n"); }
2.关于CBC(密码分组链接模式),如它的名称所示,它是让相邻的分组进行异或操作,让前后分组有关联,在发送方,异或要在加密之前完成,相对的,在接收方,异或操作要在解密之后进行,如下图所示CBC模式:
其中关键的是chainModeDecryption(Byte *stream,Byte *IV,Byte *fullKey,int size);函数,如下,stream数组是密文数组(Byte),IV为初始向量,fullkey为密钥扩展后的key,size是密文的长度,具体代码实现如下:
void chainModeDecryption(Byte *stream,Byte *IV,Byte *fullKey,int size){ //将解密后的明文保存在restream数组 Byte *restream = new Byte[size+1]; int cnt = 0; for(int r = 0;r*16<size;r++){ //将密文转化为状态 Byte state[BYTES_IN_WORD][BYTES_IN_WORD]; for(int i = 0;i<BYTES_IN_WORD;i++){ for(int j = 0;j<BYTES_IN_WORD;j++){ state[j][i] = stream[r*16+i*4+j]; } } AES_Decryption(state,fullKey);//AES解密 //解密结果与前一个分组密文异或,第一个分组则和初始向量异或 for(int i = 0;i<BYTES_IN_WORD;i++){ for(int j = 0;j<BYTES_IN_WORD;j++){ restream[cnt] = state[j][i]^IV[i*4+j]; ++cnt; } } //将后一个密文复制到IV for(int i = 0;i<16;i++){ IV[i] = stream[r*16+i]; } } //输出结果 for(int i = 16;i<cnt;i++){ cout<<(char)(restream[i]); } delete [] restream; }
3.关于CTR(计数器模式):CTR模式不存在反馈机制,也不是对明文进行直接的加密,而是对一个counter(这里是IV,)进行加密,而这个counter每进行一轮就加1,如下图所示:
其中关键的是counterModeDecryption(Byte *stream,Byte *IV,Byte *fullKey,int size);函数,如下,stream数组是密文数组(Byte),IV为初始向量,fullkey为密钥扩展后的key,size是密文的长度,具体代码如下:
void counterModeDecryption(Byte *stream,Byte *IV,Byte *fullKey,int size){ //将解密后的明文保存在restream数组 Byte *restream = new Byte[size+1]; int cnt = 0; for(int r = 0;r*16<size;r++){ //将初始向量IV转化为状态 Byte state[BYTES_IN_WORD][BYTES_IN_WORD]; for(int i = 0;i<BYTES_IN_WORD;i++){ for(int j = 0;j<BYTES_IN_WORD;j++){ state[j][i] = IV[i*4+j]; } } AES_Encryption(state,fullKey);//AES加密初始向量IV //将密文与加密后的IV异或 for(int i = 0;i<BYTES_IN_WORD;i++){ for(int j = 0;j<BYTES_IN_WORD;j++){ restream[cnt] = state[j][i]^stream[(r+1)*16+i*4+j]; cnt++; } } //计数器+1(即IV[15]++;) IV[15] = IV[15]+1; } //输出结果 for(int i = 0;i<cnt-16;i++){ cout<<(char)(restream[i]); } delete [] restream; }
标签:
原文地址:http://blog.csdn.net/u012629110/article/details/51190774