Compare commits

...
8 Commits
3 changed files with 103 additions and 13 deletions

No files matched your search

+26 -13
View File
@@ -1,12 +1,12 @@
use std::io::Write;
use std::net::TcpStream;
use crypto_box::{
aead::{Aead, AeadCore, OsRng},
SalsaBox, PublicKey, SecretKey
PublicKey, SalsaBox, SecretKey,
};
use std::io::Write;
use std::net::TcpStream;
fn main() {
let port: u32 = 7878;
let port: u32 = 7878; // Port Server
//let stream = TcpStream::connect("172.30.16.1:8080");
//let stream = TcpStream::connect("27.0.0.1:8080");
match TcpStream::connect(format!("localhost:{}", port)) {
@@ -14,12 +14,21 @@ fn main() {
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,
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());
// 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(&bob_init_pub_key, &own_secret_key);
loop {
let mut buffer = String::new();
@@ -49,11 +58,16 @@ fn main() {
//
// 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ö");
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);
println!(
"Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}",
plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext
);
}
Err(error) => {
println!("error: {error}");
@@ -82,4 +96,3 @@ fn generate_box(partner_public_key: PublicKey) -> (SalsaBox, PublicKey) {
let salsa_box = SalsaBox::new(&partner_public_key, &own_secret_key);
(salsa_box, own_public_key)
}
+2
View File
@@ -0,0 +1,2 @@
pub const KEYBYTE: u8 = 0b1;
pub const MSGBYTE: u8 = 0b01;
+75
View File
@@ -0,0 +1,75 @@
const KEYLENGTH: u8 = 32;
pub enum PayloadType {
//0-31 system messages (plaintext)
Ping = 1,
Pong = 2,
Error = 3,
//32 - 63 Serverside messages (currently plaintext)
Init = 32,
Join = 33,
Exit = 34,
//64 -95 initial setup messages(plaintext)
DhSetup = 64,
DhReturn = 65,
//96 - 127 client-client setup messages(DH or old KEY encrypted)
AUTHORIZE = 96,
KeyCurrent = 97,
KeyNew = 98,
//128-159 main messages (shared KEY encrypted)
Msg = 128,
//160-255 reserved
}
struct Message {
// Eigenschaften der Klasse
src_id: u32,
dest_id: u32,
size: u16,
payload: Vec<u8>,
}
impl Message {
fn new(src_id: u32, dest_id: u32, size: u16, msg_type: PayloadType) -> Message {
let mut msg = Message {
src_id: src_id,
dest_id: dest_id,
size: size,
payload: Vec::with_capacity(size.into()),
};
msg.payload[0] = msg_type as u8;
msg
}
fn recive(input: Vec<u8>) -> Message {
let byte1 = input.get(0).expect("Fail to Read Byte 1");
let byte2 = input.get(1).expect("Fail to Read Byte 2");
let byte3 = input.get(2).expect("Fail to Read Byte 3");
let byte4 = input.get(3).expect("Fail to Read Byte 4");
let src_id: u32 =
(byte1 << 24) as u32 | (byte2 << 16) as u32 | (byte3 << 8) as u32 | (byte4 << 0) as u32;
let byte5 = input.get(5).expect("Fail to Read Byte 5");
let byte6 = input.get(6).expect("Fail to Read Byte 6");
let byte7 = input.get(7).expect("Fail to Read Byte 7");
let byte8 = input.get(8).expect("Fail to Read Byte 8");
let dest_id: u32 =
(byte5 << 24) as u32 | (byte6 << 16) as u32 | (byte7 << 8) as u32 | (byte8 << 0) as u32;
let byte9 = input.get(9).expect("Fail to Read byte 9");
let byte10 = input.get(10).expect("Fail to Read byte 10");
let size = (byte9 << 8) as u16 | (byte10 << 0) as u16;
assert!(size > 0, "Ungültige size größe size = {}", size);
Message {
src_id: src_id,
dest_id: dest_id,
size: size,
payload: (&input[11..]).to_vec(),
}
}
// Methoden der Klasse
}