RSA

#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int TIMES=5;     //米勒罗宾素数测试次数
const int EXP=500000;   //p和q的差距
LL p,q,n,e,d,t;

LL big_rand(LL m)       //生成大的随机数
{
    LL x=rand();
    x*=rand();
    if(x<0) x=-x;
    return x%=m;
}

LL mul(LL a,LL b,LL mod)   //快速乘法,解决模大数的时候乘法溢出的问题
{
    LL ans=0;
    while(b)
    {
        if(b&1) ans=(ans+a)%mod;
        b>>=1;
        a=(a+a)%mod;
    }
    return ans;
}

LL quickpow(LL a,LL b,LL mod)   //快速幂
{
    LL ans=1;
    while(b)
    {
        if(b&1) ans=mul(ans,a,mod);
        b>>=1;
        a=mul(a,a,mod);
    }
    return ans;
}

LL gcd(LL a,LL b) {return b?gcd(b,a%b):a;}

LL exgcd(LL a,LL b,LL &x,LL &y) //扩展gcd
{
    LL d;
    if(!b) {x=1;y=0;return a;}
    d=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}

bool Miller_Rabbin(LL x)        //米勒-罗宾测试
{
    LL a,m=(x-1)>>2,i=1;
    do{
        a=(LL)rand()*rand()%(x-1)+1;
        if(quickpow(a,x-1,x)!=1) return 0;
    }while(i++


你可能感兴趣的:(其它)