LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 09-05-2006, 12:45 PM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Rep: Reputation: 33
Exclamation 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...
 
Old 09-06-2006, 05:39 AM   #2
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 21

Rep: Reputation: 0
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.
 
Old 09-06-2006, 11:16 AM   #3
juvestar15
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Debian Sid
Posts: 60

Rep: Reputation: 15
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;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenOffic.org cant read .doc formula?! milad126 Linux - General 3 09-06-2006 07:49 AM
Bitwise >> formula concept... debiant Programming 10 08-31-2006 09:28 PM
open office formula editor Four Linux - Newbie 1 03-30-2006 10:46 PM
OpenOffice Calc formula question Bigun Linux - Software 2 08-16-2004 04:07 PM
Dictionary and Formula Symbols in OpenOffice-debian... hellblade Linux - Software 2 04-29-2004 12:23 PM


All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration