Merge nach Praktikum
This commit is contained in:
commit
197c72daf5
@ -1,9 +1,9 @@
|
|||||||
use std::io::Write;
|
|
||||||
use std::net::TcpStream;
|
|
||||||
use crypto_box::{
|
use crypto_box::{
|
||||||
aead::{Aead, AeadCore, OsRng},
|
aead::{Aead, AeadCore, OsRng},
|
||||||
SalsaBox, PublicKey, SecretKey
|
PublicKey, SalsaBox, SecretKey,
|
||||||
};
|
};
|
||||||
|
use std::io::Write;
|
||||||
|
use std::net::TcpStream;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let port: u32 = 7878; // Port Server
|
let port: u32 = 7878; // Port Server
|
||||||
@ -13,7 +13,13 @@ fn main() {
|
|||||||
Ok(mut stream) => {
|
Ok(mut stream) => {
|
||||||
println!("Successfully connected to server");
|
println!("Successfully connected to server");
|
||||||
|
|
||||||
// Nachrichten senden
|
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 {
|
loop {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
match std::io::stdin().read_line(&mut buffer) {
|
match std::io::stdin().read_line(&mut buffer) {
|
||||||
@ -25,25 +31,6 @@ fn main() {
|
|||||||
// Encryption
|
// Encryption
|
||||||
//
|
//
|
||||||
|
|
||||||
// Generate a random secret key.
|
|
||||||
// NOTE: The secret key bytes can be accessed by calling `secret_key.as_bytes()`
|
|
||||||
let alice_secret_key = SecretKey::generate(&mut OsRng);
|
|
||||||
|
|
||||||
// Get the public key for the secret key we just generated
|
|
||||||
let alice_public_key_bytes = alice_secret_key.public_key().as_bytes().clone();
|
|
||||||
|
|
||||||
// Obtain your recipient's public key.
|
|
||||||
let bob_public_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,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Create a `SalsaBox` by performing Diffie-Hellman key agreement between
|
|
||||||
// the two keys.
|
|
||||||
let alice_box = SalsaBox::new(&bob_public_key, &alice_secret_key);
|
|
||||||
|
|
||||||
// Get a random nonce to encrypt the message under
|
// Get a random nonce to encrypt the message under
|
||||||
let nonce = SalsaBox::generate_nonce(&mut OsRng);
|
let nonce = SalsaBox::generate_nonce(&mut OsRng);
|
||||||
|
|
||||||
@ -51,7 +38,7 @@ fn main() {
|
|||||||
let plaintext = buffer.trim().as_bytes();
|
let plaintext = buffer.trim().as_bytes();
|
||||||
|
|
||||||
// Encrypt the message using the box
|
// Encrypt the message using the box
|
||||||
let ciphertext = alice_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
|
let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
|
||||||
|
|
||||||
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();
|
||||||
@ -60,28 +47,17 @@ fn main() {
|
|||||||
// Decryption
|
// Decryption
|
||||||
//
|
//
|
||||||
|
|
||||||
// Either side can encrypt or decrypt messages under the Diffie-Hellman key
|
|
||||||
// they agree upon. The example below shows Bob's side.
|
|
||||||
let bob_secret_key = SecretKey::from([
|
|
||||||
0xb5, 0x81, 0xfb, 0x5a, 0xe1, 0x82, 0xa1, 0x6f,
|
|
||||||
0x60, 0x3f, 0x39, 0x27, 0xd, 0x4e, 0x3b, 0x95,
|
|
||||||
0xbc, 0x0, 0x83, 0x10, 0xb7, 0x27, 0xa1, 0x1d,
|
|
||||||
0xd4, 0xe7, 0x84, 0xa0, 0x4, 0x4d, 0x46, 0x1b
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Deserialize Alice's public key from bytes
|
|
||||||
let alice_public_key = PublicKey::from(alice_public_key_bytes);
|
|
||||||
|
|
||||||
// Bob can compute the same `SalsaBox` as Alice by performing the
|
|
||||||
// key agreement operation.
|
|
||||||
let bob_box = SalsaBox::new(&alice_public_key, &bob_secret_key);
|
|
||||||
|
|
||||||
// Decrypt the message, using the same randomly generated nonce
|
// Decrypt the message, using the same randomly generated nonce
|
||||||
let decrypted_plaintext = bob_box.decrypt(&nonce, &ciphertext[..]).expect("Fehler");
|
let decrypted_plaintext =
|
||||||
let dec_plain_plaintext = std::str::from_utf8(&*decrypted_plaintext).expect("Nö");
|
salsa_box.decrypt(&nonce, &ciphertext[..]).expect("Fehler");
|
||||||
|
let dec_plain_plaintext =
|
||||||
|
std::str::from_utf8(&*decrypted_plaintext).expect("Nö");
|
||||||
assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
|
assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
|
||||||
|
|
||||||
println!("Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}", plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext);
|
println!(
|
||||||
|
"Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}",
|
||||||
|
plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
println!("error: {error}");
|
println!("error: {error}");
|
||||||
@ -95,4 +71,18 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("Terminated.");
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user