0

I'm not a programming expert. I'm new to cryptography and I had gone through the security algorithm RSA. I wrote the code like this:

#include<math.h>
#include<iostream>
#include<cmath>
#include<Windows.h>

using namespace std;

class rsacrypto
{
    long publickey;
    long privatekey;
    long modl; //Modulus

    public :
    rsacrypto(); //To be used to just generate private and public keys.
    rsacrypto(long &,long &,long &);//To be used just to generate private and public keys.
    rsacrypto(long key,long modulus) // Should be used when a data is to be encrypted or decrypted using a key.
    {
        publickey = privatekey = key;
        modl = modulus;
    }

    long ret_publickey()
    {
        return publickey;
    }
    long ret_privatekey()
    {
        return privatekey;
    }
    long ret_modulus()
    {
        return modl;
    }

    void encrypt(char *);
    void decrypt(char *);
    int genrndprimes(int, int);
    int genrndnum(int, int);
    int totient(int);
    int gcd (int, int);
    int mulinv(int, int);
    boolean isPrime(long);
};

rsacrypto::rsacrypto()
    {
        long p1,p2; //Prime numbers
        long n = 0; //Modulus
        long phi =0; //Totient value.
        long e = 0; //Public key exponent.
        long d = 0; //Private key exponent.
        p1 = genrndprimes(1,10);
        Sleep(1000);
        p2 = genrndprimes(1,10);
        n = p1*p2;
        phi = (p1-1)*(p2-1);
        e = genrndnum(2,(phi-1));
        while(gcd(e,phi)!=1)
        {
            e = genrndnum(2,(phi-1));
        }
        d = mulinv(e, phi);
        cout<<"Public Key=("<<e<<","<<n<<")"<<"\n";
        cout<<"Private Key=("<<d<<","<<n<<")"<<"\n";
        privatekey = e;
        publickey = d;
        modl = n;
        int m=11;
        int en=0, decr=0;
        //Encryption
        en=(long)pow((double)m,d)%n;
        cout<<en<<"\n";
        //Decryption
        decr=(long)pow((double)en,e)%n;
        cout<<decr;
}
/*
void rsacrypto::encrypt(char *dat)
{
    long siz = strlen(dat);
    for(long i=0;i<siz;i++)
    {
        dat[i]=(long)pow((double)dat[i],publickey)%modl;
        cout<<i<<"="<<dat[i]<<"\n";
    }

}

void rsacrypto::decrypt(char *datn)
{
    long sizz = strlen(datn);
    for(long i=0;i<sizz;i++)
    {
        datn[i]=(long)pow((double)datn[i],privatekey)%modl;
    }
    cout<<datn;
}*/
int rsacrypto::mulinv(int a, int b)
{
    int b0 = b, t, q;
    int x0 = 0, x1 = 1;
    if (b == 1) return 1;
    while (a > 1) {
        q = a / b;
        t = b, b = a % b, a = t;
        t = x0, x0 = x1 - q * x0, x1 = t;
    }
    if (x1 < 0) x1 += b0;
    return x1;
}
int rsacrypto::genrndprimes(int a, int b){
    long pivot;
    do{
        pivot= rand() % b + a;
        if (isPrime(pivot))
        return pivot;
    } while (1==1);
}

boolean rsacrypto::isPrime(long pivot) {
    if(pivot <= 1)
        return false;
    int root = sqrt((double)pivot);

    //start at 2 because all numbers are divisible by 1
    for(int x = 2; x <= root; x++)  //You only need to check up to and including the root
    {
        if(pivot % x == 0)
            return false;
    }
    return true;
}
int rsacrypto::genrndnum(int a, int b){
    long pivot;
    pivot= rand() % b + a;
    return pivot;
}

int rsacrypto::gcd ( int a, int b )
{
  int c;
  while ( a != 0 ) {
     c = a; a = b%a;  b = c;
  }
  return b;
}

void main()
{
    rsacrypto m;
    system("pause");
}

But I would like to make this code work for hexa decimal values. I don't know how to do that. I'm not a programming expert. Any help would be sinscierly appreciated. Thankyou.

user1658435
  • 574
  • 1
  • 9
  • 28

1 Answers1

0

I guess your problem is to transfer the double values (hex decimal values) into char values first. Then you can use the existing code to encrypt/decrypt the char values.

There are two ways to convert the double values into char values:

  • Convert each double into two char's as its printable/readable form, e.g., 123.455 -> "123.456";

I refer some code from this discussion:

#include <sstream>

stringstream ss;
ss << myDouble;
const char* str = ss.str().c_str();
ss >> myOtherDouble;
  • Convert each double into two char's as its byte form;

Please see this discussion:

Use a union:

union {
  double d[2];
  char b[sizeof(double) * 2];
};

Or reinterpret_cast:

char* b = reinterpret_cast<double*>(d);

Now, after converting your double values into char values, we can directly utilize the existing code to encrypt our data.

Community
  • 1
  • 1
Tao HE
  • 300
  • 1
  • 5