-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.py
54 lines (37 loc) · 1007 Bytes
/
crc.py
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
def xor(a,b):
result = []
#traverse through all bits and do XOR
for i in range(1, len(b)):
if a[i]==b[i]:
result.append('0')
else:
result.append('1')
return ''.join(result)
#Now do division
def modDiv(divident , divisior):
pick = len(divisior) #pick the no of bits to be XOR
tmp = divident[0 : pick]
while pick<len(divident):
if tmp[0] == '1':
tmp = xor(divisior , tmp) + divident[pick]
else:
tmp = xor('0'*pick, tmp) + divident[pick]
pick+=1
if tmp[0] == '1':
tmp = xor(divisior, tmp)
else:
tmp = xor('0'*pick, tmp)
checkword = tmp
return checkword
def encodeData(data,key):
l_key = len(key)
appended_data= data + '0'*(l_key-1)
remainder = modDiv(appended_data, key)
checkword = data + remainder
print("Remainder :",remainder)
print("Encoded data :" ,checkword)
data= input("Enter your data:")
key= input("Enter your key:")
print("Data:",data)
print("Key:" , key)
encodeData(data,key)