Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1a40554f9 | ||
|
|
808af585c9 | ||
|
|
9ade9799f6 | ||
|
|
bb8f5fc0d4 | ||
|
|
eaac88c973 | ||
|
|
b1a6b69867 | ||
|
|
7460cd314c | ||
|
|
9f0f5d8a3d | ||
|
|
09fd969a58 |
No files matched your search
@@ -133,3 +133,4 @@ fabric.properties
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
.vscode/launch.json
|
||||
@@ -6,3 +6,4 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
crypto_box = "0.8.2"
|
||||
+55
-4
@@ -1,13 +1,26 @@
|
||||
use std::io::Write;
|
||||
use std::net::TcpStream;
|
||||
use crypto_box::{
|
||||
aead::{Aead, AeadCore, OsRng},
|
||||
SalsaBox, PublicKey, SecretKey
|
||||
};
|
||||
|
||||
fn main() {
|
||||
let port: u32 = 7878;
|
||||
//let stream = TcpStream::connect("172.30.16.1:8080");
|
||||
//let stream = TcpStream::connect("27.0.0.1:8080");
|
||||
match TcpStream::connect("localhost:7878") {
|
||||
match TcpStream::connect(format!("localhost:{}", port)) {
|
||||
Ok(mut stream) => {
|
||||
println!("Successfully connected to server");
|
||||
|
||||
let bob_init_pub_key = PublicKey::from([
|
||||
0xe8, 0x98, 0xc, 0x86, 0xe0, 0x32, 0xf1, 0xeb,
|
||||
0x29, 0x75, 0x5, 0x2e, 0x8d, 0x65, 0xbd, 0xdd,
|
||||
0x15, 0xc3, 0xb5, 0x96, 0x41, 0x17, 0x4e, 0xc9,
|
||||
0x67, 0x8a, 0x53, 0x78, 0x9d, 0x92, 0xc7, 0x54,
|
||||
]);
|
||||
let (salsa_box, pub_key) = generate_box(bob_init_pub_key.clone());
|
||||
|
||||
loop {
|
||||
let mut buffer = String::new();
|
||||
match std::io::stdin().read_line(&mut buffer) {
|
||||
@@ -15,9 +28,32 @@ fn main() {
|
||||
if n == 0 {
|
||||
break; // Beenden bei EOF
|
||||
}
|
||||
println!("Sending {}", buffer);
|
||||
//
|
||||
// Encryption
|
||||
//
|
||||
|
||||
// Get a random nonce to encrypt the message under
|
||||
let nonce = SalsaBox::generate_nonce(&mut OsRng);
|
||||
|
||||
// Message to encrypt
|
||||
let plaintext = buffer.trim().as_bytes();
|
||||
|
||||
// Encrypt the message using the box
|
||||
let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
|
||||
|
||||
println!("Sending {0} as {1:?}", buffer.trim(), plaintext);
|
||||
stream.write(buffer.as_bytes()).unwrap();
|
||||
println!("Sent {}, awaiting reply ...", buffer);
|
||||
|
||||
//
|
||||
// Decryption
|
||||
//
|
||||
|
||||
// Decrypt the message, using the same randomly generated nonce
|
||||
let decrypted_plaintext = salsa_box.decrypt(&nonce, &ciphertext[..]).expect("Fehler");
|
||||
let dec_plain_plaintext = std::str::from_utf8(&*decrypted_plaintext).expect("Nö");
|
||||
assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
|
||||
|
||||
println!("Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}", plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext);
|
||||
}
|
||||
Err(error) => {
|
||||
println!("error: {error}");
|
||||
@@ -31,4 +67,19 @@ fn main() {
|
||||
}
|
||||
}
|
||||
println!("Terminated.");
|
||||
} // the stream is closed here
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user