C++ Secure Chat System
A secure chat system in C++ consists of two programs that work together: one for encrypting and sending messages and the other for decrypting and displaying them.
Main Features
The system includes:
Program A (Sender)
- Encrypts messages using an algorithm like AES or XOR.
- Saves encrypted messages to a file.
Program B (Receiver)
- Reads encrypted messages from the file.
- Decrypts them using the same encryption method.
Program A: Sender
This program encrypts a message and saves it to a file.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Simple XOR encryption
string encryptMessage(const string& message, char key) {
string encrypted = message;
for (char& c : encrypted) {
c ^= key;
}
return encrypted;
}
int main() {
string message;
char key = 'X'; // Secret key
cout << "Enter message to encrypt: ";
getline(cin, message);
string encryptedMessage = encryptMessage(message, key);
// Save to file
ofstream outFile("secure_message.txt");
if (outFile) {
outFile << encryptedMessage;
outFile.close();
cout << "Message encrypted and saved.\n";
} else {
cout << "Error saving message!\n";
}
return 0;
}
Program B: Receiver
This program reads an encrypted message from the file and decrypts it.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Simple XOR decryption (same as encryption)
string decryptMessage(const string& encryptedMessage, char key) {
string decrypted = encryptedMessage;
for (char& c : decrypted) {
c ^= key;
}
return decrypted;
}
int main() {
string encryptedMessage;
char key = 'X'; // Same secret key
// Read from file
ifstream inFile("secure_message.txt");
if (inFile) {
getline(inFile, encryptedMessage);
inFile.close();
string decryptedMessage = decryptMessage(encryptedMessage, key);
cout << "Decrypted message: " << decryptedMessage << endl;
} else {
cout << "Error reading message!\n";
}
return 0;
}
How It Works Together
-
Run Program A (Sender):
- Enter a message.
- The message is encrypted and saved to a file.
-
Run Program B (Receiver):
- Reads the encrypted message from the file.
- Decrypts it using the same key.
- Displays the original message.
This simple C++ secure chat system demonstrates basic encryption and decryption using XOR. For stronger security, AES or other cryptographic algorithms should be considered.
Comments
Post a Comment