0

I have given a homework about RSA Encryption, and I have wanted to complete this homework with C Programming Language. By the way, I am trying to find an answer for a problem connected with my code, not only about pow(), so it should not be marked as duplicate question.

int  *Encrypt(int* translation, int size){

int chunk;
const int E_RSA = 13;
double N_RSA = 2537;

// some codes...

//formulae = c = m^e mod n
encryptedMessage = fmod(pow(chunk,E_RSA) ,  N_RSA);

//some codes...

}

So here, when I calculate pow(chunk,E_RSA), because my E value is 13, and let's assume that the message is 1900, there will be an operation such as 1900^13, and it will give a huge output. When I calculate it from a calculator, it give a different result from my C code. This difference is being a problem for the encryption. How can I solve it so that the pow() function will return result as precise as a scientific calculator ? Thanks for the help.

Ozan Yurtsever
  • 1,274
  • 8
  • 32
  • 1
    `pow` and `fmod` are for `double`, do not use floating point functions when you calculate with integers. You probably need a bigint library, or you take a look at modular exponentiation. – Osiris Dec 03 '18 at 20:27
  • You don't actually need to calculate that big power... You should investigate modular exponentiation. – Matteo Italia Dec 03 '18 at 20:30
  • 1
    Hint: calculating m^e is different from calculating m^e mod n. Assuming `n` fits in a `int`, you won't need a bigint library. – dbush Dec 03 '18 at 20:30
  • Performing a modular exponentiation using the "dumb" way is not the best idea. There are algorithms for that. – Eugene Sh. Dec 03 '18 at 20:30
  • @Jean-FrançoisFabre: [That](https://stackoverflow.com/questions/17063102/differences-in-rounded-result-when-calling-pow) is not a good original. That is about errors in poor implementations of `pow`. This question is about finding a modulo residue in integer arithmetic. `pow` and floating-point arithmetic cannot be used to solve the problem in this question. – Eric Postpischil Dec 03 '18 at 20:34
  • okay, still a duplicate. Better now? – Jean-François Fabre Dec 03 '18 at 20:39
  • @Jean-François Fabre, the title of the question which you are saying I am duplicating from is actually the answer my question. Calculating pow(a,b) mod n is not what I was looking for, simply I didn't know there is a different calculation for modular exponentiation. – Ozan Yurtsever Dec 03 '18 at 20:39
  • 1
    okay, my job is done now :) – Jean-François Fabre Dec 03 '18 at 20:40

0 Answers0