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.3KB

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