Implementing RSA Encryption - pow() with large outputs [duplicate] - c

This question already has answers here:
Calculating pow(a,b) mod n
(14 answers)
Power for big numbers modulo m in C
(4 answers)
Closed 4 years ago.
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.

Related

Computational error of pow function in calculation of mathematical expression in language c [duplicate]

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why does pow(5,2) become 24? [closed]
(3 answers)
Closed 2 years ago.
This code is part of an original code
Why the output of this code displays the number 24?
The output of this Code should be number 25!
Where's the problem with this code?
The compiler used is CodeBlocks
Thanks
int A=7;
A = pow((((A+1)/2)+1),2);
printf("%d", A);

"warning: division by zero [-Wdiv-by-zero]" when not dividing by zero [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 3 years ago.
My code for the following simple coding exercise produces a "division by zero" warning, and I'm wondering why.
#include <stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
}
return 1;
}
temps.c: In function ‘main’:
temps.c:6:45: warning: division by zero [-Wdiv-by-zero]
printf("celsius=%d fahrenheit=%d\n", i, (i/(5/9))+32);
I realised while writing this question that it's because I should have written 5.0/9.0, since C handles division with integers in a way that I didn't expect. Posting this anyway since I couldn't find this particular error linked to this particular problem on SO.

C - Count the number of combinations possible in a string without repetitions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Good night, what's the best way in C to count the number of possibilities of UNIQUE anagrams in a string with the maximum length of 256 without allows repetitions caused by same letters? The input would be just in uppercase and only alphabet letters are allowed, A-Z. I got stuck in the worst case of the program that got 26! a very large number that overflow even my double. I think I'm very lost here, I'm not so good in C. The program just need to shows the number of possibilities, not the anagram. Like:
LOL = 3
HOUSE = 120
OLD = 6
ABCDEFGHIJKLMNOPQRSTUVWXYZ = 403291461126605635584000000
Thank you guys very much... I tried a lot and failed in every single tried, I'm in distress with it. The way I got more closer of do it was in Pascal, but it also failed in some tests, and I can't use Pascal anyway. I'm using CodeBlocks on Windows that compiles with GCC.
You should calculate factorial of the length of the given string divided by the factorial of the occurrence of every letter.
long double logFactorial (int i) {
return i < 2 ? 0.L : (logFactorial (i-1)+log(long double (i));
}
int countLetter(const char* str, char c) {
int res = 0;
while (str && *str) {
res += *str++ == c;
}
return res;
}
long double numPermutations(const char* str) {
auto res = logFactorial (strlen(str));
for (char c = 'A'; c<='Z'; c++) {
res -= logFactorial (countLetter (str,c));
}
return exp((long double)res);
}
Pay attention!
There are several people here who were correct by saying that factorial of 26 cannot be stored even in 64bit integer.
Therefore, I changed my calculation to the logarithmic of the factorial number and store it in long double which I hope is precise enough (I think that the exp() function is not precise enough)
Nevertheless you cannot use this result as an integer value, unless you find a way to store it in 128bit integer or bigger...
You should test it too if this fits your problem.
There is a faster way to calculate the factorial for this problem by storing the results of 0! up to 26! in an array of the size of [27].
I will leave it to you for asking another question.

Wrong output of program [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{ float t=1/2;
printf("\n%.4f",t);
return 0;
}
i'm trying to write a program where this section of the code is needed. The program gives wrong output and ive identified the section which is causing the problem. Instead of 0.500000 im getting 0.00000.In this program ive used constants to find whether its working or not.
cant seem to find the problem.
please help.
Thanks.
I'll link you back to Why I am getting zero in float expressions like 1/2? which has the answer.
TLDR: When you do 1/2, you're doing integer division instead of float division and getting 0. Try either 1.0/2, 1/2.0, or 1.0/2.0.

Converting char integer representations to binary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a char array composed of elements 5 3. The array is used to represent number 53. What should be the approach to convert this number of three chars to its binary equivalent? I am implementing this in C, later on it will need to be rewritten in assembly. The solution I seek should be purely low stuff work without any helper libraries.
I am basically stuck with an idea to convert separately 5 and 4 (via mapping 5 and 4 to their ascii equivalents). Yet the idea would not work for sure. I have another idea to convert char '5' to int 5 by right shifting the byte by 4. Same with 4. Then multiply 5 by 10 and add 4, and then use division by two algorithm to find remainder and compose the binary number.
In C:
int asciToInteger(char *c)
{
int result = 0;
while (*c)
{
result *= 10;
result += (*c - '0');
c++;
}
return result;
}
Assumes input is valid.
You can get a head start on the assembly language version by compiling with certain switches which will output as ... assembly language! For example in GNU C: gcc -S -c ascii2int.c.

Resources