/* Returns true if "candidate" is a witness for the compositeness * of "number", false if "candidate" is a strong liar. "exponent" * and "squareCount" are used for computation */ bool PrimeGenerator::isWitness( BigInt candidate, const BigInt &number, const BigInt &exponent, unsigned long int squareCount, const BigInt &numberMinusOne) { //calculate candidate = (candidate to the power of exponent) mod number candidate.SetPowerMod(exponent, number); //temporary variable, used to call the divide function BigInt quotient; for (unsigned long int i = 0; i < squareCount; i++) { bool maybeWitness(false); if (candidate != BigIntOne && candidate != numberMinusOne) maybeWitness = true; //PrimeGenerator used to be a friend of BigInt, so the following //statement produced the result in one call to BigInt::divide() // BigInt::divide(candidate * candidate, number, quotient, candidate); //That doesn't work any more, so we have to use two calls candidate = candidate * candidate; quotient = (candidate) / number; candidate = (candidate) % number; if (maybeWitness && candidate == BigIntOne) return true; //definitely a composite number } if (candidate != BigIntOne) return true; //definitely a composite number return false; //probable prime }
/* Returns true if "candidate" is a witness for the compositeness * of "number", false if "candidate" is a strong liar. "exponent" * and "squareCount" are used for computation */ bool PrimeGenerator::isWitness( BigInt candidate, const BigInt &number, const BigInt &exponent, unsigned long int squareCount, const BigInt &numberMinusOne) { //calculate candidate = (candidate to the power of exponent) mod number candidate.SetPowerMod(exponent, number); //temporary variable, used to call the divide function BigInt quotient; for (unsigned long int i = 0; i < squareCount; i++) { bool maybeWitness(false); if (candidate != BigIntOne && candidate != numberMinusOne) maybeWitness = true; BigInt::divide(candidate * candidate, number, quotient, candidate); if (maybeWitness && candidate == BigIntOne) return true; //definitely a composite number } if (candidate != BigIntOne) return true; //definitely a composite number return false; //probable prime }
/* Decrypts a "chunk" (a small part of a message) using "key" */ string RSA::decryptChunk(const BigInt &chunk, const Key &key) { BigInt a = chunk; // The RSA decryption algorithm is a congruence equation. a.SetPowerMod(key.GetExponent(), key.GetModulus()); // Decode the message to a readable form. return RSA::decode(a); }
/* Encrypts a "chunk" (a small part of a message) using "key" */ string RSA::encryptChunk(const string &chunk, const Key &key) { // First encode the chunk, to make sure it is represented as an integer. BigInt a = RSA::encode(chunk); // The RSA encryption algorithm is a congruence equation. a.SetPowerMod(key.GetExponent(), key.GetModulus()); return a.ToString(); }