-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc.py
More file actions
69 lines (53 loc) · 1.3 KB
/
crc.py
File metadata and controls
69 lines (53 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class CRC:
def __init__(self):
self.cdw = ''
def xor(self,a,b):
result = []
for i in range(1,len(b)):
if a[i] == b[i]:
result.append('0')
else:
result.append('1')
return ''.join(result)
def crc(self,message, key):
pick = len(key)
tmp = message[:pick]
while pick < len(message):
if tmp[0] == '1':
tmp = self.xor(key,tmp)+message[pick]
else:
tmp = self.xor('0'*pick,tmp) + message[pick]
pick+=1
if tmp[0] == "1":
tmp = self.xor(key,tmp)
else:
tmp = self.xor('0'*pick,tmp)
checkword = tmp
return checkword
def encodedData(self,data,key):
l_key = len(key)
append_data = data + '0'*(l_key-1)
remainder = self.crc(append_data,key)
codeword = data+remainder
self.cdw += codeword
print("Remainder: " ,remainder)
print("Data: " ,codeword)
def reciverSide(self,key,data):
r = self.crc(data,key)
size = len(key)
print(r)
print("------------------- CRC -------------------")
c = CRC()
print(" ")
data = str(input("data for CRC: "))
key = str(input("key for CRC: "))
print(" ")
print('------------ At sender Side ------------ ')
c.encodedData(data,key)
print(" ")
print("------------ At reciver Side ------------")
print("checkword of codeword and key:", end= " ")
c.reciverSide(c.cdw, key)
print(" ")
print("Code Word:", end= " ")
print(c.cdw)