现在的电子设备中,通讯报文的收发都需要进行加密和解密操作,不能明文发送,因为明文容易被黑客破解出协议报文,进而改动协议数据,以伪指令报文的方式进行发送,这样会造成非常严重的安全隐患,尤其是在生命科学医用电子仪器设备,自能驾驶汽车应用上,黑客入侵会导致威胁生命安全的危害事件发生。
加密和解密变得越来越重要,随着物联网和车联网的迅速崛起,信息安全变成一种新型的科研方向。本文就是以DES加密和解密算法来进行解释和说明数据加密和解密操作的。
DES(Data Encryption Standard)是目前最为流行的加密算法之一。DES是对称的,也就是说它使用同一个密钥来加密和解密数据。
DES还是一种分组加密算法,该算法每次处理固定长度的数据段,称之为分组。DES分组的大小是64位,如果加密的数据长度不是64位的倍数,可以按照某种具体的规则来填充位。
从本质上来说,DES的安全性依赖于虚假表象,从密码学的术语来讲就是依赖于“混乱和扩散”的原则。混乱的目的是为隐藏任何明文同密文、或者密钥之间的关系,而扩散的目的是使明文中的有效位和密钥一起组成尽可能多的密文。两者结合到一起就使得安全性变得相对较高。
DES算法具体通过对明文进行一系列的排列和替换操作来将其加密。过程的关键就是从给定的初始密钥中得到16个子密钥的函数。要加密一组明文,每个子密钥按照顺序(1-16)以一系列的位操作施加于数据上,每个子密钥一次,一共重复16次。每一次迭代称之为一轮。要对密文进行解密可以采用同样的步骤,只是子密钥是按照逆向的顺序(16-1)对密文进行处理。
计算16个子密钥
上面提到DES算法的第一步就是从初始密钥中计算得出16个子密钥。图示1展示了这个过程。DES使用一个56位的初始密钥,但是这里提供的是一个64位的值,这是因为在硬件实现中每8位可以用于奇偶校验,在软件实现中多出的位只是简单的忽略掉。要获得一个56位的密钥,可以执照表1的方式执行密钥转换。解释一下表1,按照从左往右从上往下的方式看,表格中每个位置P包含初始密钥中位在转换后的密钥中所占的位置。比如,初始密钥中的第57位就是转换后的密钥中的第1位,而初始密钥中的第49位则变成转换后的密钥中的第2位,以此类推...。(数据位的计数顺序按照从左到右从1开始的)
表1:DES中密钥的转换表(DesTransform[56])
将密钥转换为56位后,接下来计算子密钥。首先,将56位的密钥分为两个28位的组。然后,针对每个子密钥,根据子密钥的序列值(也就是16个子密钥中的第几个)旋转这两组值(旋转的位数见表2),然后重新合并。之后, 再按照表3所示对重组后的密钥进行置换,使56位的子密钥缩小为48位(注意表3只有48位,丢弃了8位) 。 这个排列过程就称为置换选择 。
针对16个子密钥,每个子密钥重复一次该过程。这里的目的是保证将初始密钥中的不同位在每一轮排列后应用于加密的数据上。
表2:针对DES子密钥每一轮的旋转次数(Round轮,Rotations旋转次数)(DesRotations)
表3:DES子密钥的置换选择(Despermuted[48])
图1:在DES中计算子密钥的过程
加密和解密数据块
DES算法的实现
#define EN0 0 /* MODE == encrypt 加密*/
#define DE1 1 /* MODE == decrypt 解密*/
#define ENDE 2 /* MODE == Encrypt and Decrypt 加密和解密*/
/// Key Structure Definition ///
typedef struct {
unsigned long ek[32];
unsigned long dk[32];
} des_ctx;
/// Function Declarations ///
extern void DES_Enc_CBC (des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucIV);
extern void DES_Dec_CBC (des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucIV);
extern void TripleDES_ENC_CBC( unsigned char *pucData, short sBlocks, unsigned char *pucKey1,unsigned char *pucKey2,unsigned char *pucKey3, unsigned char *pucIV);
extern void TripleDES_DEC_CBC( unsigned char *pucData, short sBlocks, unsigned char *pucKey1,unsigned char *pucKey2,unsigned char *pucKey3, unsigned char *pucIV);
extern void Des_Key(des_ctx *dc, unsigned char pucKey, short sMode);
/
- Function that starts key schedule.
*/
extern void Des_Enc(des_ctx *dc, unsigned char pucData, short sBlocks);
/
- Function that starts encoding
*/
extern void Des_Dec(des_ctx *dc, unsigned char pucData, short sBlocks);
/
- Function that starts decoding
*/
extern void TripleDES_ENC( des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucKey1, unsigned char *pucKey2, unsigned char *pucKey3);
extern void TripleDES_DEC( des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucKey1, unsigned char *pucKey2, unsigned char *pucKey3);
#include <stdio.h>
#include "DES.h"
void CBCfunc(unsigned char* pucData, unsigned char BlockNumber){
int i;
for (i=0; i<8; i++){
pucData[(BlockNumber -1)*8 + i] = pucData[(BlockNumber -2)*8 +i] ^ pucData[(BlockNumber -1)*8 +i];
}
}
void deskey(unsigned char key, short edf) {
/ Thanks to James Gillogly & Phil Karn! */
register int i, j, l, m, n;
unsigned char pc1m[56], pcr[56];
unsigned long kn[32];
for ( j = 0; j < 56; j++ ) {
l = pc1[j];
m = l & 07;
pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
}
for( i = 0; i < 16; i++ ) {
if( edf == DE1 ) m = (15 - i) << 1;
else m = i << 1;
n = m + 1;
kn[m] = kn[n] = 0L;
for( j = 0; j < 28; j++ ) {
l = j + totrot[i];
if( l < 28 ) pcr[j] = pc1m[l];
else pcr[j] = pc1m[l - 28];
}
for( j = 28; j < 56; j++ ) {
l = j + totrot[i];
if( l < 56 ) pcr[j] = pc1m[l];
else pcr[j] = pc1m[l - 28];
}
for( j = 0; j < 24; j++ ) {
if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];
}
}
cookey(kn);
}
static void cookey(unsigned long *raw1) {
register unsigned long *cook, *raw0;
unsigned long dough[32];
register int i;
cook = dough;
for( i = 0; i < 16; i++, raw1++ ) {
raw0 = raw1++;
*cook = (*raw0 & 0x00fc0000L) << 6;
*cook |= (*raw0 & 0x00000fc0L) << 10;
*cook |= (*raw1 & 0x00fc0000L) >> 10;
*cook++|= (*raw1 & 0x00000fc0L) >> 6;
*cook = (*raw0 & 0x0003f000L) << 12;
*cook |= (*raw0 & 0x0000003fL) << 16;
*cook |= (*raw1 & 0x0003f000L) >> 4;
*cook++ |= (*raw1 & 0x0000003fL);
}
usekey(dough);
}
void cpkey(unsigned long *into) {
register unsigned long *from, *endp;
from = KnL, endp = &KnL[32];
while( from < endp ) *into++ = *from++;
}
void usekey(unsigned long *from) {
register unsigned long *to, *endp;
to = KnL, endp = &KnL[32];
while( to < endp ) *to++ = from++;
}
/
#if 0
void des(unsigned char *inblock, unsigned char *outblock) {
unsigned long work[2];
scrunch(inblock, work);
desfunc(work, KnL);
unscrun(work, outblock);
}
#endif
*/
static void scrunch(unsigned char *outof, unsigned long *into) {
*into = (*outof++ & 0xffL) << 24;
*into |= (*outof++ & 0xffL) << 16;
*into |= (*outof++ & 0xffL) << 8;
*into++ |= (*outof++ & 0xffL);
*into = (*outof++ & 0xffL) << 24;
*into |= (*outof++ & 0xffL) << 16;
*into |= (*outof++ & 0xffL) << 8;
*into |= (*outof & 0xffL);
}
static void unscrun(unsigned long *outof, unsigned char *into) {
*into++ = (*outof >> 24) & 0xffL;
*into++ = (*outof >> 16) & 0xffL;
*into++ = (*outof >> 8) & 0xffL;
*into++ = *outof++ & 0xffL;
*into++ = (*outof >> 24) & 0xffL;
*into++ = (*outof >> 16) & 0xffL;
*into++ = (*outof >> 8) & 0xffL;
*into = *outof & 0xffL;
}
static void desfunc(unsigned long *block, unsigned long *keys) {
register unsigned long fval, work, right, leftt;
register int round;
leftt = block[0];
right = block[1];
work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;
right ^= work;
leftt ^= (work << 4);
work = ((leftt >> 16) ^ right) & 0x0000ffffL;
right ^= work;
leftt ^= (work << 16);
work = ((right >> 2) ^ leftt) & 0x33333333L;
leftt ^= work;
right ^= (work << 2);
work = ((right >> 8) ^ leftt) & 0x00ff00ffL;
leftt ^= work;
right ^= (work << 8);
right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL;
for( round = 0; round < 8; round++ ) {
work = (right << 28) | (right >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = right ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
leftt ^= fval;
work = (leftt << 28) | (leftt >> 4);
work ^= *keys++;
fval = SP7[ work & 0x3fL];
fval |= SP5[(work >> 8) & 0x3fL];
fval |= SP3[(work >> 16) & 0x3fL];
fval |= SP1[(work >> 24) & 0x3fL];
work = leftt ^ *keys++;
fval |= SP8[ work & 0x3fL];
fval |= SP6[(work >> 8) & 0x3fL];
fval |= SP4[(work >> 16) & 0x3fL];
fval |= SP2[(work >> 24) & 0x3fL];
right ^= fval;
}
right = (right << 31) | (right >> 1);
work = (leftt ^ right) & 0xaaaaaaaaL;
leftt ^= work;
right ^= work;
leftt = (leftt << 31) | (leftt >> 1);
work = ((leftt >> 8) ^ right) & 0x00ff00ffL;
right ^= work;
leftt ^= (work << 8);
work = ((leftt >> 2) ^ right) & 0x33333333L;
right ^= work;
leftt ^= (work << 2);
work = ((right >> 16) ^ leftt) & 0x0000ffffL;
leftt ^= work;
right ^= (work << 16);
work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;
leftt ^= work;
right ^= (work << 4);
*block++ = right;
*block = leftt;
}
/* Validation sets:
*
- Single-length key, single-length plaintext -
- Key : 0123 4567 89ab cdef
- Plain : 0123 4567 89ab cde7
- Cipher : c957 4425 6a5e d31d
**********************************************************************/
void Des_Key(des_ctx *dc, unsigned char *pucKey, short sMode){
if (sMode == EN0){
deskey(pucKey,sMode);
cpkey(dc->ek);
}
else if(sMode == DE1) {
deskey(pucKey, sMode);
cpkey(dc->dk);
}
else if(sMode == ENDE){
deskey(pucKey,EN0);
cpkey(dc->ek);
deskey(pucKey,DE1);
cpkey(dc->dk);
}
}
/* Encrypt several blocks in ECB sMode. Caller is responsible for
short blocks. */
void Des_Enc(des_ctx *dc, unsigned char *pucData, short sBlocks){
unsigned long work[2];
int i;
unsigned char *cp;
cp = pucData;
for(i=0;i<sBlocks;i++){
scrunch(cp,work);
desfunc(work,dc->ek);
unscrun(work,cp);
cp+=8;
}
}
void Des_Dec(des_ctx *dc, unsigned char *pucData, short sBlocks){
unsigned long work[2];
int i;
unsigned char *cp;
cp = pucData;
for(i=0;i<sBlocks;i++){
scrunch(cp,work);
desfunc(work,dc->dk);
unscrun(work,cp);
cp+=8;
}
}
void TripleDES_ENC( des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucKey1, unsigned char *pucKey2, unsigned char *pucKey3){
Des_Key(dc,pucKey1, EN0);
Des_Enc(dc,pucData,sBlocks);
Des_Key(dc,pucKey2, DE1);
Des_Dec(dc, pucData, sBlocks);
Des_Key(dc,pucKey3, EN0);
Des_Enc(dc,pucData,sBlocks);
}
void TripleDES_DEC( des_ctx *dc,unsigned char *pucData, short sBlocks,unsigned char *pucKey1,unsigned char *pucKey2,unsigned char *pucKey3){
Des_Key(dc,pucKey3, DE1);
Des_Dec(dc, pucData, sBlocks);
Des_Key(dc,pucKey2, EN0);
Des_Enc(dc,pucData,sBlocks);
Des_Key(dc,pucKey1, DE1);
Des_Dec(dc, pucData, sBlocks);
}
void TripleDES_ENC_CBC( unsigned char *pucData, short sBlocks, unsigned char *pucKey1, unsigned char *pucKey2, unsigned char *pucKey3, unsigned char *pucIV){
int i,f,x;
des_ctx dc1, dc2, dc3;
if(sBlocks<=1){ //Sanity Check
return;
}
Des_Key(&dc1,pucKey1, EN0);
Des_Key(&dc2,pucKey2, DE1);
Des_Key(&dc3,pucKey3, EN0);
for (f=0;f<16;f++){
pucData[f] ^= pucIV[f];
}
for(i=0; i< sBlocks; i++){
x=i*16;
Des_Enc(&dc1,&pucData[x],1);
Des_Dec(&dc2,&pucData[x],1);
Des_Enc(&dc3,&pucData[x],1);
if( i < (sBlocks-1)){
for(f=0;f<16;f++){
pucIV[x+f] = pucData[x+f];
}
}
else{
for(f=0;f<16;f++){
pucData[x+f+16] = pucData[x+f];
}
}
}
}
void TripleDES_DEC_CBC(unsigned char *pucData, short sBlocks, unsigned char *pucKey1,unsigned char *pucKey2,unsigned char *pucKey3, unsigned char *pucIV){
int i,f,x;
des_ctx dc1, dc2, dc3;
unsigned char temp[8];
if(sBlocks<=1){ //Sanity Check
return;
}
else{
Des_Key(&dc1,pucKey3, DE1); //Key Schedules
Des_Key(&dc2,pucKey2, EN0);
Des_Key(&dc3,pucKey1, DE1);
x=(sBlocks-1)*16; // address of start of last block of data
for(f=0;f<16;f++){
temp[f] = pucData[x+f]; //Copy newIV to Temp
}
for(i=0;i<sBlocks;i++){
Des_Dec(&dc1,&pucData[x],1); //3DES algorithm
Des_Enc(&dc2,&pucData[x],1);
Des_Dec(&dc3,&pucData[x],1);
x = x-16;
if(x >= 0){
for(f=0;f<16;f++){
pucData[x+f+16] ^= pucData[x+f]; //XOR block with input of next block
}
}
else{
for(f=0;f<16;f++){
pucData[f] ^= pucIV[f]; // XOR first block with IV
pucIV[f] = temp[f]; //Update IV;
}
}
}
}
}
void DES_Enc_CBC (des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucIV){
int CurrentBlock = 1;
//unsigned char bx[8];
//unsigned char ax[8];
unsigned char d =0;
unsigned char *cp;
int i;
cp = pucData;
if (sBlocks <= 1) return;
else{
for(i=0; i<8; i++){
pucData[i] ^= pucIV[i];
}
while(CurrentBlock < sBlocks){
Des_Enc(dc,cp,1);
// for(i=0;i<8;i++){
// bx[i] = pucData[i+d];
// }
for (i=0; i <8; i++){
pucData[i+8+d] ^= pucData[i+d];
}
// for(i=0;i<8;i++){
// ax[i+d] = pucData[i+d];
// }
cp+=8;
CurrentBlock++;
d+=8;
}
Des_Enc(dc,cp,1);
for(i=0; i<8; i++){
pucIV[i] = cp[i];
}
// for(i=0;i<8;i++){
// ax[i] = pucData[i+d];
// }
//d=ax[0];
}
}
void DES_Dec_CBC (des_ctx *dc,unsigned char *pucData, short sBlocks, unsigned char *pucIV){
int CurrentBlock;
//unsigned char bx[8];
//unsigned char ax[8];
unsigned char d =0;
unsigned char *cp;
unsigned char temp[8];
int i;
cp = pucData + (sBlocks-1)*8; //Starting DEC backwards
CurrentBlock = sBlocks;
for (i=0; i <8; i++){
temp[i] = cp[i]; //Copying new pucIV (updated version)
}
if (sBlocks <= 1) return;
else{
while(CurrentBlock != 1){
Des_Dec(dc,cp,1); //dec last block
d= (CurrentBlock -1) *8; //reset block count
// for(i=0;i<8;i++){
// bx[i] = pucData[i+d];
// }
for (i=0; i <8; i++){
pucData[i+d] ^= pucData[i+ d - 8];
}
// for(i=0;i<8;i++){
// ax[i+d] = pucData[i+d];
// }
cp-=8;
CurrentBlock--;
//d+=8;
}
Des_Dec(dc,cp,1);
for (i=0; i <8; i++){
pucData[i] ^= pucIV[i];
pucIV[i] = temp[i];
}
// for(i=0;i<8;i++){
// ax[i] = pucData[i+d];
// }
// d=ax[0];
}
}
先打开我们之前使用过的AES128和SHA256算法工程,主要使用里边的串口
在KEIL路径添加
同时工程添加头文件
#include "hal_data.h"
#include "stdio.h"
#include <stdbool.h>
#include "hal_systick.h"
#include "aes_128.h"
#include "sha2.h"
#include "DES.h"
printf("/DES加密解密测试********/\n");
des_ctx dc1; // Key schedule structure
unsigned char *cp;
unsigned char data[] = {0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0xd4, 0x30};
unsigned char key[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xfe};
cp = data;
for(i=0;i<8;i++)
printf("原始数据 data[%d] = 0x%x\r\n",i,*(data+i));
Des_Key(&dc1, key, ENDE); // Sets up key schedule for Encryption and Decryption
Des_Enc(&dc1, cp, 1); //Encrypt Data, Result is stored back into Data
for(i=0;i<8;i++)
printf("加密后的数据 data[%d] = 0x%x\r\n",i,*(cp+i));
Des_Dec(&dc1, cp, 1); //Decrypt Data, Result is stored back into Data
for(i=0;i<8;i++)
printf("解密后的数据 data[%d] = 0x%x\r\n",i,*(cp+i));
编译烧录程序,
打开串口助手查看