Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <time.h>
- #include "BigInt.h"
- #include "BIStack.h"
- #include <iostream>
- using namespace std;
- bool primetest(BigInt b) {
- bool flag = false;
- flag = b.isProbablePrime(1000);
- if(!flag)
- return false;
- return true;
- }
- bool easynotprime(BigInt b) {
- if(b % BigInt::two() == BigInt::zero()) return false;
- BigInt three = BigInt::two() + BigInt::one();
- BigInt five = three + BigInt::two();
- BigInt seven = five + BigInt::two();
- BigInt eleven = seven + three + BigInt::one();
- if(b % three == BigInt::zero() || b % five == BigInt::zero() || b % seven == BigInt::zero() || b % eleven == BigInt::zero())
- return false;
- else
- return true;
- }
- int main() {
- FILE *f, *g;
- f = fopen("open.txt", "w");
- g = fopen("close.txt", "w");
- BigInt b = -BigInt::one();
- b.generateRandom(256);
- if(b < BigInt::zero())
- b = BigInt::zero() - b;
- while(!primetest(b)) {
- b += BigInt::one();
- while(!easynotprime(b))
- b += BigInt::one();
- }
- BigInt c = -BigInt::one();
- c.generateRandom(256);
- if(c < BigInt::zero())
- c = BigInt::zero() - c;
- while(!primetest(c)) {
- c += BigInt::one();
- while(!easynotprime(c))
- c += BigInt::one();
- }
- BigInt n = b * c;
- BigInt fn = (b - 1) * (c - 1), e;
- char line1[513];
- fn.toString(line1, 513);
- e.generateRandom(20);
- if(e < BigInt::zero())
- e = BigInt::zero() - e;
- while(BigInt::gcd(fn, e) != BigInt::one()) {
- e += BigInt::one();
- }
- char line3[512];
- e.toString(line3, 512);
- BigInt d, p, a;
- a = BigInt::extEucl(e, fn, d, p, a);
- char line4[512];
- if(d < BigInt::zero())
- d += fn;
- d.toString(line4, 512);
- fprintf(f, "Открытый ключ\n%s\n%s\n", line1, line3);
- fprintf(g, "Закрытый ключ\n%s\n%s\n", line1, line4);
- fclose(f);
- fclose(g);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement