Simple AES chat program
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

server.c 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include "comm.h"
  4. #include "aes.h"
  5. int main(int argc, char **argv)
  6. {
  7. struct session sess;
  8. initializeSBox();
  9. sess_init(&sess);
  10. if (argc != 4)
  11. {
  12. printf("Usage: %s <key file> <bind address> <port>\n", argv[0]);
  13. return 0;
  14. }
  15. FILE *keyf = fopen(argv[1], "r");
  16. if (keyf == NULL)
  17. {
  18. perror("could not open key file");
  19. return 1;
  20. }
  21. if (4 != gmp_fscanf(keyf, "m=%Zd\ng=%Zd\nx=%Zd\np=%Zd\n", sess.eg.m, sess.eg.g, sess.eg.x, sess.eg.p))
  22. {
  23. fprintf(stderr, "not a valid key file");
  24. return 2;
  25. }
  26. fclose(keyf);
  27. if (do_resolve(argv[2], argv[3], &sess.params))
  28. {
  29. perror("could not open socket");
  30. return 1;
  31. }
  32. if (do_receive(&sess))
  33. {
  34. perror("could not accept");
  35. return 1;
  36. }
  37. int byte;
  38. while (1)
  39. {
  40. byte = getchar();
  41. if (byte == EOF)
  42. break;
  43. send_encrypted_byte(&sess, byte);
  44. if (byte == '\n')
  45. {
  46. send_encrypted_byte(&sess, 0);
  47. while ((byte = recv_encrypted_byte(&sess)))
  48. {
  49. if (byte == EOF)
  50. {
  51. goto break2;
  52. }
  53. printf("%c", byte);
  54. }
  55. }
  56. }
  57. break2:
  58. do_unresolve(sess.params);
  59. sess_destroy(&sess);
  60. }