AES加密
AES算法原理和C語言實現
AES加密算法圖解
AES算法原理:
AES算法可以歸納爲四種操作處理,分別爲密鑰加法層、字節代換層、行位移層、列混淆層
標準的AES算法密鑰和明文都是等長的,一共有128位,196位和256位三種長度,這裏重點講解128位的情況:
128位(16個字節)的的明文會被處理成4X4的二維數組
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
---|
↓轉化
1 | 5 | 9 | 13 |
---|---|---|---|
2 | 6 | 10 | 14 |
3 | 7 | 11 | 15 |
4 | 8 | 12 | 16 |
,AES算法在處理輪數上只有最後一輪操作與前面的輪數有些不同的地方(少了列混淆這一步),每一輪進行加密時,密鑰都會進行一次處理
1.密鑰處理
子密鑰的生存是以列爲單位進行的,子密鑰在加密過程中會根據需要生成多個,而這多個密鑰都是通過初始密鑰擴展而來的
首先將初始密鑰(16個字節)轉化成4X4的二維數組,然後對該數組進行擴展操作,擴展規則如下:
下圖爲初始子密鑰
我們首先把每一列分成一組分別記爲w0,w1,w2,w3,後面依次類推
(1)若w[i]爲4的倍數例如w4,那麼該列的密鑰生成公式如下:
W[i]=W[i-4]^W[i-1]
即將該列往前4個列的每一位和前1個列的進行按位異或操作
(2)若w[i]爲4的倍數例如w4,那麼該列的密鑰生成公式如下:
W[i]=W[i-4]^G(W[i-1])
即將該列往前4個列的每一位和前1個列的G函數返回值進行按位異或操作
G函數首先會將4個輸入字節進行翻轉,並執行一個按字節的S盒替換,最後用第一個字節與輪系數Rcon進行異或操作。輪系數是一個含有10個元素的一位數組,一個元素1個字節
輪系數Rcon | 0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40 | 0x80 | 0x1b | 0x36 |
---|
S盒形成過程:S盒是通過將輸入的數據進行乘法逆元計算、然後將計算結果進行仿射映射得到的。
具體原理實現稍後補上!!!!
總之按照這種方式,我們最後會生成所有的子密鑰
代碼如下:
//用於密鑰擴展 Rcon[0]作爲填充,沒有實際用途
const unsigned int Rcon[11] = { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 };
int Key_S_Substitution(unsigned char(*ExtendKeyArray)[44], unsigned int nCol)
{
int ret = 0;
for (int i = 0; i < 4; i++)
{
ExtendKeyArray[i][nCol] = S_Table[(ExtendKeyArray[i][nCol]) >> 4][(ExtendKeyArray[i][nCol]) & 0x0F];
}
return ret;
}
int G_Function(unsigned char(*ExtendKeyArray)[44], unsigned int nCol)
{
int ret = 0;
//1、將擴展密鑰矩陣的nCol-1列複製到nCol列上,並將nCol列第一行的元素移動到最後一行,其他行數上移一行
for (int i = 0; i < 4; i++)
{
ExtendKeyArray[i][nCol] = ExtendKeyArray[(i + 1) % 4][nCol - 1];
}
//2、將nCol列進行S盒替換
Key_S_Substitution(ExtendKeyArray, nCol);
//3、將該列第一行元素與Rcon進行異或運算
ExtendKeyArray[0][nCol] ^= Rcon[nCol / 4];
return ret;
}
int CalculateExtendKeyArray(const unsigned char(*PasswordArray)[4], unsigned char(*ExtendKeyArray)[44])
{
int ret = 0;
//1、將密鑰數組放入前四列擴展密鑰組
for (int i = 0; i < 16; i++)
{
ExtendKeyArray[i & 0x03][i >> 2] = PasswordArray[i & 0x03][i >> 2];
}
//2、計算擴展矩陣的後四十列
for (int i = 1; i < 11; i++) //進行十輪循環
{
//(1)如果列號是4的倍數,這執行G函數 否則將nCol-1列複製到nCol列上
G_Function(ExtendKeyArray, 4*i);
//(2)每一輪中,各列進行異或運算
// 列號是4的倍數
for (int k = 0; k < 4; k++)//行號
{
ExtendKeyArray[k][4 * i] = ExtendKeyArray[k][4 * i] ^ ExtendKeyArray[k][4 * (i - 1)];
}
// 其他三列
for (int j = 1; j < 4; j++)//每一輪的列號
{
for (int k = 0; k < 4; k++)//行號
{
ExtendKeyArray[k][4 * i + j] = ExtendKeyArray[k][4 * i + j - 1] ^ ExtendKeyArray[k][4 * (i - 1) + j];
}
}
}
return ret;
}
1.明文加密之字節代換層
字節代換層的主要功能是讓數據通過S盒完成從一個字節到另一個字節的映射,S盒的計算方式和密鑰中S盒計算方式相同。S盒是一個擁有256個字節元素的數組,逆S盒與S盒對應用於AES算法的解密,字節代換層將明文的每一個字節作爲數組下標,對進行相關索引操作
若將S盒作爲一維數組處理,則只需要將每個字節作爲索引下標進行索引
若將S盒作爲二維數組,則需要將每個字節的高4位作爲第一個下標,第四位作爲第二個下標來進行索引
S_box[高四位][低四位]
代碼實現如下:
//S盒
const unsigned char S_Table[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, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
};
//字節代換
int Plain_S_Substitution(unsigned char *PlainArray)
{
int ret = 0;
for (int i = 0; i < 16; i++)
{
PlainArray[i] = S_Table[PlainArray[i] >> 4][PlainArray[i] & 0x0F];
}
return ret;
}
//逆S盒
const unsigned char ReS_Table[16][16] =
{
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, 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, 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 Cipher_S_Substitution(unsigned char *CipherArray)
{
int ret = 0;
for (int i = 0; i < 16; i++)
{
CipherArray[i] = ReS_Table[CipherArray[i] >> 4][CipherArray[i] & 0x0F];
}
return ret;
}
2.明文加密之行位移
行位移的操作是將數據作爲一個4X4的字節矩陣處理,然後將這個矩陣的字節進行位置上的置換,其位移操作非常簡單:
將第一行保持不變,第二行整體向左移1個字節,第三行整體向左位移2個字節,第四行整體左移3個字節
int ShiftRows(unsigned int *PlainArray)
{
int ret = 0;
//第一行 不移位
//PlainArray[0] = PlainArray[0];
//第二行 左移8Bit
PlainArray[1] = (PlainArray[1] >> 8) | (PlainArray[1] << 24);
//第三行 左移16Bit
PlainArray[2] = (PlainArray[2] >> 16) | (PlainArray[2] << 16);
//第四行 左移24Bit
PlainArray[3] = (PlainArray[3] >> 24) | (PlainArray[3] << 8);
return ret;
}
2.明文加密之列混淆
列混淆負責對AES算法的元素進行擴散操作,其中包含了矩陣乘法、伽羅瓦域加法和乘法操作
經過了行位移操作後的矩陣通過和固定矩陣相乘得到混淆後的矩陣
而根據伽羅瓦域的相關加法和乘法性質,由於兩個矩陣的加法和乘法都在GF(2^8)中進行,而在擴展域中加法操作等同於異或操作,乘法操作需要通過一個乘法函數實現
伽羅瓦域相關知識:矩陣元素的乘法和加法都是定義在基於GF(2^8)上的二元運算,並不是通常意義上的乘法和加法,
其實這種二元運算的加法等價於兩個字節的異或
乘法則複雜一點。對於一個8位的二進制數來說,使用域上的乘法乘以(00000010)等價於左移1位(低位補0)後,再根據情況同(00011011)進行異或運算
舉個栗子:
假設S1=(a7,a6,a5,a4,a3,a2,a1,a0),那麼0x02*S1,就可以理解爲
即,如果a7=1,則進行異或操作,否則就不進行任何操作
同理,若爲S1*(00000100)那麼就將(00000100)拆分成兩個(00000010)進行乘法操作
若爲S1*(00000011)那麼久按照如下拆分,將兩個乘積分別異或
伽羅瓦域乘法:
///////////////////////////////////////////////////////////////
//功能: 伽羅瓦域內的乘法運算 GF(128)
//參數: Num_L 輸入的左參數
// Num_R 輸入的右參數
//返回值:計算結果
char GaloisMultiplication(unsigned char Num_L, unsigned char Num_R)
{
//定義變量
unsigned char Result = 0; //伽羅瓦域內乘法計算的結果
while (Num_L)
{
//如果Num_L最低位是1就異或Num_R,相當於加上Num_R * 1
if (Num_L & 0x01)
{
Result ^= Num_R;
}
//Num_L右移一位,相當於除以2
Num_L = Num_L >> 1;
//如果Num_R最高位爲1
if (Num_R & 0x80)
{
//左移一位相當於乘二
Num_R = Num_R << 1; //注:這裏會丟失最高位,但是不用擔心
Num_R ^= 0x1B; //計算伽羅瓦域內除法Num_R = Num_R / (x^8(剛好丟失最高位) + x^4 + x^3 + x^1 + 1)
}
else
{
//左移一位相當於乘二
Num_R = Num_R << 1;
}
}
return Result;
}
矩陣乘法:
//列混淆左乘矩陣
const unsigned char MixArray[4][4] =
{
0x02, 0x03, 0x01, 0x01,
0x01, 0x02, 0x03, 0x01,
0x01, 0x01, 0x02, 0x03,
0x03, 0x01, 0x01, 0x02
};
int MixColum(unsigned char(*PlainArray)[4])
{
int ret = 0;
//定義變量
unsigned char ArrayTemp[4][4];
//初始化變量
memcpy(ArrayTemp, PlainArray, 16);
//矩陣乘法 4*4
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
PlainArray[i][j] =
MixArray[i][0] * ArrayTemp[0][j] +
MixArray[i][1] * ArrayTemp[1][j] +
MixArray[i][2] * ArrayTemp[2][j] +
MixArray[i][3] * ArrayTemp[3][j];
}
}
return ret;
}
整體列混淆代碼:
const unsigned char MixArray[4][4] =
{
0x02, 0x03, 0x01, 0x01,
0x01, 0x02, 0x03, 0x01,
0x01, 0x01, 0x02, 0x03,
0x03, 0x01, 0x01, 0x02
};
int MixColum(unsigned char(*PlainArray)[4])
{
int ret = 0;
//定義變量
unsigned char ArrayTemp[4][4];
//初始化變量
memcpy(ArrayTemp, PlainArray, 16);
//矩陣乘法 4*4
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
PlainArray[i][j] =
GaloisMultiplication(MixArray[i][0], ArrayTemp[0][j]) ^
GaloisMultiplication(MixArray[i][1], ArrayTemp[1][j]) ^
GaloisMultiplication(MixArray[i][2], ArrayTemp[2][j]) ^
GaloisMultiplication(MixArray[i][3], ArrayTemp[3][j]);
}
}
return ret;
}
Java代碼實現
import java.util.Base64;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AES {
public static final String KEY_ALGORITHM="AES";
public static final String CIPHER_ALGORITHM="AES/ECB/PKCS5Padding";
private static Key toKey(byte[] key) throws Exception{
//實例化AES密鑰
SecretKey secretKey=new SecretKeySpec(key,KEY_ALGORITHM);
return secretKey;
}
public static byte[] decrypt(byte[] data,byte[] key)throws Exception{
Key k=toKey(key);
Cipher cipher =Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
public static byte[] encrypt(byte[] data,byte[] key)throws Exception{
Key k=toKey(key);
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static byte[] initKey() throws Exception{
//實例化
KeyGenerator kg=KeyGenerator.getInstance(KEY_ALGORITHM);
//AES 要求密鑰長度爲128位,192位或者256位
kg.init(128);
//生成密鑰
SecretKey secretKey=kg.generateKey();
//獲得密鑰的二進制編碼形式
return secretKey.getEncoded();
}
public static void main(String[] args) throws Exception {
String inputStr="AES";
byte[] inputData=inputStr.getBytes();
System.out.println("原文:\t"+inputStr);
byte[] key=AES.initKey();
System.out.println("密鑰:\t"+new String(Base64.getEncoder().encode(key)));
//加密
inputData=AES.encrypt(inputData, key);
System.out.println("加密後:\t"+new String(Base64.getEncoder().encode(inputData)));
byte[] outputData=AES.decrypt(inputData, key);
String outputStr=new String(outputData);
System.out.println("解密後:\t"+outputStr);
//S2Tpe4l3YKuJB8Ew51dAprqX2WKJhRUxM5SolIgqnoY=
//KK7tt4PIdJZkTwz6t0d2at6eEkifcTtsoSHl2J1BmD8=
}
}
參考鏈接:
https://blog.csdn.net/qq_28205153/article/details/55798628
https://bbs.pediy.com/thread-253884.htm