getting the mod from a double number in C++

Posted on 2008-06-25
Last Modified: 2010-04-21
Hi, Im trying to do the RSA Algorithm as in this page:

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(){
  n=p*q;//n for Encryption  and DesEncryption 
  for(e=2;e<m;e++)//getting e as public key
  for(int x=0;x<m;x++)
      d=(1+x*m)/e;//getting d as private key
   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

Question by:manganzon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

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.

Author Comment

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?

Accepted Solution

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

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


Author Closing Comment

ID: 31470730

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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