crypto-chat-rs
v1.0.3
Published
Encrypted terminal-based chat application built with Rust and Tokio
Maintainers
Readme
Crypto Chat RS
A secure, encrypted terminal-based chat application built with Rust and Tokio. Features real-time bidirectional communication with XOR cipher encryption over TCP.
Features
- Real-time Communication: Asynchronous TCP-based chat using Tokio
- Encryption: XOR cipher encryption for message privacy
- Bidirectional Messaging: Both server and client can send and receive messages simultaneously
- Terminal-based UI: Simple, clean command-line interface
- Easy to Use: Run with npx or directly with Cargo
Prerequisites
- Rust (1.70 or later): Install Rust
- Node.js (18.0 or later): For npx wrapper (optional)
Installation
Option 1: Using npx (Recommended)
No installation needed! Just run with npx:
# Server mode
npx crypto-chat-rs listen 127.0.0.1:8080
# Client mode
npx crypto-chat-rs connect 127.0.0.1:8080Option 2: Clone and Build
git clone <repository-url>
cd crypto-chat-rs
cargo build --releaseUsage
Starting a Server
The server listens for incoming connections on a specified address and port.
With npx:
npx crypto-chat-rs listen 127.0.0.1:8080With Cargo:
cargo run -- listen 127.0.0.1:8080Direct binary:
./target/release/crypto-chat-rs listen 127.0.0.1:8080Connecting as a Client
The client connects to a running server.
With npx:
npx crypto-chat-rs connect 127.0.0.1:8080With Cargo:
cargo run -- connect 127.0.0.1:8080Direct binary:
./target/release/crypto-chat-rs connect 127.0.0.1:8080Default Settings
If you omit the address:port, the default is 127.0.0.1:8080:
npx crypto-chat-rs listen # Listens on 127.0.0.1:8080
npx crypto-chat-rs connect # Connects to 127.0.0.1:8080How It Works
Architecture
Server Mode:
- Binds to a TCP socket and waits for a connection
- Accepts one client connection
- Spawns tasks for sending and receiving messages
Client Mode:
- Connects to a running server
- Spawns tasks for sending and receiving messages
Message Flow:
- User input is read from stdin
- Messages are encrypted using XOR cipher
- Length-prefixed protocol ensures complete message delivery
- Received messages are decrypted and displayed
Encryption
The application uses a simple XOR cipher for demonstration purposes:
fn xor_cipher(data: &[u8], key: &[u8]) -> Vec<u8> {
data.iter()
.enumerate()
.map(|(i, &byte)| byte ^ key[i % key.len()])
.collect()
}Note: XOR cipher is used for educational purposes. For production use, implement proper encryption (AES-GCM, ChaCha20-Poly1305, etc.).
Protocol
Messages are sent with a length-prefix protocol:
- 4 bytes (big-endian u32): Message length
- N bytes: Encrypted message data
Example Session
Terminal 1 (Server):
$ npx crypto-chat-rs listen 127.0.0.1:8080
Listening on 127.0.0.1:8080
Connected: 127.0.0.1:54321
> Hello from server!
< Hello from client!
> How are you?Terminal 2 (Client):
$ npx crypto-chat-rs connect 127.0.0.1:8080
Connecting to 127.0.0.1:8080
Connected!
< Hello from server!
> Hello from client!
< How are you?
> I'm good, thanks!Development
Project Structure
crypto-chat-rs/
├── src/
│ └── main.rs # Main application code
├── index.ts # npx wrapper
├── package.json # Node.js package configuration
├── Cargo.toml # Rust package configuration
└── README.md # This fileBuilding
# Debug build
cargo build
# Release build (optimized)
cargo build --release
# Using npm scripts
npm run buildRunning in Development
# Using Cargo
cargo run -- listen 127.0.0.1:8080
cargo run -- connect 127.0.0.1:8080
# Using npm scripts
npm run listen
npm run connectTechnical Details
Dependencies
- tokio: Async runtime with TCP and I/O utilities
- std::io: Standard I/O for user input
Async Architecture
- Uses Tokio's
#[tokio::main]for async main function tokio::spawnfor concurrent send/receive taskstokio::sync::mpscchannel for stdin to async bridgespawn_blockingfor blocking stdin operations
Key Functions
xor_cipher(): Encrypts/decrypts messages (src/main.rs:7)send_messages(): Handles user input and message sending (src/main.rs:14)receive_messages(): Handles incoming message reception (src/main.rs:61)server_mode(): Server initialization and connection handling (src/main.rs:88)client_mode(): Client connection and setup (src/main.rs:105)
Security Considerations
WARNING: This is a demonstration project. The XOR cipher provides minimal security:
- Not cryptographically secure: XOR with a static key is easily broken
- No authentication: No verification of message sender
- No forward secrecy: Compromised key compromises all messages
- No integrity protection: Messages can be modified undetected
For Production Use:
- Use proper encryption (TLS, AES-GCM, ChaCha20-Poly1305)
- Implement key exchange (Diffie-Hellman, ECDH)
- Add message authentication (HMAC, AEAD)
- Consider using existing protocols (Signal Protocol, Noise Protocol)
- Use libraries like
rustls,ring, orsodiumoxide
Troubleshooting
"Address already in use"
The port is occupied. Choose a different port:
npx crypto-chat-rs listen 127.0.0.1:8081"Connection refused"
Ensure the server is running before starting the client.
Build fails
Ensure Rust is installed and up to date:
rustup updateLicense
MIT
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues.
Roadmap
- [ ] Support multiple clients
- [ ] Implement proper encryption (TLS)
- [ ] Add user authentication
- [ ] Chat rooms/channels
- [ ] File transfer support
- [ ] Message history
- [ ] Configuration file support
Acknowledgments
Built with Rust and Tokio for learning purposes.
