# Thread: pow function returns wrong value

1. ## pow function returns wrong value

I am very confused as to why this is happening, and cannot find any information on the correction of the issue.

I am doing a homework assignment in which I need to implement the RSA Algorithm and encrypt, then decrypt a plain text file which reads "Every good boy deserves food." without the quotes. I have managed to create both the public keys and the private key without difficulty, but when working on encrypting the file, which I am reading character by character with putc, I cannot get it to properly do this formula:

for encryption: C = m^e mod n
for decryption: M = C^d mod n

I have written these function which using the pow function to raise the inputed character to the power of e, and then mods it by n. The problem occurs at raising the value to the power of e.

The character y will be my example, but this is standard for all characters in the file to date.

When the character y is read into the function and sent through pow(input,e) (input is an int and e is an unsigned int, though pow calls for doubles) it returns the value of 167620825 instead of 25937424601.

e is 5, n is 91 and y is 121 in this equation.

When the final mod is done, instead of being 88, I get 8, which is ofcourse throwing the entire algorithm off balance.

Each and every character is the wrong when computed, and I am at a point of utter frustration. I am including the code below to show you the encryptMessage function to see if anyone can explain why this is not working and what I can do to get aimed in the right direction. Thank you.

Code:
```void encryptMessage(int input) {
unsigned int cypher = 0, //cypher key
e = 0, //first public encryption key
n = P*Q, //second public encryption key
power = 0; //value of inputed character raised to the e
e = getPublicKey(P,Q); //generate public keys
//computer cypher key
printf("\nEncrypt Character:  %u\n", input);
power = pow(((double)input),e);
printf("\nEncrypt Power:  %u\n", power);
cypher = power%n;
printf("\nEncrypt Cypher:  %u\n", cypher);
//fprintf(encrypt, "%c", (char)cypher);
putc(cypher, encrypt);
} //end encrypt method```
Thank you,

CK

2. I've got good news and bad news. The good news is that pow is correct. The bad is of course that the errors seem to be occurring in printf. The best way to get accurate results would be to use doubles as much as you can. Like most functions using integers for large floating point operations tends to leave you with inaccurate results. You could write your own pow routine for ints though:

Example
Code:
```int ipow(int a, int b) {
for(;b--;)
}```
But I think it would be best if you used doubles.

I just tested the ipow() function with your values and got an incorrect returns, so it would be advantageous to use doubles.

3. ## pow function returns wrong value

Originally posted by master5001
[B]I've got good news and bad news. The good news is that pow is correct. The bad is of course that the errors seem to be occurring in printf. The best way to get accurate results would be to use doubles as much as you can. Like most functions using integers for large floating point operations tends to leave you with inaccurate results. You could write your own pow routine for ints though:

How then do I get around the problem of the next step, in which I mod the resultant value and use that to write my character? When I use a double I cannot compile due to inproper binary operators around the % symbol for that line of code? Would casting the result of power to int for that mathematical equation still skew my results?

And thanks, I will go play with this while I wait for more input..thank you much...

CK

4. Ah yes, the modulus operation. You can use modf() for splitting a double into an integer and a fraction. Then you could convert the fraction part into a remainder. That is the best way I can think of doing this off the top of my head.

5. Your values are overflowing. Use doubles to store the high numbers.

Does this do what you want:
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
int input = 'y';
int e = 5;
int n = 91;

double power, cypher ;

power = pow(input,e);

printf("\nEncrypt Power:  %f\n", power);

cypher = fmod(power,n);

printf("\nEncrypt Cypher:  %f\n", cypher);

putc(cypher, stdout);

return 0;
}```

6. ## pow function returns wrong value

Originally posted by Hammer
Your values are overflowing. Use doubles to store the high numbers.

Does this do what you want:
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
int input = 'y';
int e = 5;
int n = 91;

double power, cypher ;

power = pow(input,e);

printf("\nEncrypt Power:  %f\n", power);

cypher = fmod(power,n);

printf("\nEncrypt Cypher:  %f\n", cypher);

putc(cypher, stdout);

return 0;
}```
Yes, actually, it's a lot closer than I have been. My only battle now it to reverse the process. I truly appreciate your help.

I am working on taking the result of the above ('X') and turn it back into ('y') to see if my decrypt function is working properly with these modifcations, but I believe I am running into an overflow again.

Fortuantely, I have another week to battle this issue on the assignment and I truly appreciate your insight as to why I am having these problems. Thank you.

CK