Compare commits
No commits in common. "fc0dba35524b1e0f68218e6185b7516438bd4274" and "b94dba6141c2cf7d801655443f7b481fa8f55456" have entirely different histories.
fc0dba3552
...
b94dba6141
@ -43,35 +43,6 @@ string encoder(string data, string key) {
|
||||
return data + crc;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@ -80,23 +51,13 @@ int main() {
|
||||
cout << "Enter primary key:\t";
|
||||
getline(cin, key);
|
||||
|
||||
cout<<endl<<"Original data:\t"<<data;
|
||||
cout<<endl<<"Key:\t"<<key;
|
||||
string messageToSend = encoder(data, key);
|
||||
cout<<endl<<"----------------------------------------"<<endl;
|
||||
cout<<endl<<"--------------------"<<endl;
|
||||
cout<<"Message to be sent:\t"<<messageToSend;
|
||||
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;
|
||||
}
|
||||
cout<<endl<<"--------------------"<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// - Check if input data is actually binary
|
||||
|
@ -1,79 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SlidingWindow {
|
||||
private:
|
||||
int window_size;
|
||||
int total_frames;
|
||||
vector<int> sent_frames; // queue of sent frames
|
||||
int next_frame_to_send; // index of the next frame to send
|
||||
int next_frame_to_receive; // index of the next frame to receive
|
||||
|
||||
public:
|
||||
SlidingWindow(int window_size, int total_frames) {
|
||||
this->window_size = window_size;
|
||||
this->total_frames = total_frames;
|
||||
this->next_frame_to_send = 0;
|
||||
this->next_frame_to_receive = 0;
|
||||
}
|
||||
|
||||
// send a frame
|
||||
void send_frame() {
|
||||
if (sent_frames.size() < window_size && next_frame_to_send < total_frames) {
|
||||
sent_frames.push_back(next_frame_to_send);
|
||||
next_frame_to_send++;
|
||||
cout << "Sent frame " << sent_frames.back() << endl;
|
||||
} else {
|
||||
cout << "Window is full. Waiting before sending next frames." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// receive a frame
|
||||
void receive_frame(int frame_number) {
|
||||
if (frame_number == next_frame_to_receive) {
|
||||
next_frame_to_receive++;
|
||||
cout << "Received frame " << frame_number << endl;
|
||||
} else {
|
||||
cout << "Received frame " << frame_number << ", but expected frame " << next_frame_to_receive << endl;
|
||||
}
|
||||
|
||||
// remove acknowledged frames from the sent_frames queue
|
||||
while (!sent_frames.empty() && sent_frames.front() <= next_frame_to_receive - 1) {
|
||||
sent_frames.erase(sent_frames.begin());
|
||||
}
|
||||
}
|
||||
|
||||
bool all_frames_sent() {
|
||||
return next_frame_to_send == total_frames;
|
||||
}
|
||||
|
||||
bool all_frames_received() {
|
||||
return next_frame_to_receive == total_frames;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
int window_size, total_frames;
|
||||
cout << "Enter the window size:\t";
|
||||
cin >> window_size;
|
||||
cout << "Enter the total number of frames:\t";
|
||||
cin >> total_frames;
|
||||
|
||||
SlidingWindow window(window_size, total_frames);
|
||||
|
||||
while (!window.all_frames_sent() || !window.all_frames_received()) {
|
||||
window.send_frame();
|
||||
|
||||
// simulate receiving frames
|
||||
for (int i = 0; i < rand() % 3; i++) {
|
||||
int frame_number = rand() % total_frames;
|
||||
window.receive_frame(frame_number);
|
||||
}
|
||||
}
|
||||
cout << "All frames sent and received successfully!" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user