You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.rs 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use std::io::Write;
  2. use std::net::TcpStream;
  3. use crypto_box::{
  4. aead::{Aead, AeadCore, OsRng},
  5. SalsaBox, PublicKey, SecretKey
  6. };
  7. fn main() {
  8. let port: u32 = 7878; // Port Server
  9. //let stream = TcpStream::connect("172.30.16.1:8080");
  10. //let stream = TcpStream::connect("27.0.0.1:8080");
  11. match TcpStream::connect(format!("localhost:{}", port)) {
  12. Ok(mut stream) => {
  13. println!("Successfully connected to server");
  14. // Nachrichten senden
  15. loop {
  16. let mut buffer = String::new();
  17. match std::io::stdin().read_line(&mut buffer) {
  18. Ok(n) => {
  19. if n == 0 {
  20. break; // Beenden bei EOF
  21. }
  22. //
  23. // Encryption
  24. //
  25. // Generate a random secret key.
  26. // NOTE: The secret key bytes can be accessed by calling `secret_key.as_bytes()`
  27. let alice_secret_key = SecretKey::generate(&mut OsRng);
  28. // Get the public key for the secret key we just generated
  29. let alice_public_key_bytes = alice_secret_key.public_key().as_bytes().clone();
  30. // Obtain your recipient's public key.
  31. let bob_public_key = PublicKey::from([
  32. 0xe8, 0x98, 0xc, 0x86, 0xe0, 0x32, 0xf1, 0xeb,
  33. 0x29, 0x75, 0x5, 0x2e, 0x8d, 0x65, 0xbd, 0xdd,
  34. 0x15, 0xc3, 0xb5, 0x96, 0x41, 0x17, 0x4e, 0xc9,
  35. 0x67, 0x8a, 0x53, 0x78, 0x9d, 0x92, 0xc7, 0x54,
  36. ]);
  37. // Create a `SalsaBox` by performing Diffie-Hellman key agreement between
  38. // the two keys.
  39. let alice_box = SalsaBox::new(&bob_public_key, &alice_secret_key);
  40. // Get a random nonce to encrypt the message under
  41. let nonce = SalsaBox::generate_nonce(&mut OsRng);
  42. // Message to encrypt
  43. let plaintext = buffer.trim().as_bytes();
  44. // Encrypt the message using the box
  45. let ciphertext = alice_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
  46. println!("Sending {0} as {1:?}", buffer.trim(), plaintext);
  47. stream.write(buffer.as_bytes()).unwrap();
  48. //
  49. // Decryption
  50. //
  51. // Either side can encrypt or decrypt messages under the Diffie-Hellman key
  52. // they agree upon. The example below shows Bob's side.
  53. let bob_secret_key = SecretKey::from([
  54. 0xb5, 0x81, 0xfb, 0x5a, 0xe1, 0x82, 0xa1, 0x6f,
  55. 0x60, 0x3f, 0x39, 0x27, 0xd, 0x4e, 0x3b, 0x95,
  56. 0xbc, 0x0, 0x83, 0x10, 0xb7, 0x27, 0xa1, 0x1d,
  57. 0xd4, 0xe7, 0x84, 0xa0, 0x4, 0x4d, 0x46, 0x1b
  58. ]);
  59. // Deserialize Alice's public key from bytes
  60. let alice_public_key = PublicKey::from(alice_public_key_bytes);
  61. // Bob can compute the same `SalsaBox` as Alice by performing the
  62. // key agreement operation.
  63. let bob_box = SalsaBox::new(&alice_public_key, &bob_secret_key);
  64. // Decrypt the message, using the same randomly generated nonce
  65. let decrypted_plaintext = bob_box.decrypt(&nonce, &ciphertext[..]).expect("Fehler");
  66. let dec_plain_plaintext = std::str::from_utf8(&*decrypted_plaintext).expect("Nö");
  67. assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
  68. println!("Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}", plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext);
  69. }
  70. Err(error) => {
  71. println!("error: {error}");
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. Err(e) => {
  78. println!("Failed to connect: {}", e);
  79. }
  80. }
  81. println!("Terminated.");
  82. } // the stream is closed here