For one of my programs i'm trying to impliment my own version of the RSA algorithm, the encryption works fine, however the decrption does not, even though it uses basically the same algorithm. The code is as follows:
// g++ main.c -o main
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
int do_crypto(int M, int e, int N);
int main()
{
printf("...\n");
// TWO PRIMES FOR OUR KEY
int p = 17;
int q = 11;
// THIRD PART OF OUR KEY (PUBLIC)
int e = 7;
// CALCULATE THE OTHER PART OF THE PUBLIC KEY
int N = p * q; // = 187
// THE CHARACTER TO ENCODE AS ASCII
int M = 88;
//int M = "M";
// ENCRYPT A CHARACTER
//int C = int ( M * exp(e) ) % N;
int C = do_crypto(M, e, N); // int M, int e, int N)
printf("C = %d \n", C);
// CALCULATE THE DECRYPT KEY
//int d = ( 1 % ( (p-1) * (q-1) ) ) / e;
int d = ( ( (p-1) * (q-1) ) / e );
printf("d = %d \n", d);
// DECRYPT THE CHARACTER
int m = do_crypto(11, 23, 187); // int C, int d, int N)
printf("m = %d \n", m);
return 0;
}
int do_crypto(int M, int e, int N)
{
int iret = int ( M * exp(e) ) % N;
return iret;
}
When decrypting I have put in actual values for the keys etc, and the result should be 88.
The actual formulas are:
ENCRYPT: C = Me (Mod N)
DECRYPT: M = Cd (Mod N)
*** Please note the 'e' and 'd' are supposed to be superscript, e.g. raised to the power of...











