Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following expression for calculating the decryption key in a Public Key Encryption System :

d = N-1 mod LCM(p-1,q-1)

where N = p2q.

I wrote the follwing code to find d ,

long long int gendecrypkey(long long int modulo,long long int p,long long int q)
        long long int dk = 0;

        while(!(dk * modulo == fmod(1,lcm((p-1),(q-1)))))
            ++dk ;

        return dk;


but is this the right way of finding d ? If not please suggest a C++ code for finding d.
Upon execution of this function the console wont show anything (blank).What could be the mistake?


sscrypto(long long int &publkey, long long int &privtkey,long long int &pq)
        publickey = publkey =0;
        privatekey = privtkey = 0;
        long long int  p = genrndprimes(50,200);
        long long int q = genrndprimes(200,300);
        long long int n = (p*p)*q; //Modulus.
        long long int d = fmod((1/n),lcm(p-1,q-1)); //Problematic code
        pq = p*q;
        publkey =   publickey = n;
        privtkey = privatekey = d;
Updated 11-Jun-13 17:38pm

1 solution

Wouldn't it be

fmod((1/(p*p*q)), lcm((p-1), (q-1)))


Also, you are getting a blank screen because your while loop never converges, so you are stuck in an infinite loop. I'm also not sure what the "modulo" value is based on the information you gave, so I can't really tell you if that part is right or wrong...
Share this answer
compuknow 11-Jun-13 22:52pm    
Are you asking the modulo in the function signature ? That modulo is p*p*q.
Any way this is a method which i had already tried "fmod((1/p*p*q),lcm((p-1),(q-1)))" but please understand i got 0 as the decryption key (in fact Always , generating p and q at random) , or is it something that I did not understand the formula properly ?
Ron Beyer 11-Jun-13 22:57pm    
What is it that you are really trying to do here? Reverse engineer the private encryption key from a public key? This looks like RSA encryption, and if you are trying to brute force it then I hope you have years of processor time. If you are trying to decrypt data in a public key encryption system, you are supposed to give the public key with the encrypted data, the private key remains private... Not sure what you are really trying to do with the formula...
compuknow 11-Jun-13 23:12pm    
No , I am not a cryptanalyst , infact i am new to cryptography. This is not at all RSA , its Schmidt - Samoa public key encryption , the article is in wikipedia (
I am trying to implement it using C++. According to the article , this is the way i should generate the Private key using the publickey (ie., N itself).
Ron Beyer 11-Jun-13 23:27pm    
Ok, I read that and understand it now, you don't need a loop or anything at all, what you are doing is generating a public key and a private key. The public key (N) is p*p*q where P and Q are "distinct large primes". Its important that they are prime numbers, distinct, and large. You then generate the private key by using d = 1/N mod lcm(p-1, q-1). There should be no looping or modulus.
compuknow 11-Jun-13 23:32pm    
Why do i get a zero if i am going by 1/N mod lcm(p-1,q-1) , i always get zero regardless of what p,q or n are. I tried this earlier.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900