python实现循环冗余校验

from tkinter import *
import random


def XOR(str1, str2):
    """异或操作"""
    result = ""
    # 输入数据处理
    if len(str1) > len(str2):  # 若后者的位数不足时
        for i in range(len(str1) - len(str2)):  # 对string2的首位补0
            str2 = "0" + str2
    if len(str1) < len(str2):  # 若前者的位数不足时
        for i in range(len(str2) - len(str1)):  # 对string1的首位补0
            str1 = "0" + str1

    # 异或计算
    for i in range(len(str1)):
        if str1[i] != str2[i]:
            result += "1"
        else:
            result += "0"
    ##    print(result)
    # 输出数据处理
    for j in range(len(result)):  # 删除无效0
        if result[j] == "1":  # 找到第一个有效的值
            return result[j::]
    return result


def Change(polynomial):
    """多项式转换为二进制"""
    # 输入数据处理
    polynomial = polynomial.upper()  # 大写转换
    li = list(polynomial.split("+"))  # 以加号为标志进行分割
    binary = li[-1]  # 多项式末尾的数字1
    del li[-1]
    li = li[::-1]
    # 二进制转换
    for i in range(len(li)):
        if len(binary) == int(li[i][1]):  # 满足位数与变量的数字相同时
            binary += "1"
        else:
            for j in range(int(li[i][1]) - len(binary)):  # 用0补全相差的位数
                binary += "0"
            binary += "1"
    result = binary[::-1]
    return result


def CRC(string, polynomial):
    """产生CRC校验码"""
    crc = ""
    # 输入数据处理
    bin_polynomial = Change(polynomial)  # 转换为二进制串
    for i in range(len(bin_polynomial) - 1):  # 在二进制串末尾补0
        string += "0"

    # CRC
    for i in range(len(string)):  # 遍历被除数二进制串
        crc += string[i]
        if len(crc) == len(bin_polynomial):  # 当位数足够异或计算时
            crc = XOR(crc, bin_polynomial)
    ##    print(crc)
    # 输出数据处理
    if len(crc) < len(bin_polynomial) - 1:  # 在首部补0
        for i in range(len(bin_polynomial) - len(crc)):
            crc = "0" + crc
    if len(crc) > len(bin_polynomial) - 1:  # 当crc位数过多时
        crc = crc[len(crc) - len(bin_polynomial) + 1::]  # 消除首部多余的0
    return crc


def Check(string, polynomial):  # 检测传输数据是否正确,并打印结果
    # 输入数据处理
    bin_polynomial = Change(polynomial)  # 转换为二进制串
    ori_crc = string[len(string) - len(bin_polynomial) + 1::]  # 原数据末尾的crc校验码
    # CRC计算
    crc = CRC(string[:len(string) - len(bin_polynomial) + 1:], polynomial)
    # 检验
    if ori_crc == crc:  # 当尾部的CRC校验码,与首部数据生成的CRC校验码一致时
        return "数据传输过程正常.."
    else:
        return "数据传输过程出错.."
    # print("检测出的校验码:", crc)
    # print("尾部校验码:", right_crc)


def start(el, el2, res, resi, res2):
    polynomial = el.get().strip()  # 多项式
    string = el2.get().strip()  # 二进制串
    crc = CRC(string, polynomial)
    i = random.randint(0, len(string) - 1)
    res_crc = string + crc
    if res_crc[i] == '1':
        res_crc[i] = '0'
    else:
        res_crc[i] = '1'

    resultstring = Check(string, polynomial)
    res.set(crc)
    resi.set(i + 1)
    res2.set(resultstring)


def start2(el3, el4, res3):
    polynomial = el3.get().strip()  # 多项式
    string = el4.get().strip()  # 二进制串
    result = Check(string, polynomial)
    res3.set(result)


if __name__ == '__main__':
    # 主窗口
    root = Tk()
    root.title("CRC算法")
    root.geometry('450x600')
    Label(root, text='1.产生CRC校验码,并检测突变结果(多项式格式如:x3+1)').place(x=30, y=0)
    Label(root, text='生成多项式:').place(x=30, y=30)
    Label(root, text='二进制串:').place(x=30, y=70)
    Label(root, text='产生的校验码:').place(x=30, y=170)
    Label(root, text='随机突变位置:').place(x=30, y=210)
    Label(root, text='检测结果:').place(x=30, y=250)
    Label(root, text='2.检测传输数据').place(x=30, y=300)
    Label(root, text='生成多项式:').place(x=30, y=340)
    Label(root, text='二进制串:').place(x=30, y=380)
    Label(root, text='检测结果:').place(x=30, y=460)

    # 设置焦点  第一部分
    el = Entry(root)
    el.focus()
    el.place(x=100, y=30, width=250, height=30)
    # 设置焦点
    el2 = Entry(root)
    el2.focus()
    el2.place(x=100, y=70, width=250, height=30)

    res = StringVar()
    e2 = Entry(root, textvariable=res)
    e2.place(x=130, y=170, width=250, height=30)

    resi = StringVar()
    e2 = Entry(root, textvariable=resi)
    e2.place(x=130, y=210, width=250, height=30)

    res2 = StringVar()
    e2 = Entry(root, textvariable=res2)
    e2.place(x=130, y=250, width=250, height=30)

    Button(root, text='产生CRC校验码', width=20, command=lambda: start(el, el2, res, resi, res2)).place(x=100, y=110)

    # 设置焦点  第二部分
    el3 = Entry(root)
    el3.focus()
    el3.place(x=100, y=340, width=250, height=30)
    # 设置焦点
    el4 = Entry(root)
    el4.focus()
    el4.place(x=100, y=380, width=250, height=30)

    res3 = StringVar()
    e2 = Entry(root, textvariable=res3)
    e2.place(x=100, y=460, width=250, height=30)
    Button(root, text='检测传输数据', width=20, command=lambda: start2(el3, el4, res3)).place(x=100, y=420)

    Button(root, text='退出', width=10, command=root.quit).place(x=200, y=540)

    mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值