打印
[C语言]

CRC算法 反推

[复制链接]
3532|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keaiduoyu123|  楼主 | 2018-2-3 17:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请教高手下面的CRC 能看出来什么算法吗,哪位高手能知道算法如何实现的?谢谢



#include"crc8.h"

#defineP_80xD5


uint8_tcrc8_table[256];


voidinit_crc8_normal_tab(uint8_ttable[],uint8_tpolynom)
{
inti,j;
uint8_tcrc;

for(i=0;i<256;i++)
{
        crc=(uint8_t)i;

        for(j=0;j<8;j++)
        {
        if(crc&0x80)
                                crc=(crc<<1)^polynom;
        else
                                crc<<=1;
        }
               
        table[i]=crc;
}
}



voidinit_crc8_tab(void)
{
init_crc8_normal_tab(crc8_table,P_8);
}


uint8_tupdate_crc8(uint8_tcrc,uint8_tc)
{
        return(crc8_table[crc^c]);
}


uint8_tcalculate_crc8(uint8_tp[],unsignedintlength)
{
        uint8_tcrc;
        unsignedinti;

crc=0;

for(i=0;i<length;i++)
{
crc=update_crc8(crc,p[i]);
}
       
returncrc;
}

相关帖子

沙发
linqing171| | 2018-2-3 20:46 | 只看该作者
table没有贴上来,看不出是哪个多项式。
@hotpower

使用特权

评论回复
板凳
dirtwillfly| | 2018-2-4 16:53 | 只看该作者
看不出的,找菜农大叔问问

使用特权

评论回复
地板
hotpower| | 2018-2-5 09:42 | 只看该作者
点击关注菜农:http://m.toutiao.com/profile/3995409288/


回忆中国象棋数字编码方案的设计过程
http://m.pstatp.com/item/6516715139754885636/
菜农星期公式的推导过程及与其他星期公式的比对
http://m.pstatp.com/i6516662162096652813/
工控应用中实用的“跳水滤波算法”
http://m.toutiaoimg.cn/i6518256890315538948/


万年历为何是四百年一轮回的?
https://m.zjurl.cn/i6518130080357548548/
为何已知一条整数直角边,求另一条整数直角边和整数斜边会有解?
https://m.toutiao.com/i6516527403932582407/


HotPower超级CRC计算器
http://www.21ic.com/tools/HotPower/HotWC3_V1.23.html
菜农密码
http://www.21ic.com/tools/HotPower/HotWC3.html


菜农微信公众号


使用特权

评论回复
5
keaiduoyu123|  楼主 | 2018-2-5 19:22 | 只看该作者
各位高手 ,完整的程序来了,请指点

使用特权

评论回复
6
keaiduoyu123|  楼主 | 2018-2-5 19:23 | 只看该作者
package com.ly.logic.tools;

public class CRC8 {
        /*
         * static byte[] crc8_tab = { (byte) 0, (byte) 94, (byte) 188, (byte) 226,
         * (byte) 97, (byte) 63, (byte) 221, (byte) 131, (byte) 194, (byte) 156,
         * (byte) 126, (byte) 32, (byte) 163, (byte) 253, (byte) 31, (byte) 65,
         * (byte) 157, (byte) 195, (byte) 33, (byte) 127, (byte) 252, (byte) 162,
         * (byte) 64, (byte) 30, (byte) 95, (byte) 1, (byte) 227, (byte) 189, (byte)
         * 62, (byte) 96, (byte) 130, (byte) 220, (byte) 35, (byte) 125, (byte) 159,
         * (byte) 193, (byte) 66, (byte) 28, (byte) 254, (byte) 160, (byte) 225,
         * (byte) 191, (byte) 93, (byte) 3, (byte) 128, (byte) 222, (byte) 60,
         * (byte) 98, (byte) 190, (byte) 224, (byte) 2, (byte) 92, (byte) 223,
         * (byte) 129, (byte) 99, (byte) 61, (byte) 124, (byte) 34, (byte) 192,
         * (byte) 158, (byte) 29, (byte) 67, (byte) 161, (byte) 255, (byte) 70,
         * (byte) 24, (byte) 250, (byte) 164, (byte) 39, (byte) 121, (byte) 155,
         * (byte) 197, (byte) 132, (byte) 218, (byte) 56, (byte) 102, (byte) 229,
         * (byte) 187, (byte) 89, (byte) 7, (byte) 219, (byte) 133, (byte) 103,
         * (byte) 57, (byte) 186, (byte) 228, (byte) 6, (byte) 88, (byte) 25, (byte)
         * 71, (byte) 165, (byte) 251, (byte) 120, (byte) 38, (byte) 196, (byte)
         * 154, (byte) 101, (byte) 59, (byte) 217, (byte) 135, (byte) 4, (byte) 90,
         * (byte) 184, (byte) 230, (byte) 167, (byte) 249, (byte) 27, (byte) 69,
         * (byte) 198, (byte) 152, (byte) 122, (byte) 36, (byte) 248, (byte) 166,
         * (byte) 68, (byte) 26, (byte) 153, (byte) 199, (byte) 37, (byte) 123,
         * (byte) 58, (byte) 100, (byte) 134, (byte) 216, (byte) 91, (byte) 5,
         * (byte) 231, (byte) 185, (byte) 140, (byte) 210, (byte) 48, (byte) 110,
         * (byte) 237, (byte) 179, (byte) 81, (byte) 15, (byte) 78, (byte) 16,
         * (byte) 242, (byte) 172, (byte) 47, (byte) 113, (byte) 147, (byte) 205,
         * (byte) 17, (byte) 79, (byte) 173, (byte) 243, (byte) 112, (byte) 46,
         * (byte) 204, (byte) 146, (byte) 211, (byte) 141, (byte) 111, (byte) 49,
         * (byte) 178, (byte) 236, (byte) 14, (byte) 80, (byte) 175, (byte) 241,
         * (byte) 19, (byte) 77, (byte) 206, (byte) 144, (byte) 114, (byte) 44,
         * (byte) 109, (byte) 51, (byte) 209, (byte) 143, (byte) 12, (byte) 82,
         * (byte) 176, (byte) 238, (byte) 50, (byte) 108, (byte) 142, (byte) 208,
         * (byte) 83, (byte) 13, (byte) 239, (byte) 177, (byte) 240, (byte) 174,
         * (byte) 76, (byte) 18, (byte) 145, (byte) 207, (byte) 45, (byte) 115,
         * (byte) 202, (byte) 148, (byte) 118, (byte) 40, (byte) 171, (byte) 245,
         * (byte) 23, (byte) 73, (byte) 8, (byte) 86, (byte) 180, (byte) 234, (byte)
         * 105, (byte) 55, (byte) 213, (byte) 139, (byte) 87, (byte) 9, (byte) 235,
         * (byte) 181, (byte) 54, (byte) 104, (byte) 138, (byte) 212, (byte) 149,
         * (byte) 203, (byte) 41, (byte) 119, (byte) 244, (byte) 170, (byte) 72,
         * (byte) 22, (byte) 233, (byte) 183, (byte) 85, (byte) 11, (byte) 136,
         * (byte) 214, (byte) 52, (byte) 106, (byte) 43, (byte) 117, (byte) 151,
         * (byte) 201, (byte) 74, (byte) 20, (byte) 246, (byte) 168, (byte) 116,
         * (byte) 42, (byte) 200, (byte) 150, (byte) 21, (byte) 75, (byte) 169,
         * (byte) 247, (byte) 182, (byte) 232, (byte) 10, (byte) 84, (byte) 215,
         * (byte) 137, (byte) 107, 53 };
         */
        static byte[] crc8_tab = { (byte) 0, (byte) 213, (byte) 127, (byte) 170, (byte) 254, (byte) 43, (byte) 129,
                        (byte) 84, (byte) 41, (byte) 252, (byte) 86, (byte) 131, (byte) 215, (byte) 2, (byte) 168, (byte) 125,
                        (byte) 82, (byte) 135, (byte) 45, (byte) 248, (byte) 172, (byte) 121, (byte) 211, (byte) 6, (byte) 123,
                        (byte) 174, (byte) 4, (byte) 209, (byte) 133, (byte) 80, (byte) 250, (byte) 47, (byte) 164, (byte) 113,
                        (byte) 219, (byte) 14, (byte) 90, (byte) 143, (byte) 37, (byte) 240, (byte) 141, (byte) 88, (byte) 242,
                        (byte) 39, (byte) 115, (byte) 166, (byte) 12, (byte) 217, (byte) 246, (byte) 35, (byte) 137, (byte) 92,
                        (byte) 8, (byte) 221, (byte) 119, (byte) 162, (byte) 223, (byte) 10, (byte) 160, (byte) 117, (byte) 33,
                        (byte) 244, (byte) 94, (byte) 139, (byte) 157, (byte) 72, (byte) 226, (byte) 55, (byte) 99, (byte) 182,
                        (byte) 28, (byte) 201, (byte) 180, (byte) 97, (byte) 203, (byte) 30, (byte) 74, (byte) 159, (byte) 53,
                        (byte) 224, (byte) 207, (byte) 26, (byte) 176, (byte) 101, (byte) 49, (byte) 228, (byte) 78, (byte) 155,
                        (byte) 230, (byte) 51, (byte) 153, (byte) 76, (byte) 24, (byte) 205, (byte) 103, (byte) 178, (byte) 57,
                        (byte) 236, (byte) 70, (byte) 147, (byte) 199, (byte) 18, (byte) 184, (byte) 109, (byte) 16, (byte) 197,
                        (byte) 111, (byte) 186, (byte) 238, (byte) 59, (byte) 145, (byte) 68, (byte) 107, (byte) 190, (byte) 20,
                        (byte) 193, (byte) 149, (byte) 64, (byte) 234, (byte) 63, (byte) 66, (byte) 151, (byte) 61, (byte) 232,
                        (byte) 188, (byte) 105, (byte) 195, (byte) 22, (byte) 239, (byte) 58, (byte) 144, (byte) 69, (byte) 17,
                        (byte) 196, (byte) 110, (byte) 187, (byte) 198, (byte) 19, (byte) 185, (byte) 108, (byte) 56, (byte) 237,
                        (byte) 71, (byte) 146, (byte) 189, (byte) 104, (byte) 194, (byte) 23, (byte) 67, (byte) 150, (byte) 60,
                        (byte) 233, (byte) 148, (byte) 65, (byte) 235, (byte) 62, (byte) 106, (byte) 191, (byte) 21, (byte) 192,
                        (byte) 75, (byte) 158, (byte) 52, (byte) 225, (byte) 181, (byte) 96, (byte) 202, (byte) 31, (byte) 98,
                        (byte) 183, (byte) 29, (byte) 200, (byte) 156, (byte) 73, (byte) 227, (byte) 54, (byte) 25, (byte) 204,
                        (byte) 102, (byte) 179, (byte) 231, (byte) 50, (byte) 152, (byte) 77, (byte) 48, (byte) 229, (byte) 79,
                        (byte) 154, (byte) 206, (byte) 27, (byte) 177, (byte) 100, (byte) 114, (byte) 167, (byte) 13, (byte) 216,
                        (byte) 140, (byte) 89, (byte) 243, (byte) 38, (byte) 91, (byte) 142, (byte) 36, (byte) 241, (byte) 165,
                        (byte) 112, (byte) 218, (byte) 15, (byte) 32, (byte) 245, (byte) 95, (byte) 138, (byte) 222, (byte) 11,
                        (byte) 161, (byte) 116, (byte) 9, (byte) 220, (byte) 118, (byte) 163, (byte) 247, (byte) 34, (byte) 136,
                        (byte) 93, (byte) 214, (byte) 3, (byte) 169, (byte) 124, (byte) 40, (byte) 253, (byte) 87, (byte) 130,
                        (byte) 255, (byte) 42, (byte) 128, (byte) 85, (byte) 1, (byte) 212, (byte) 126, (byte) 171, (byte) 132,
                        (byte) 81, (byte) 251, (byte) 46, (byte) 122, (byte) 175, (byte) 5, (byte) 208, (byte) 173, (byte) 120,
                        (byte) 210, (byte) 7, (byte) 83, (byte) 134, (byte) 44, (byte) 249 };

        /**
         * 计算数组的CRC8校验值
         *
         * @param data
         *            需要计算的数组
         * @return CRC8校验值
         */
        public static byte calcCrc8(byte[] data) {
                return calcCrc8(data, 0, data.length, (byte) 0);
        }

        /**
         * 计算CRC8校验值
         *
         * @param data
         *            数据
         * @param offset
         *            起始位置
         * @param len
         *            长度
         * @return 校验值
         */
        public static byte calcCrc8(byte[] data, int offset, int len) {
                return calcCrc8(data, offset, len, (byte) 0);
        }

        /**
         * 计算CRC8校验值
         *
         * @param data
         *            数据
         * @param offset
         *            起始位置
         * @param len
         *            长度
         * @param preval
         *            之前的校验值
         * @return 校验值
         */
        public static byte calcCrc8(byte[] data, int offset, int len, byte preval) {
                byte ret = preval;
                for (int i = offset; i < (offset + len); ++i) {
                        ret = crc8_tab[(0x00ff & (ret ^ data))];
                }
                return ret;
        }

        public static byte crc8new(byte[] buffer, int offset, int len) {
                int crci = 0; // 起始字节FF
                int length = (offset + len);
                for (int j = offset; j < length; j++) {
                        crci ^= buffer[j] & 0xFF;
                        for (int i = 0; i < 8; i++) {
                                if ((crci & 0X80) != 0) {
                                        crci >>= 1;
                                        crci ^= 0xd5; // 多项式当中的那个啥的,不同多项式不一样
                                } else {
                                        crci >>= 1;
                                }
                        }
                }
                return (byte) crci;
        }

        // 测试
        public static void main(String[] args) {
                createCRCBuff();
                byte crc = CRC8.calcCrc(new byte[] { 1, 2, 3, 4, 8, 6, 7, 8, 9, 10 }, 0, 10);
                //System.out.println("" + Integer.toHexString(0x00ff & crc));
        }

        public static byte[] buff = new byte[256];
        static {
                createCRCBuff();
        }

        public static void createCRCBuff() {
                int i, j;
                byte crc;

                for (i = 0; i < 256; i++) {
                        crc = (byte) i;

                        for (j = 0; j < 8; j++) {
                                if ((crc & 0x01) != 0)
                                        crc = (byte) ((crc >> 1) ^ 0XD5);
                                else
                                        crc >>= 1;
                        }

                        buff = crc;
                        //System.out.println(buff);

                }
        }

        public static byte update(byte crc, byte c) {
                return buff[0X00ff & (crc ^ c)];
        }

        /**
         * 验证
         *
         * @param b
         * @param offset
         * @param len
         * @return
         */
        public static byte calcCrc(byte[] b, int offset, int len) {
                int length = offset + len;
                byte crc = 0;
                for (int j = offset; j < length; j++) {
                        crc = update(crc, b[j]);
                }
                return crc;
        }
}

使用特权

评论回复
7
keaiduoyu123|  楼主 | 2018-2-5 19:23 | 只看该作者
linqing171 发表于 2018-2-3 20:46
table没有贴上来,看不出是哪个多项式。
@hotpower

我把完整程序贴出来了,请指点

使用特权

评论回复
8
linqing171| | 2018-2-5 22:54 | 只看该作者
CRC8 implementation with polynom = x^8+x^7+x^6+x^4+x^2+1 (0xD5)

使用特权

评论回复
9
zqx1000| | 2018-4-10 11:36 | 只看该作者

用MODBUS就可以,是毕业设计吧?

使用特权

评论回复
10
hotpower| | 2018-11-20 14:20 | 只看该作者
www.hotpage.com.cn/hotcrc 可以逆向crc表格

使用特权

评论回复
11
hotpower| | 2018-12-29 23:51 | 只看该作者
采用四点攻击(逆向第三方CRC计算器或函数)即可推出算法!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

51

帖子

2

粉丝