View the Most Wanted LQ Wiki articles.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Crypto Formula
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 09-05-2006, 12:45 PM #1 rubadub Member   Registered: Jun 2004 Posts: 233 Rep: 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 #include #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...
09-06-2006, 05:39 AM   #2
magnus.therning
LQ Newbie

Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 21

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

 09-06-2006, 11:16 AM #3 juvestar15 Member   Registered: Aug 2005 Location: Australia Distribution: Debian Sid Posts: 60 Rep: What magnus said. ^^ Not sure on the data type but the formula should be this.... if I can remember crypto class. iret = pow(M,e) % N;