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 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use crypto_box::{
  2. aead::{Aead, AeadCore, OsRng},
  3. PublicKey, SalsaBox, SecretKey,
  4. };
  5. use std::io::Write;
  6. use std::net::TcpStream;
  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. let bob_init_pub_key = PublicKey::from([
  15. 0xe8, 0x98, 0xc, 0x86, 0xe0, 0x32, 0xf1, 0xeb, 0x29, 0x75, 0x5, 0x2e, 0x8d, 0x65,
  16. 0xbd, 0xdd, 0x15, 0xc3, 0xb5, 0x96, 0x41, 0x17, 0x4e, 0xc9, 0x67, 0x8a, 0x53, 0x78,
  17. 0x9d, 0x92, 0xc7, 0x54,
  18. ]);
  19. let (salsa_box, pub_key) = generate_box(bob_init_pub_key.clone());
  20. loop {
  21. let mut buffer = String::new();
  22. match std::io::stdin().read_line(&mut buffer) {
  23. Ok(n) => {
  24. if n == 0 {
  25. break; // Beenden bei EOF
  26. }
  27. //
  28. // Encryption
  29. //
  30. // Get a random nonce to encrypt the message under
  31. let nonce = SalsaBox::generate_nonce(&mut OsRng);
  32. // Message to encrypt
  33. let plaintext = buffer.trim().as_bytes();
  34. // Encrypt the message using the box
  35. let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
  36. println!("Sending {0} as {1:?}", buffer.trim(), plaintext);
  37. stream.write(buffer.as_bytes()).unwrap();
  38. //
  39. // Decryption
  40. //
  41. // Decrypt the message, using the same randomly generated nonce
  42. let decrypted_plaintext =
  43. salsa_box.decrypt(&nonce, &ciphertext[..]).expect("Fehler");
  44. let dec_plain_plaintext =
  45. std::str::from_utf8(&*decrypted_plaintext).expect("Nö");
  46. assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
  47. println!(
  48. "Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}",
  49. plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext
  50. );
  51. }
  52. Err(error) => {
  53. println!("error: {error}");
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. Err(e) => {
  60. println!("Failed to connect: {}", e);
  61. }
  62. }
  63. println!("Terminated.");
  64. }
  65. fn generate_box(partner_public_key: PublicKey) -> (SalsaBox, PublicKey) {
  66. // Generate a random secret key.
  67. // NOTE: The secret key bytes can be accessed by calling `secret_key.as_bytes()`
  68. let own_secret_key = SecretKey::generate(&mut OsRng);
  69. // Get the public key for the secret key we just generated
  70. let own_public_key = own_secret_key.public_key().clone();
  71. // Create a `SalsaBox` by performing Diffie-Hellman key agreement between
  72. // the two keys.
  73. let salsa_box = SalsaBox::new(&partner_public_key, &own_secret_key);
  74. (salsa_box, own_public_key)
  75. }