-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCRC_ErrorDetectionAlgorithm.java
94 lines (75 loc) · 3.89 KB
/
CRC_ErrorDetectionAlgorithm.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Contributed by - Anuj Das ( GC University, Silchar - @ Department of Computer Science )
// 1. Simulate and implement Cyclic Redundancy Check (CRC) Error Detection Algorithm for Noisy Channel.
import java.util.Scanner;
class CRC_ErrorDetectionAlgorithm {
// It returns the Remainder (i.e., CRC Check Bits)
public static String xorOperation(String modified_message, String polynomial) {
int size_in_bits = 16;
int shift = modified_message.length() - polynomial.length();
while(shift >= 0) {
modified_message = Integer.toBinaryString(Integer.parseInt(modified_message,2)^Integer.parseInt(polynomial,2)<<shift);
shift = modified_message.length() - polynomial.length();
}
if(modified_message.length() < size_in_bits) {
while(modified_message.length() != size_in_bits) {
modified_message = "0" + modified_message;
}
}
// System.out.println(modified_message);
return modified_message;
}
// It generates the encoded data/message to be transmitted (i.e., by adding CRC Check Bits to the Original Message)
public static String generateCRC_CheckBits(String modified_message, String polynomial) {
String remainder = xorOperation(modified_message,polynomial); // 16bits remainder
remainder = remainder.substring(remainder.length() - modified_message.length()); // substring(int startIndex)
int CRC_CheckBits[] = new int[modified_message.length()];
for(int i = 0; i < modified_message.length(); i++) {
CRC_CheckBits[i] = Character.getNumericValue(modified_message.charAt(i)) + Character.getNumericValue(remainder.charAt(i));
}
String msgToBeTransmitted = "";
for(int i = 0; i < CRC_CheckBits.length; i++) {
msgToBeTransmitted = msgToBeTransmitted.concat(Integer.toString(CRC_CheckBits[i]));
}
// System.out.print(msgToBeTransmitted);
return msgToBeTransmitted;
}
// If CRC generator is of 'n' bits then it appends 'n-1' zeroes at the end of the original data/message and returns the modified data/message
public static String modifyMessage(String message, String polynomial) {
for(int i = 0; i < polynomial.length() - 1; i++) {
message += "0";
}
return message;
}
// Passing Through Noisy Channel
public static String noisyChannel(String msgToBeTransmitted) {
StringBuilder noise = new StringBuilder(msgToBeTransmitted);
noise.setCharAt(msgToBeTransmitted.length()/2 , '1');
String noisyData = noise.toString();
return noisyData;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Data(Message) to be Encrypted: "); // Original Message
String message = sc.next(); // Test --> 1010000 , 100100111
System.out.print("Enter the CRC Generator(Polynomial): "); // CRC Generator
String polynomial = sc.next(); // Test --> 1001
// Sender's end
String modified_message = modifyMessage(message,polynomial);
System.out.println("Modified Data(Message): " + modified_message);
String msgToBeTransmitted = generateCRC_CheckBits(modified_message, polynomial);
System.out.println("\nData(Message) is ready to be Transmitted: " + msgToBeTransmitted);
// Noisy Transmission Channel
String send_data = noisyChannel(msgToBeTransmitted);
System.out.println("Data(Message) Transmitted through Noisy Channel: "+send_data);
// Receiver's end
String checkAllZeroes = xorOperation(send_data,polynomial);
// It checks if the remainder contains only zeroes --> If it contains only zeros then the data/message is accepted else considered as error in Transmission
for(int i = 0; i < checkAllZeroes.length(); i++) {
if(checkAllZeroes.charAt(i) == '1') {
System.out.println("\nError in Transmission!\n");
return;
}
}
System.out.println("\nData(Message) Transmitted Successfully!\n");
}
}