Cryptography Reference
In-Depth Information
// generate primeq>pwith length qmin <= q <= qmax
key.q = findprime (qmin, qmax, PubExp);
// generate modulus mod = p*q such that 2 ˆ (length - 1) <= mod < 2 ˆ length
key.mod = key.p * key.q;
// calculate Euler phi function
LINT phi_n = key.mod - key.p - key.q + 1;
// generate public exponent if not specified in PubExp
if (1 == PubExp)
{
key.pubexp = randBBS (length/2) | 1; // half the length of the modulus
while (gcd (key.pubexp, phi_n) != 1)
{
++key.pubexp;
++key.pubexp;
}
}
else
{
key.pubexp = PubExp;
}
// generate secret exponent
key.prvexp = key.pubexp.inv (phi_n);
// generate secret components for rapid decryption
key.ep = key.prvexp % (key.p - 1);
key.eq = key.prvexp % (key.q - 1);
key.r = inv (key.p, key.q);
return testkey();
}
// test function for RSA-key
int RSAkey::testkey (void)
{
LINT mess = randBBS (ld (key.mod) >> 1);
return (mess == fastdecrypt (mexpkm (mess, key.pubexp, key.mod)));
}
// rapid RSA decryption
LINT RSAkey::fastdecrypt (const LINT& mess)
{
LINT m, w;
m = mexpkm (mess, key.ep, key.p);
w = mexpkm (mess, key.eq, key.q);
w.msub (m, key.q);
w = w.mmul (key.r, key.q) * key.p;
Search WWH ::




Custom Search