CRC碼由發送端計算,放置於發送資訊報文的尾部。接收資訊的裝置再重新計算接收到資訊報文的CRC,比較計算得到的CRC是否與接收到的相符,如果兩者不相符,則表明出錯。
校驗碼的計算多項式爲(X16 + X15 + X2 + 1)。具體CRC16碼的計算方法是:
1.預置1個16位元的暫存器爲十六進制FFFF(即全爲1);稱此暫存器爲CRC暫存器;
2.把第一個8位元二進制數據 (既通訊資訊幀的第一個位元組)與16位元的CRC暫存器的低8位元相互斥或,把結果放於CRC暫存器;
3.把CRC暫存器的內容右移一 位(朝低位)用0填補最高位,並檢查右移後的移出位;
4.如果移出位爲0:重複第3步(再次右移一位);
如果移出位爲1:CRC暫存器與多項式A001(1010 0000 0000 0001)進行互斥或;(Modbus)
5.重複步驟3和4,直到右移8次,這樣整個8位元數據全部進行了處理;
6.重複步驟2到步驟5,進行通訊資訊幀下一個位元組的處理;
7.將該通訊資訊幀所有位元組按上述步驟計算完成後,得到的16位元CRC暫存器的高、低位元組進行交換;
8.最後得到的CRC暫存器內容即爲:CRC碼。
CRC16常見的標準有以下幾種,被用在各個規範中,其演算法原理基本一致,就是在數據的輸入和輸出有所差異,下邊把這些標準的差異列出,並給出C語言的演算法實現。
CRC16_CCITT:多項式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在後,結果與0x0000互斥或
CRC16_CCITT_FALSE:多項式x16+x12+x5+1(0x1021),初始值0xFFFF,低位在後,高位在前,結果與0x0000互斥或
CRC16_XMODEM:多項式x16+x12+x5+1(0x1021),初始值0x0000,低位在後,高位在前,結果與0x0000互斥或
CRC16_X25:多項式x16+x12+x5+1(0x1021),初始值0x0000,低位在前,高位在後,結果與0xFFFF互斥或
CRC16_MODBUS:多項式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在後,結果與0x0000互斥或
CRC16_IBM:多項式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在後,結果與0x0000互斥或
CRC16_MAXIM:多項式x16+x15+x2+1(0x8005),初始值0x0000,低位在前,高位在後,結果與0xFFFF互斥或
CRC16_USB:多項式x16+x15+x2+1(0x8005),初始值0xFFFF,低位在前,高位在後,結果與0xFFFF互斥或
模式
|
多項式
|
初始值
|
數據位序
|
結果處理
|
CRC16_CCITT
|
x16+x12+x5+1(0x1021)
|
0x0000
|
低位在前,高位在後
|
與0x0000互斥或
|
CRC16_CCITT_FALSE
|
x16+x12+x5+1(0x1021)
|
0xFFFF
|
低位在後,高位在前
|
與0x0000互斥或
|
CRC16_XMODEM
|
x16+x12+x5+1(0x1021)
|
0x0000
|
低位在後,高位在前
|
與0x0000互斥或
|
CRC16_X25
|
x16+x12+x5+1(0x1021)
|
0x0000
|
低位在後,高位在前
|
與0xFFFF互斥或
|
CRC16_ MODBUS
|
x16+x15+x2+1(0x8005)
|
0xFFFF
|
低位在前,高位在後
|
與0x0000互斥或
|
CRC16_ IBM
|
x16+x15+x2+1(0x8005)
|
0x0000
|
低位在前,高位在後
|
與0x0000互斥或
|
CRC16_ MAXIM
|
x16+x15+x2+1(0x8005)
|
0x0000
|
低位在前,高位在後
|
與0xFFFF互斥或
|
CRC16_ USB
|
x16+x15+x2+1(0x8005)
|
0xFFFF
|
低位在前,高位在後
|
與0xFFFF互斥或
|
多項式產生:
如x16+x12+x5+1
x16表示第16位元爲1,x5表示第5位爲1
(1 << 16) | (1 << 12) | (1 << 5) | (1) = 0x11021
但是CRC16只取低16位元,寫成16進位制數就是 0x1021
CRC16的演算法原理:
1.根據CRC16的標準選擇初值CRCIn的值。
2.將數據的第一個位元組與CRCIn高8位元互斥或。
3.判斷最高位,若該位爲 0 左移一位,若爲 1 左移一位再與多項式Hex碼互斥或。
4.重複3直至8位元全部移位計算結束。
5.重複將所有輸入數據操作完成以上步驟,所得16位元數即16位元CRC校驗碼。
根據演算法原理與標準要求就能簡單的寫出具體程式:
[cpp] view plain copy
- unsigned short CRC16_CCITT(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0x0000;
- unsigned short wCPoly = 0x1021;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8(&wChar,&wChar);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint16(&wCRCin,&wCRCin);
- return (wCRCin) ;
- }
- unsigned short CRC16_CCITT_FALSE(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0xFFFF;
- unsigned short wCPoly = 0x1021;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- return (wCRCin) ;
- }
- unsigned short CRC16_XMODEM(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0x0000;
- unsigned short wCPoly = 0x1021;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- return (wCRCin) ;
- }
-
- unsigned short CRC16_X25(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0xFFFF;
- unsigned short wCPoly = 0x1021;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8(&wChar,&wChar);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint16(&wCRCin,&wCRCin);
- return (wCRCin^0xFFFF) ;
- }
-
- unsigned short CRC16_MODBUS(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0xFFFF;
- unsigned short wCPoly = 0x8005;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8(&wChar,&wChar);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint16(&wCRCin,&wCRCin);
- return (wCRCin) ;
- }
- unsigned short CRC16_IBM(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0x0000;
- unsigned short wCPoly = 0x8005;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8(&wChar,&wChar);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint16(&wCRCin,&wCRCin);
- return (wCRCin) ;
- }
- unsigned short CRC16_MAXIM(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0x0000;
- unsigned short wCPoly = 0x8005;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8(&wChar,&wChar);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint16(&wCRCin,&wCRCin);
- return (wCRCin^0xFFFF) ;
- }
- unsigned short CRC16_USB(unsigned char *puchMsg, unsigned int usDataLen)
- {
- unsigned short wCRCin = 0xFFFF;
- unsigned short wCPoly = 0x8005;
- unsigned char wChar = 0;
-
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8(&wChar,&wChar);
- wCRCin ^= (wChar << 8);
- for(int i = 0;i < 8;i++)
- {
- if(wCRCin & 0x8000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint16(&wCRCin,&wCRCin);
- return (wCRCin^0xFFFF) ;
- }
[cpp] view plain copy
- void InvertUint8(unsigned char *dBuf,unsigned char *srcBuf)
- {
- int i;
- unsigned char tmp[4];
- tmp[0] = 0;
- for(i=0;i< 8;i++)
- {
- if(srcBuf[0]& (1 << i))
- tmp[0]|=1<<(7-i);
- }
- dBuf[0] = tmp[0];
-
- }
- void InvertUint16(unsigned short *dBuf,unsigned short *srcBuf)
- {
- int i;
- unsigned short tmp[4];
- tmp[0] = 0;
- for(i=0;i< 16;i++)
- {
- if(srcBuf[0]& (1 << i))
- tmp[0]|=1<<(15 - i);
- }
- dBuf[0] = tmp[0];
- }
- void InvertUint32(unsigned int *dBuf,unsigned int *srcBuf)
- {
- int i;
- unsigned int tmp[4];
-
- tmp[0] = 0;
-
- for(i=0;i< 32;i++)
- {
- if(srcBuf[0]& (1 << i))
- tmp[0]|=1<<(15 - i);
- }
- dBuf[0] = tmp[0];
- }
具體驗證使用這個工具,內含CRC演算法的計算,和後邊的部落格中提到的其他演算法的工具合集
加密解密演算法工具集
在這個基礎上也加入CRC32 的校驗演算法
[html] view plain copy
- //CRC32演算法:
- unsigned int CRC32(unsigned char *puchMsg, unsigned int usDataLen)
- {
- int i;
- unsigned int wCRCin = 0xFFFFFFFF;
- unsigned int wCPoly = 0x04C11DB7;
- unsigned int wChar = 0;
- while (usDataLen--)
- {
- wChar = *(puchMsg++);
- InvertUint8((unsigned char *)&wChar,(unsigned char *)&wChar);
- wCRCin ^= (wChar << 24);
- for(i = 0;i < 8;i++)
- {
- if(wCRCin & 0x80000000)
- wCRCin = (wCRCin << 1) ^ wCPoly;
- else
- wCRCin = wCRCin << 1;
- }
- }
- InvertUint32(&wCRCin,&wCRCin);
- return (wCRCin ^ 0xFFFFFFFF) ;
- }
對於CRC32可能還有其他的多項式和初始值和結果值是否需要互斥或以及輸入數據是否需要位序倒轉等要求在原始碼中修改