Solved

getting the mod from a double number in C++

Posted on 2008-06-25
4
996 Views
Last Modified: 2010-04-21
Hi, Im trying to do the RSA Algorithm as in this page:
http://pajhome.org.uk/crypt/rsa/rsa.html

but I have a problem with data types (in, double, lon int)

My code is like this:
double p,q,n,m,e,d;

double coded,decoded,msg;
 
 

unsigned int mcd(unsigned int a, unsigned int b){

     return (b == 0)? a : mcd(b, a % b);

}
 

void main(){

  p=7;

  q=9;

  n=p*q;//n for Encryption  and DesEncryption 
 

  m=(p-1)*(q-1);
 

  for(e=2;e<m;e++)//getting e as public key

    if(mcd(m,e)==1)

      break;
 

  for(int x=0;x<m;x++)

  {

    if(fmod((1+x*m),e)==0)

    {

      d=(1+x*m)/e;//getting d as private key

      break;

    }

  }

}

   msg=6;//original message

  //Here I try to Encryp the message

  coded=fmod(pow(mensaje,e),n);//cant use % coz are doubles....

  //coded is 62
 

  //Now I try yo DesEncryp the message

   decoded=fmod(pow(cifrado,d),n);//It is suppose that i must result 6 as msg, but it doesn't. It give me 21.

Open in new window

0
Comment
Question by:manganzon
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Darrylsh
ID: 21869429
Why are you using Doubles.  You probably should be using a Big Number library depending on the size of intergers you need, but for learning purposes, you could use a 64 bit int which on most systems would be type long long.
0
 

Author Comment

by:manganzon
ID: 21871922
Im using double coz I need a large rangen and long int is not enough. I need very large numbers like 65^70.

Whats the range of  64bits Int? and how do I declare a variable of it?  and wich library do you mean?
0
 
LVL 3

Accepted Solution

by:
Darrylsh earned 125 total points
ID: 21871943
unsigned a 64 bit integer is 0 to 18,446,744,073,709,551,615
They are declare like:

long long MyVar;

If you need 65^70 you will need a large number library.  I'd recommend GMP which can be found here http://gmplib.org/

Doubles won't work  because of rounding, if you are doing cryptography, you need integers.


0
 

Author Closing Comment

by:manganzon
ID: 31470730
thanks;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By default, Carbonite Server Backup manages your encryption key for you using Advanced Encryption Standard (AES) 128-bit encryption. If you choose to manage your private encryption key, your backups will be encrypted using AES 256-bit encryption.
Article by: btan
Provide an easy one stop to quickly get the relevant information on common asked question on Ransomware in Expert Exchange.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now