#include<iostream> #include<stdio.h> #include<cmath> #include<time.h> #include<stdlib.h> using std::cin; using std::cout; using std::endl; class Pair{ public: Pair(){}; int e,d,n; }; bool is_prime(int n,int *p,int c){ if(c>sqrt((double)n)) c=sqrt((double)n); for(int i=1;i<=c;i++) if(n%p[i]==0) return false; return true; } Pair rsa(){ while(true){ cout<<"finding......."<<endl; bool find=true; long begain,end; begain=clock(); srand((int)time(0)); int n=100+rand()%100; int *prime = new int[n]; prime[1] = 2; int current=1; for(int i=3;i<=n;i++){ if(is_prime(i,prime,current)){ prime[++current]=i; }else continue; } int p = current-rand()%10; int q = current-rand()%10; while(p==q){ q = current-rand()%1; } p = prime[p]; q = prime[q]; n = p*q; int f_n = (p-1)*(q-1); int e = (int)rand()%20; int i=0; e=prime[e]; while((e==p)||(e==q)||(e>f_n)||(e==0)){ e = rand()%10; i-=1; e=prime[e]; } int d = 1; while(((e*d)%f_n)!=1){ d++; end=clock(); if(end-begain>5000000){ find = false; break; } } Pair pair; pair.e=e; pair.d=d; pair.n=n; if(find) return pair; } } int Mont(int b,int e,int n){ int rt=1; int x=b; while(e>0){ if(e%2==1) rt=rt*x%n; x=x*x%n; e/=2; } return rt; } int main(){ cout<<"Welcome to use rsa , because of the aplication don't support big number , your key will not security enough"<<endl; Pair pair = rsa(); cout<<"YOUR PU("<<pair.e<<","<<pair.n<<")"<<endl; cout<<"YOUR PR("<<pair.d<<","<<pair.n<<")"<<endl; cout<<"please input an string :"; char plain[300]; int cipher[300]; fgets(plain,300,stdin); int last = 0; for(int i=0;i<300;i++) if((plain[i]=='n')||(plain[i]=='
|
近期评论