Simple AES chat program
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "randsource.h"
  2. #include "sgprime.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #define RAND_SIZE 512
  7. #define CHUNK_SIZE 1
  8. int main(int argc, char **argv)
  9. {
  10. unsigned int threads;
  11. unsigned int bits;
  12. FILE *keyoutf;
  13. if (argc < 3 || argc > 4)
  14. {
  15. printf("Usage: %s <bits> <output file> [threads]\n", argv[0]);
  16. return 0;
  17. }
  18. if (argc == 3)
  19. {
  20. threads = 0;
  21. } else
  22. {
  23. if (1 != sscanf(argv[3], "%u", &threads) || threads == 0)
  24. {
  25. printf("number of threads must be a positive integer\n");
  26. return 0;
  27. }
  28. }
  29. if (1 != sscanf(argv[1], "%u", &bits) || bits == 0)
  30. {
  31. printf("number of bits must be a positive integer\n");
  32. return 0;
  33. }
  34. keyoutf = fopen(argv[2], "w");
  35. if (keyoutf == NULL)
  36. {
  37. perror("could not open key out file: ");
  38. return 1;
  39. }
  40. printf("Key Generation Tool\n");
  41. printf("generating a key with %u bits ...\n", bits);
  42. fflush(stdout);
  43. mpz_t l, p, seed, rc, g, t;
  44. mpz_init(p);
  45. mpz_init(l);
  46. mpz_init(g);
  47. mpz_init(t);
  48. mpz_init(seed);
  49. mpz_set_ui(l, 0);
  50. mpz_setbit(l, bits);
  51. unsigned long int offset, primorial;
  52. void *difflist = make_difflist(l, &offset, &primorial);
  53. gmp_randstate_t rs;
  54. gmp_randinit_default(rs);
  55. char *randbuff = malloc(RAND_SIZE);
  56. if (fill_random(randbuff, RAND_SIZE))
  57. {
  58. perror("unable to read randomness source: ");
  59. return 2;
  60. }
  61. mpz_import(seed, RAND_SIZE, 1, 1, 0, 0, randbuff);
  62. gmp_randseed(rs, seed);
  63. free(randbuff);
  64. mpz_urandomb(p, rs, bits);
  65. mpz_setbit(p, bits);
  66. printf("generating safe prime ... ");
  67. fflush(stdout);
  68. get_sg_prime(difflist, offset, primorial, p, threads, bits + 4, CHUNK_SIZE);
  69. printf("done\n");
  70. fflush(stdout);
  71. destroy_difflist(difflist);
  72. mpz_mul_ui(l, p, 2);
  73. rc[0] = seed[0]; // Moving variable names -- not portable
  74. mpz_set(rc, l);
  75. mpz_add_ui(l, l, 1);
  76. printf("finding a generator ... ");
  77. fflush(stdout);
  78. while (1)
  79. {
  80. mpz_urandomm(g, rs, rc);
  81. mpz_add_ui(rc, rc, 1);
  82. mpz_powm_ui(t, g, 2, l);
  83. if (mpz_cmp_ui(g, 1) != 0)
  84. {
  85. mpz_powm(t, g, p, l);
  86. if (mpz_cmp_ui(g, 1) != 0)
  87. break;
  88. }
  89. }
  90. printf("done\n");
  91. fflush(stdout);
  92. printf("creating a secret exponent ... ");
  93. fflush(stdout);
  94. mpz_urandomm(p, rs, l);
  95. printf("done\n");
  96. fflush(stdout);
  97. printf("computing public point ... ");
  98. fflush(stdout);
  99. mpz_powm(rc, g, p, l);
  100. printf("done\n");
  101. fflush(stdout);
  102. gmp_randclear(rs);
  103. gmp_fprintf(keyoutf, "m=%Zd\ng=%Zd\nx=%Zd\np=%Zd\n", l, g, p, rc);
  104. mpz_clear(seed);
  105. mpz_clear(p);
  106. mpz_clear(l);
  107. mpz_clear(g);
  108. mpz_clear(t);
  109. fclose(keyoutf);
  110. }