Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #include "BigInt.h"
  8. #include "BIStack.h"
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. bool primetest(BigInt b) {
  14. bool flag = false;
  15. flag = b.isProbablePrime(1000);
  16. if(!flag)
  17. return false;
  18. return true;
  19. }
  20.  
  21. bool easynotprime(BigInt b) {
  22. if(b % BigInt::two() == BigInt::zero()) return false;
  23. BigInt three = BigInt::two() + BigInt::one();
  24. BigInt five = three + BigInt::two();
  25. BigInt seven = five + BigInt::two();
  26. BigInt eleven = seven + three + BigInt::one();
  27. if(b % three == BigInt::zero() || b % five == BigInt::zero() || b % seven == BigInt::zero() || b % eleven == BigInt::zero())
  28. return false;
  29. else
  30. return true;
  31. }
  32.  
  33. int main() {
  34. FILE *f, *g;
  35. f = fopen("open.txt", "w");
  36. g = fopen("close.txt", "w");
  37. BigInt b = -BigInt::one();
  38. b.generateRandom(256);
  39. if(b < BigInt::zero())
  40. b = BigInt::zero() - b;
  41. while(!primetest(b)) {
  42. b += BigInt::one();
  43. while(!easynotprime(b))
  44. b += BigInt::one();
  45. }
  46.  
  47. BigInt c = -BigInt::one();
  48. c.generateRandom(256);
  49. if(c < BigInt::zero())
  50. c = BigInt::zero() - c;
  51. while(!primetest(c)) {
  52. c += BigInt::one();
  53. while(!easynotprime(c))
  54. c += BigInt::one();
  55. }
  56.  
  57. BigInt n = b * c;
  58. BigInt fn = (b - 1) * (c - 1), e;
  59. char line1[513];
  60. fn.toString(line1, 513);
  61.  
  62. e.generateRandom(20);
  63. if(e < BigInt::zero())
  64. e = BigInt::zero() - e;
  65. while(BigInt::gcd(fn, e) != BigInt::one()) {
  66. e += BigInt::one();
  67. }
  68. char line3[512];
  69. e.toString(line3, 512);
  70.  
  71. BigInt d, p, a;
  72. a = BigInt::extEucl(e, fn, d, p, a);
  73. char line4[512];
  74. if(d < BigInt::zero())
  75. d += fn;
  76. d.toString(line4, 512);
  77.  
  78. fprintf(f, "Открытый ключ\n%s\n%s\n", line1, line3);
  79. fprintf(g, "Закрытый ключ\n%s\n%s\n", line1, line4);
  80. fclose(f);
  81. fclose(g);
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement