3

I am trying to implement the RSA Key Generation algorithm for a school project. I am having trouble with the encryption stage of the algorithm. It requires me to raise a user inputted integers to the power of a random number (random number has a criteria) and these random numbers are usually 2 or 3 digits long; eg. 23^239 = d. In most situations, the 'd' value is 100s of digits long and I cannot find anything to store it as. I have tried long long but the return value is always negative.

How can I generate and store very large numbers?

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
user3519448
  • 111
  • 1
  • 9

1 Answers1

4

All exponentiation in RSA is modular exponentiation. That is, you'll never just compute 23^239... instead, you'll compute something like 23^239 mod 11. You could calculate the exponentiation, and then calculate the modulus.... but as you've seen, that requires the ability to represent a pretty big number. Instead, one does the two operations at the same time; this is much more efficient, and does not require much storage space.

See https://en.wikipedia.org/wiki/Modular_exponentiation for more details, and algorithms.

(Note that, ordinarily, you would still need a bigint library for RSA, because you'd be working with numbers hundreds of digits long, not 2 or 3 digits long. The assignment here seems to have been designed to keep you from having to do that, though.)

Sneftel
  • 40,271
  • 12
  • 71
  • 104