示例#1
0
void RSA::GenRSAKey(RSA_PRIVATE_KEY *pri_key, RSA_PUBLIC_KEY *pub_key)
{
    int i, phi;
    pri_key->d = -1;    //set d to it's sentinel value

    while (pri_key->d < 0)
    {
        pri_key->p = 0; //clear p
        while ((pri_key->p)*(pri_key->q) < 255 || pri_key->p == pri_key->q) //Get p and q if n < 255 or p == q
        {
            pri_key->p = rand()%51+75;  //Get seed for genPrime from 75 to 125
            pri_key->p |= 1 << 0; //sets the lowest bit of seed to a 1 to ensure a odd number
            pri_key->p = genPrime(pri_key->p);  //pass seed to genPrime to find next lowest prime
            pri_key->q = rand()%51+75;
            pri_key->q |= 1 << 0; //sets the lowest bit to a 1 to ensure a odd number
            pri_key->q = genPrime(pri_key->q);
        }

        pri_key->n = (pri_key->p)*(pri_key->q); //set n
        phi = (pri_key->p - 1)*(pri_key->q - 1);    //set phi

        for (i = 0; i < 6; i++) //Only allow 5 guesses here before new primes are picked
        {
            pri_key->d = -1;
            pri_key->e = picke(pri_key->p, pri_key->q); //pick a e
            pri_key->d = findd(pri_key->e, phi);    //find a d
            if (pri_key->d > 0) //break from loop if a valid d is found matching with p,q, and e
                break;
        }
    }
    pub_key->d = pri_key->d;    //store public key exponent
    pub_key->n = pri_key->n;    //store public key modulus
    return;
}
示例#2
0
文件: 535C.cpp 项目: 8cbx/code-area
int main()
{
    long long l,t,m;
    while(scanf("%lld%lld%lld",&a,&b,&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld%lld",&l,&t,&m);
            be=a+(l-1)*b;
            long long tmpr=findd(l,t,m);
            if(tmpr<1)
            {
                printf("-1\n");
            }
            else
            {
                printf("%lld\n",tmpr+l-1);
            }
        }
    }
    return 0;
}