Although this introduce several casts, it might be the most clean way to do the calculation, since you use the standard libs:
#include <math.h>
answer = ((int) pow((double) m, (double) e)) % n;
0
Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.
'e>>1' for symmetry with 'e&1', and to raise suspicion about it working with e negative.
('e&1' usually works better than 'e%2' for negative e, but in this case, speed may be the only difference)
I'd have made e unsigned, but the question asked for int
0
kplonkAuthor Commented:
E(m)=m^e mod n = c
D(c)=c^d mod n
using e and d =11 and n=35
any value of m should give a value of c and then when c is in the second formual i should get back to m but this dont work, for some reason. is % the mod that i want?
e and d =11 and n=35 should work.
but if you are using PC_User321's or hernani's method, you are probably overflowing an int.
0
kplonkAuthor Commented:
yer but is this the correct, version of mod i am not sure that this is the thing that i am looking for, eg if m is 3 and e=11 n=35 then encyrapting gives 12 and decrypting gives 3 all is well, but 4 does not work so i recok ythe mod i asm using is not the correct interpretation of mod. what you reckon.
3^11 % 35 = 12 is correct
12^11 % 35 = 3
4^11 % 35 = 9
9^11 % 35 = 4
But 9^11 = 31381059609, which overflows a 32 bit int,
So you can't use the method proposed by PC_User321 or hernani
My method will work fine. You must heed the warning on the last, which says "If you use large numbers you will have to use larger variable types, e.g. long."
In particular you should ensure that the types used for 'result' and 'power' can hold the size of the result of the exponentiation.
My method will work fine. You must heed the warning on the last, which says "If you use large numbers you will have to use larger variable types, e.g. long."
In particular you should ensure that the types used for 'result' and 'power' can hold the size of the result of the exponentiation.
Wouldn't
4^5 % 35 = 9
9^5 % 35 = 4
be more in the spirit of things, considering that 5 x 7 = 35 and
(4 x 6) +1 = 25 whose factors are 5?
And you should stick with ozo's method.
0
kplonkAuthor Commented:
thanks for all the help
0
Featured Post
Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.
{
int index;
int result;
result = 1;
for (index = 0; index < n; index++)
result = result* x;
return(result);
}
/* To calculate m^e mod n */
answer = power(m, e) % n;
No headers are necessary.
This assumes that e is positive.
If you use large numbers you will have to use larger variable types, e.g. long.