/* Creates an odd BigInt with the specified number of digits. * Returns it by reference in the "number" parameter. */ void PrimeGenerator::makePrimeCandidate(BigInt &number, unsigned long int digitCount) { PrimeGenerator::MakeRandom(number, digitCount); //make the number odd if (!number.IsOdd()) number.SetDigit(0, number.GetDigit(0) + 1); //make sure the leading digit is not a zero if (number.GetDigit(number.Length() - 1) == 0) number.SetDigit(number.Length() - 1, (std::rand() % 9) + 1); }
/* Transforms a BigInt cyphertext into a std::string cyphertext. */ string RSA::decode(const BigInt &message) { string decoded; // The special symbol '1' we added to the beginning of the encoded message // will now be positioned at message[message.Length() - 1], and // message.Length() -1 must be divisible by 3 without remainder. Thus we // can ignore the special symbol by only using digits in the range // from message[0] to message[message.Length() - 2]. for (unsigned long int i(0); i < message.Length() / 3; i++) { // Decode the characters using the ASCII values in the BigInt digits. char ASCII = 100 * char(message.GetDigit(i * 3)); ASCII += 10 * char(message.GetDigit(i * 3 + 1)); decoded.push_back(ASCII + char(message.GetDigit(i * 3 + 2))); } return decoded; }