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

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