Cyclic Redundancy Code - Final (with Receiver Side ) #1

Closed
opened 2024-08-01 20:14:31 +05:30 by Kalaskar_admin03 · 1 comment

/*
#include
#include
using namespace std;

// Function to perform XOR operation
string XOR(string a, string b) {
string result = "";
for (int i = 0; i < a.length(); i++) {
result += (a[i] == b[i]) ? '0' : '1';
}
return result;
}

// Function to encode data using CRC
string encoder(string data, string key) {
int keyLen = key.length();
int dataLen = data.length();

// Append keyLen-1 zeroes to the data
string dataWithZeroes = data + string(keyLen - 1, '0');

// Initial segment to process
string temp = dataWithZeroes.substr(0, keyLen);

// Perform XOR operations on each segment
for (int i = keyLen; i < dataWithZeroes.length(); i++) {
    if (temp[0] == '1') {  // Only perform XOR if the leading bit is 1
        temp = XOR(temp, key);
    }
    temp = temp.substr(1) + dataWithZeroes[i];  // Shift left and add the next bit
}

// Perform the final XOR operation
if (temp[0] == '1') {
    temp = XOR(temp, key);
}

// Extract the remainder (last keyLen-1 bits)
string crc = temp.substr(temp.length() - (keyLen - 1));

return data + crc;

}

// Function to perform CRC division and find the remainder
string performDivision(string data, string key) {
int keyLen = key.length();

// Initialize with the data to be divided
string temp = data.substr(0, keyLen);

// Perform XOR operations on each segment
for (int i = keyLen; i < data.length(); i++) {
    if (temp[0] == '1') {  // Only perform XOR if the leading bit is 1
        temp = XOR(temp, key);
    }
    temp = temp.substr(1) + data[i];  // Shift left and add the next bit
}

// Perform the final XOR operation
if (temp[0] == '1') {
    temp = XOR(temp, key);
}

// Extract the remainder (last keyLen-1 bits)
return temp.substr(temp.length() - (keyLen - 1));

}

// Function to check the correctness of received data
bool checkData(string data, string key) {
string remainder = performDivision(data, key);
return (remainder.find('1') == string::npos); // No '1' means remainder is all zeros
}

int main() {
string data, key;

cout << "Enter original data:\t";
getline(cin, data);
cout << "Enter key:\t";
getline(cin, key);

string messageToSend = encoder(data, key);

cout << endl << "--------------------" << endl;
cout << "ORIGINAL DATA: " << data << endl;
cout << "KEY: " << key << endl;
cout << "DATA WITH ZEROS: " << data + string(key.length() - 1, '0') << endl;
cout << "REMAINDER: " << messageToSend.substr(data.length()) << endl;
cout << "DATA TO BE SENT: " << messageToSend << endl;
cout << "--------------------" << endl;

// Simulate receiving data
string receivedData;
cout << "Enter received data:\t";
getline(cin, receivedData);

if (receivedData == messageToSend) {
    cout << "The received data is correct." << endl;
} else {
    cout << "There is an error in the received data." << endl;
}

return 0;

}
*/

/* #include <iostream> #include <cstring> using namespace std; // Function to perform XOR operation string XOR(string a, string b) { string result = ""; for (int i = 0; i < a.length(); i++) { result += (a[i] == b[i]) ? '0' : '1'; } return result; } // Function to encode data using CRC string encoder(string data, string key) { int keyLen = key.length(); int dataLen = data.length(); // Append keyLen-1 zeroes to the data string dataWithZeroes = data + string(keyLen - 1, '0'); // Initial segment to process string temp = dataWithZeroes.substr(0, keyLen); // Perform XOR operations on each segment for (int i = keyLen; i < dataWithZeroes.length(); i++) { if (temp[0] == '1') { // Only perform XOR if the leading bit is 1 temp = XOR(temp, key); } temp = temp.substr(1) + dataWithZeroes[i]; // Shift left and add the next bit } // Perform the final XOR operation if (temp[0] == '1') { temp = XOR(temp, key); } // Extract the remainder (last keyLen-1 bits) string crc = temp.substr(temp.length() - (keyLen - 1)); return data + crc; } // Function to perform CRC division and find the remainder string performDivision(string data, string key) { int keyLen = key.length(); // Initialize with the data to be divided string temp = data.substr(0, keyLen); // Perform XOR operations on each segment for (int i = keyLen; i < data.length(); i++) { if (temp[0] == '1') { // Only perform XOR if the leading bit is 1 temp = XOR(temp, key); } temp = temp.substr(1) + data[i]; // Shift left and add the next bit } // Perform the final XOR operation if (temp[0] == '1') { temp = XOR(temp, key); } // Extract the remainder (last keyLen-1 bits) return temp.substr(temp.length() - (keyLen - 1)); } // Function to check the correctness of received data bool checkData(string data, string key) { string remainder = performDivision(data, key); return (remainder.find('1') == string::npos); // No '1' means remainder is all zeros } int main() { string data, key; cout << "Enter original data:\t"; getline(cin, data); cout << "Enter key:\t"; getline(cin, key); string messageToSend = encoder(data, key); cout << endl << "--------------------" << endl; cout << "ORIGINAL DATA: " << data << endl; cout << "KEY: " << key << endl; cout << "DATA WITH ZEROS: " << data + string(key.length() - 1, '0') << endl; cout << "REMAINDER: " << messageToSend.substr(data.length()) << endl; cout << "DATA TO BE SENT: " << messageToSend << endl; cout << "--------------------" << endl; // Simulate receiving data string receivedData; cout << "Enter received data:\t"; getline(cin, receivedData); if (receivedData == messageToSend) { cout << "The received data is correct." << endl; } else { cout << "There is an error in the received data." << endl; } return 0; } */
Owner

The XOR function has been kept as is from testing branch. The performDivision and checkData functions have been added, along with some modifications to the main function.

Check the code here.

The `XOR` function has been kept as is from testing branch. The `performDivision` and `checkData` functions have been added, along with some modifications to the `main` function. Check the code [here](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/commit/6e0b540c621d50cba79431211275d3fbe063984b/Codes/Code-A4.cpp).
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sppu-te-comp-content/ComputerNetworksAndSecurity#1
No description provided.