2024-07-29 19:27:12 +05:30
|
|
|
#include <iostream>
|
|
|
|
#include <cstring>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
string XOR(string data, string key) {
|
|
|
|
// Dividend is data
|
|
|
|
// Divisor is the primary key, i.e. the key
|
|
|
|
string result = "";
|
2024-07-29 19:57:00 +05:30
|
|
|
int dataLen = data.length();
|
|
|
|
int keyLen = key.length();
|
|
|
|
|
|
|
|
// Perform XOR operation
|
|
|
|
for (int i=0; i<keyLen; i++) {
|
|
|
|
if (i < dataLen) {
|
|
|
|
// Only perform XOR if within the length of data
|
|
|
|
if (data[i] == key[i]) {
|
|
|
|
result += '0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result += '1';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If data length exceeded, append the key
|
|
|
|
result += key[i];
|
2024-07-29 19:27:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-29 19:57:00 +05:30
|
|
|
string encoder(string data, string key) {
|
|
|
|
int keyLen = key.length();
|
2024-07-29 19:27:12 +05:30
|
|
|
|
2024-07-29 19:57:00 +05:30
|
|
|
// Append n-1 zeroes to the data
|
|
|
|
string dataWithZeroes = data + string(keyLen-1, '0');
|
2024-07-29 19:27:12 +05:30
|
|
|
|
2024-07-29 19:57:00 +05:30
|
|
|
// Perform XOR operation with the key
|
|
|
|
string remainder = XOR(dataWithZeroes, key);
|
2024-07-29 19:27:12 +05:30
|
|
|
|
2024-07-29 19:57:00 +05:30
|
|
|
// Get the remainder (last n-1 bits)
|
|
|
|
string crc = remainder.substr(remainder.length() - (keyLen-1));
|
2024-07-29 19:27:12 +05:30
|
|
|
|
2024-07-29 19:57:00 +05:30
|
|
|
// Append the CRC to the original data
|
|
|
|
return data + crc;
|
2024-07-29 19:27:12 +05:30
|
|
|
}
|
2024-07-29 19:57:00 +05:30
|
|
|
|
2024-08-11 21:55:12 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-29 19:57:00 +05:30
|
|
|
int main() {
|
|
|
|
string data, key;
|
|
|
|
|
|
|
|
cout << endl << "Enter data:\t";
|
|
|
|
getline(cin, data);
|
|
|
|
cout << "Enter primary key:\t";
|
|
|
|
getline(cin, key);
|
|
|
|
|
2024-08-11 21:55:12 +05:30
|
|
|
cout<<endl<<"Original data:\t"<<data;
|
|
|
|
cout<<endl<<"Key:\t"<<key;
|
2024-07-29 19:57:00 +05:30
|
|
|
string messageToSend = encoder(data, key);
|
2024-08-11 21:55:12 +05:30
|
|
|
cout<<endl<<"----------------------------------------"<<endl;
|
2024-07-29 19:57:00 +05:30
|
|
|
cout<<"Message to be sent:\t"<<messageToSend;
|
2024-08-11 21:55:12 +05:30
|
|
|
cout<<endl<<"----------------------------------------"<<endl;
|
|
|
|
|
|
|
|
string receivedData;
|
|
|
|
cout<<endl<<"HINT: Received data should be the same as message to be sent.";
|
|
|
|
cout<<endl<<"Enter received data:\t";
|
|
|
|
getline(cin, receivedData);
|
|
|
|
|
|
|
|
if (receivedData == messageToSend) {
|
|
|
|
cout<<"The received data is correct."<<endl;
|
|
|
|
} else {
|
|
|
|
cout<<"The received data appears to be tampered."<<endl;
|
|
|
|
}
|
2024-07-29 19:57:00 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|