Do not use unsigned values here because it invalidates the algorithm. So instead of unsigned long long int
you could use long long int
. Also note that iter = -iter
will not work as you intended when using unsigned values because it will underflow. Let's say you have iter = 1
then -iter
won't be -1
but instead 18446744073709551615
(=2^64 - 1). The check for iter < 0
will always be false, too.
For any real life applications you will need much larger numbers than you can fit into 64-bit types. There are many ways of doing this but GMP (GNU Multiple Precision lib) has mpz_powm(r, base, exp, mod)
that you can use in the decryption phase. More info here.
So basically if you had your cipher text c
and your private key defined as n
(=p*q) and d
then you could decrypt c
into plaintext m
like this:
#include <gmp.h> // requires GMP lib to be installed.
void rsaDecrypt(const mpz_t c, const mpz_t n, const mpz_t d, mpz_t m) {
mpz_init(m);
mpz_powm(m, c, d, n); // m = c^d (mod n)
}