Cyclic Redundancy Check Generates Different Values for Message

-1

For the 3 codes, I need to achieve the following:

  • A sends a message to B along with the CRC32 code.
  • B receives this message and CRC32 code.
  • B follows a 40% probability to change the message.
  • B sends the message along with the original CRC32 code to C.
  • C receives the message and CRC32 code and check whether it is correct or not.

The code is nearly complete, but the CRC's are completely different, and they never equal one another. When comparing the CRC from message one to the second CRC, they are never equal.

Part A:

import socket
import struct
import sys
import binascii

def crc32(v):
     r = binascii.crc32(v.encode())
     return r


if len(sys.argv) != 3:
    print("Useage: python " + sys.argv[0] + " <ip> <liseten port>")
    sys.exit(-1)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
    print("Input text:")
    text = sys.stdin.readline().strip()
    ss = struct.pack("!50sL",text.encode(),crc32(text))
    s.sendto(ss,(sys.argv[1],int(sys.argv[2])))
    if text == "bye":
        break

Part B:

import socket
import operator
import sys
import binascii
import struct
import random

def crc32(v):
    return binascii.crc32(v.encode())

if len(sys.argv) != 3:
    print("Useage: python " + sys.argv[0] + " <liseten port>")
    sys.exit(-1)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("0.0.0.0", int(sys.argv[1])))
print("Waiting...")
while True:
    data, addr = s.recvfrom(1024)
    str,crc = struct.unpack("!50sL",data)
    str = str.decode("utf-8").replace("\0","")
    if random.randint(0,100) < 40:
        str = str + "x"
    print("str:%s\ncrc:%X" % (str,crc & 0xffffffff))
    str2 = str.encode("utf-8")
    tpack = struct.pack("!50sL", str2, crc)
    s.sendto(tpack,("127.0.0.1",int(sys.argv[2])))

    if str == "bye":
        break

Part C:

import socket
import operator
import sys
import binascii
import struct

def crc32(v):
    return binascii.crc32(v.encode())

if len(sys.argv) != 2:
    print("Useage: python " + sys.argv[0] + " <liseten port>")
    sys.exit(-1)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("0.0.0.0", int(sys.argv[1])))
print("Waiting...")
while True:
    data, addr = s.recvfrom(1024)
    str,crc = struct.unpack("!50sL",data)
    str = str.decode("utf-8")
    print("str:%s\ncrc:%X" % (str,crc & 0xffffffff))

    ncrc = crc32(str)
    if crc == ncrc:
        print ("CRC's are equal.")
    if str == "bye":
        break
python
sockets
udp
crc
udpclient
asked on Stack Overflow Mar 8, 2019 by godspeedd • edited Mar 8, 2019 by godspeedd

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0