LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Crypto Formula (http://www.linuxquestions.org/questions/programming-9/crypto-formula-480641/)

rubadub 09-05-2006 11:45 AM

Crypto Formula
 
Hi,

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:

Code:


//                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...

magnus.therning 09-06-2006 04:39 AM

Quote:

Originally Posted by rubadub
Hi,

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:

Code:


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...

exp(double) is the base-e exponential function, i.e. it calculates (roughly) 2.71^e. That's hardly what you are looking for.

You should probably also factor in the severe risk for integer overflow when using builtin types. Your goal is to make a program that calculates 88^7, which is 40867559636992. A 32-bit machine can't represent that in an int.

juvestar15 09-06-2006 10:16 AM

What magnus said. ^^

Not sure on the data type but the formula should be this.... if I can remember crypto class. :D
iret = pow(M,e) % N;


All times are GMT -5. The time now is 10:58 PM.