现在的电子设备中,通讯报文的收发都需要进行加密和解密操作,不能明文发送,因为明文容易被黑客破解出协议报文,进而改动协议数据,以伪指令报文的方式进行发送,这样会造成非常严重的安全隐患,尤其是在生命科学医用电子仪器设备,自能驾驶汽车应用上,黑客入侵会导致威胁生命安全的危害事件发生。
加密和解密变得越来越重要,随着物联网和车联网的迅速崛起,信息安全变成一种新型的科研方向。本文就是以AES128对称加密和解密算法来进行解释和说明数据加密和解密操作的。
AES(Advanced Encryption Standard)是对称加密算法的一个标准,主要用于保护电子数据的安全。AES 支持128、192、和256位密钥长度,其中AES-128是最常用的一种,它使用128位(16字节)的密钥进行加密和解密操作。AES属于分组密码,每次操作128位(16字节)的数据块。
AES128加密流程
AES的加密过程包括以下几步:
密钥扩展(Key Expansion):密钥会通过一个密钥扩展算法生成一系列称为“轮密钥”(Round Keys)的密钥。AES-128需要10轮,每轮用到一个轮密钥。

这里的K矩阵就是原始密钥,把每一列用4维向量w来表示,就拆分成了w0,w1,w2,w3,将w3进行g中的运算,先是把4个字节左环移,然后对这4个字节进行S盒变换(字节代替),变换完后,最左面的字节与RCj相加,AES128加密要把以上步骤进行10轮,RCj在每一轮的计算中都不一样,具体如下

2.初始轮(Initial Round):在加密的初始步骤中,将数据块与初始密钥通过按位异或(XOR)进行操作。

3.主要轮(Main Rounds,9轮):每一轮包括四个操作:
(1)字节代换(SubBytes):将每个字节使用一个固定的查找表(S盒,Substitution Box)进行替换。

(2)行移位(ShiftRows):行内移位操作,按特定规则将数据块的每一行向左循环移位。

(3)行移位(ShiftRows):行内移位操作,按特定规则将数据块的每一行向左循环移位。

(4)列混合(MixColumns):列内进行线性变换,使用矩阵乘法混合列的数据。

好了,原理讲完了,现在开始进行编码操作
1.先打开一个建立好的KEIL 串口工程,串口创建就不必再讲了,详情请看我之前的串口帖子。

2.在KEIL中添加AES128文件夹路劲和文件


3.AES128的核心算法


//Unsigned char *state ¨C Pointer to data block to be encrypted
//Unsigned char *key ¨C Pointer to 128-bit key
//Unsigned char dir ¨C Value that dictates Encryption (¡®0¡¯) or Decryption (¡®1¡¯)
void aes_enc_dec(unsigned char *state, unsigned char *key, unsigned char dir)
{
unsigned char buf1, buf2, buf3, buf4, round, i;
// In case of decryption
if (dir) {
// compute the last key of encryption before starting the decryption
for (round = 0 ; round < 10; round++) {
//key schedule
key[0] = sbox[key[13]]^key[0]^Rcon[round];
key[1] = sbox[key[14]]^key[1];
key[2] = sbox[key[15]]^key[2];
key[3] = sbox[key[12]]^key[3];
for (i=4; i<16; i++) {
key[i] = key[i] ^ key[i-4];
}
}
//first Addroundkey
for (i = 0; i <16; i++){
state[i]=state[i] ^ key[i];
}
}
// main loop
for (round = 0; round < 10; round++){
if (dir){
//Inverse key schedule
for (i=15; i>3; --i) {
key[i] = key[i] ^ key[i-4];
}
key[0] = sbox[key[13]]^key[0]^Rcon[9-round];
key[1] = sbox[key[14]]^key[1];
key[2] = sbox[key[15]]^key[2];
key[3] = sbox[key[12]]^key[3];
} else {
for (i = 0; i <16; i++){
// with shiftrow i+5 mod 16
state[i]=sbox[state[i] ^ key[i]];
}
//shift rows
buf1 = state[1];
state[1] = state[5];
state[5] = state[9];
state[9] = state[13];
state[13] = buf1;
buf1 = state[2];
buf2 = state[6];
state[2] = state[10];
state[6] = state[14];
state[10] = buf1;
state[14] = buf2;
buf1 = state[15];
state[15] = state[11];
state[11] = state[7];
state[7] = state[3];
state[3] = buf1;
}
//mixcol - inv mix
if ((round > 0 && dir) || (round < 9 && !dir)) {
for (i=0; i <4; i++){
buf4 = (i << 2);
if (dir){
// precompute for decryption
buf1 = galois_mul2(galois_mul2(state[buf4]^state[buf4+2]));
buf2 = galois_mul2(galois_mul2(state[buf4+1]^state[buf4+3]));
state[buf4] ^= buf1; state[buf4+1] ^= buf2; state[buf4+2] ^= buf1; state[buf4+3] ^= buf2;
}
// in all cases
buf1 = state[buf4] ^ state[buf4+1] ^ state[buf4+2] ^ state[buf4+3];
buf2 = state[buf4];
buf3 = state[buf4]^state[buf4+1]; buf3=galois_mul2(buf3); state[buf4] = state[buf4] ^ buf3 ^ buf1;
buf3 = state[buf4+1]^state[buf4+2]; buf3=galois_mul2(buf3); state[buf4+1] = state[buf4+1] ^ buf3 ^ buf1;
buf3 = state[buf4+2]^state[buf4+3]; buf3=galois_mul2(buf3); state[buf4+2] = state[buf4+2] ^ buf3 ^ buf1;
buf3 = state[buf4+3]^buf2; buf3=galois_mul2(buf3); state[buf4+3] = state[buf4+3] ^ buf3 ^ buf1;
}
}
if (dir) {
//Inv shift rows
// Row 1
buf1 = state[13];
state[13] = state[9];
state[9] = state[5];
state[5] = state[1];
state[1] = buf1;
//Row 2
buf1 = state[10];
buf2 = state[14];
state[10] = state[2];
state[14] = state[6];
state[2] = buf1;
state[6] = buf2;
//Row 3
buf1 = state[3];
state[3] = state[7];
state[7] = state[11];
state[11] = state[15];
state[15] = buf1;
for (i = 0; i <16; i++){
// with shiftrow i+5 mod 16
state[i]=rsbox[state[i]] ^ key[i];
}
} else {
//key schedule
key[0] = sbox[key[13]]^key[0]^Rcon[round];
key[1] = sbox[key[14]]^key[1];
key[2] = sbox[key[15]]^key[2];
key[3] = sbox[key[12]]^key[3];
for (i=4; i<16; i++) {
key[i] = key[i] ^ key[i-4];
}
}
}
if (!dir) {
//last Addroundkey
for (i = 0; i <16; i++){
// with shiftrow i+5 mod 16
state[i]=state[i] ^ key[i];
} // enf for
} // end if (!dir)
} // end function

这个函数
aes_enc_dec(state,key1,0);//“0”表示加密
aes_enc_dec(state,key2,1);//“1”表示解密


主函数里添加如下代码
printf("/AES128加密解密测试********/\n");
unsigned char state[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
//unsigned char ciphertext[] = {0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
// 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a};
unsigned char key1[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
unsigned char key2[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
printf("原始数据state为");
for(i=0;i<16;i++)
printf("state[%d] = 0x%x\r\n",i,state[i]);
aes_enc_dec(state,key1,0);//“0”表示加密
printf("AES128加密后的数据state为");
for(i=0;i<16;i++)
printf("state[%d] = 0x%x\r\n",i,state[i]);
aes_enc_dec(state,key2,1);//“1”表示解密
printf("AES128解密后的数据state为");
for(i=0;i<16;i++)
printf("state[%d] = 0x%x\r\n",i,state[i]);


下载到板子上运行,验证AES128算法,通过串口助手查看数据


最终结果解密后能还原原始数据,AES128测试OK!