Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:57:17+00:00 2026-06-07T00:57:17+00:00

I am trying to implement a ElGamal-like crypto algorithm using Java’s BigInteger objects. q

  • 0

I am trying to implement a ElGamal-like crypto algorithm using Java’s BigInteger objects.

  • q is a safe prime in the form 2p+1
  • g is a generator of the group
    Zq

I want to calculate calculate g^r from g^(xr) and x but i am having troubles. Using modInverse i can calculate 1/x but if i use this value with modPow i only get wrong results.

The only Example i found on the web is this one where the author uses modInverse to calculate 1/x:

        BigInteger temp = c1.modPow(a,p);
        temp = temp.modInverse(p);

        // Print this out.
        System.out.println("Here is c1^ -a = "+temp);

I tried some variants (including using modPow with -1) but just cannot get it to work. I think the math should be right but any help is appreciated.

Here is my code:

final static BigInteger q = new BigInteger("179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624225795083");
final static BigInteger p = new BigInteger("89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112897541");
final static BigInteger g = new BigInteger("117265744532406309959187881490003058805548219220442880294934902019840205433866971629230940840348591638390822573295684678850519428432938503385192533090834775615734759306193531798190548626342600942782601381215002354918333367595380233608085319759193895027739039963819751637948789055533978566423454988608037601806");

/**
 * @param args
 */
public static void main(String[] args) {
    BigInteger x = new BigInteger("1143167411333064507035595976576260123572705969224418468247407610494944119131645169381885774886951623439260024159767473519706771572117243833759909829897948112642480886709322424314787175230081859236165044801596619590783556439791012887937120324676147585272259948372265307207312838134079528284932292492131276823586631161241002772401238870376093826305673839039010423270418706970005486897400");
    BigInteger r = new BigInteger("28622599320501138892999789676320846139720948572640603818980549097364886339367");

    // g^(xr) = g^(x*r)
    BigInteger g_xr = g.modPow(x.multiply(r), q);

    // 1/x
    BigInteger x_inverse = x.modInverse(q);
    System.out.println(x.multiply(x_inverse).mod(q)); // -> 1 --> correct

    // g^r = g^(xr) ^ (1/x)
    BigInteger g_r = g_xr.modPow(x_inverse, q); // FIXME: wrong result

    System.out.println(g_r); //result
    System.out.println(g.modPow(r, q)); // expected result
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:57:18+00:00Added an answer on June 7, 2026 at 12:57 am

    The multiplicative group \mathbb Z^*_q is of order q−1 — that is to say, gq−1 = 1 mod q. Thus, what you need to find is not the inverse of x modulo q, but modulo q−1.

    (Also, user1008646 is right that gxr = (gx)r ≠ gxgr = gx+r.)


    Edit: To summarize the discussion below, the paper describing the algorithm the OP is implementing has a typo: instead of working in \mathbb Z^*_q, he needs to work in the order-p subgroup of it. (Also, the paper is using p and q in the opposite sense to the way the OP is using them here.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to implement a destructor for the objects of linked-list Iv created. I
Trying to implement an autocomplete based on this It looks like it's very straight
Trying to implement search with Sunspot Gem wich is using Solr.Fulltext search works fine
Trying to implement clean URLs (without .form, .do, etc.) with Spring MVC 3.0 (actually
I'm trying implement a way to recursively template using jsRender. The issue is, my
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
I have this weird kind of error. I am trying implement basic Euclidean algorithm
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.