|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
use std::io::Write; |
|
|
use std::io::Write; |
|
|
use std::net::TcpStream; |
|
|
use std::net::TcpStream; |
|
|
|
|
|
use crate::message::*; |
|
|
|
|
|
|
|
|
|
|
|
mod message; |
|
|
|
|
|
|
|
|
fn main() { |
|
|
fn main() { |
|
|
let port: u32 = 7878; // Port Server |
|
|
|
|
|
//let stream = TcpStream::connect("172.30.16.1:8080"); |
|
|
|
|
|
//let stream = TcpStream::connect("27.0.0.1:8080"); |
|
|
|
|
|
|
|
|
// Server Port |
|
|
|
|
|
let port: u32 = 7878; |
|
|
|
|
|
|
|
|
match TcpStream::connect(format!("localhost:{}", port)) { |
|
|
match TcpStream::connect(format!("localhost:{}", port)) { |
|
|
Ok(mut stream) => { |
|
|
Ok(mut stream) => { |
|
|
println!("Successfully connected to server"); |
|
|
println!("Successfully connected to server"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Encrypt the message using the box |
|
|
// Encrypt the message using the box |
|
|
let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler"); |
|
|
let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler"); |
|
|
|
|
|
let message = Message::new(0,0,4, PayloadType::Msg); |
|
|
|
|
|
|
|
|
println!("Sending {0} as {1:?}", buffer.trim(), plaintext); |
|
|
println!("Sending {0} as {1:?}", buffer.trim(), plaintext); |
|
|
stream.write(buffer.as_bytes()).unwrap(); |
|
|
stream.write(buffer.as_bytes()).unwrap(); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
println!("Terminated."); |
|
|
println!("Terminated."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fn generate_box(partner_public_key: PublicKey) -> (SalsaBox, PublicKey) { |
|
|
|
|
|
// Generate a random secret key. |
|
|
|
|
|
// NOTE: The secret key bytes can be accessed by calling `secret_key.as_bytes()` |
|
|
|
|
|
let own_secret_key = SecretKey::generate(&mut OsRng); |
|
|
|
|
|
|
|
|
|
|
|
// Get the public key for the secret key we just generated |
|
|
|
|
|
let own_public_key = own_secret_key.public_key().clone(); |
|
|
|
|
|
|
|
|
|
|
|
// Create a `SalsaBox` by performing Diffie-Hellman key agreement between |
|
|
|
|
|
// the two keys. |
|
|
|
|
|
let salsa_box = SalsaBox::new(&partner_public_key, &own_secret_key); |
|
|
|
|
|
(salsa_box, own_public_key) |
|
|
|
|
|
} |
|
|
|