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

  • Home
  • SEARCH
  • 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 8199877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:15:41+00:00 2026-06-07T06:15:41+00:00

I don’t know if I am using this code properly -> main. It is

  • 0

I don’t know if I am using this code properly -> main. It is extremely slow. It is DDS algorithm.
It is implementation of DSA: http://en.wikipedia.org/wiki/Digital_Signature_Algorithm

THIS LOOP TAKES MUCH TIME

do {
            pTemp = new BigInteger(l, primeCenterie, rand);
            pTemp2 = pTemp.subtract(BigInteger.ONE);
            pTemp = pTemp.subtract(pTemp2.remainder(q));
            System.out.println("1 " + i++);
        } while (!pTemp.isProbablePrime(primeCenterie)
                || pTemp.bitLength() != l);

I added some printlns and even them are slow.

 import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Key {
    int primeCenterie = 20;
    BigInteger q;
    BigInteger p;
    BigInteger g;
    BigInteger y;
    BigInteger x;
    BigInteger k;
    Random rand = new Random();

    Key() {
    }

    public void generateKey() {
        q = new BigInteger(160, primeCenterie, rand);
        p = generateP(q, 512);
        g = generateG(p, q);
        do {
            x = new BigInteger(q.bitCount(), rand);
        } while (x.compareTo(BigInteger.ZERO) != 1 && x.compareTo(q) != -1);
        y = g.modPow(x, p);
    }

    private BigInteger generateP(BigInteger q, int l) {
        if (l % 64 != 0) {
            throw new IllegalArgumentException("L value is wrong");
        }
        BigInteger pTemp;
        BigInteger pTemp2;
        int i = 0;
        do {
            pTemp = new BigInteger(l, primeCenterie, rand);
            pTemp2 = pTemp.subtract(BigInteger.ONE);
            pTemp = pTemp.subtract(pTemp2.remainder(q));
            System.out.println("1 " + i++);
        } while (!pTemp.isProbablePrime(primeCenterie)
                || pTemp.bitLength() != l);
        return pTemp;
    }

    private BigInteger generateG(BigInteger p, BigInteger q) {
        BigInteger aux = p.subtract(BigInteger.ONE);
        BigInteger pow = aux.divide(q);
        BigInteger gTemp;
        int i = 0;
        do {
            gTemp = new BigInteger(aux.bitLength(), rand);
            System.out.println("2 " + i++);
        } while (gTemp.compareTo(aux) != -1
                && gTemp.compareTo(BigInteger.ONE) != 1);
        return gTemp.modPow(pow, p);
    }

    public BigInteger generateK(BigInteger q) {
        BigInteger tempK;
        int i = 0;
        do {
            tempK = new BigInteger(q.bitLength(), rand);
            System.out.println("3 " + i++);
        } while (tempK.compareTo(q) != -1
                && tempK.compareTo(BigInteger.ZERO) != 1);
        return tempK;
    }

    public BigInteger generateR() {
        k = generateK(q);
        BigInteger r = g.modPow(k, p).mod(q);
        return r;
    }

    public BigInteger generateS(BigInteger r, byte[] data) {
        MessageDigest md;
        BigInteger s = BigInteger.ONE;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(data);
            BigInteger hash = new BigInteger(md.digest());
            s = (k.modInverse(q).multiply(hash.add(x.multiply(r)))).mod(q);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(DSA.class.getName()).log(Level.SEVERE, null, ex);
        }
        return s;
    }

    boolean verify(byte[] data, BigInteger r, BigInteger s) {
        if (r.compareTo(BigInteger.ZERO) <= 0 || r.compareTo(q) >= 0) {
            return false;
        }
        if (s.compareTo(BigInteger.ZERO) <= 0 || s.compareTo(q) >= 0) {
            return false;
        }
        MessageDigest md;
        BigInteger v = BigInteger.ZERO;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(data);
            BigInteger hash = new BigInteger(md.digest());
            BigInteger w = s.modInverse(q);
            BigInteger u1 = hash.multiply(w).mod(q);
            BigInteger u2 = r.multiply(w).mod(q);
            v = ((g.modPow(u1, p).multiply(y.modPow(u2, p))).mod(p)).mod(q);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(DSA.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v.compareTo(r) == 0;
    }

    public static void main(String[] args) {
        Key key = new Key();
        key.generateKey();
        byte[] data = "a".getBytes();
        BigInteger r = key.generateR();
        BigInteger s = key.generateS(r, data);
        System.out.println(key.verify(data,r,s)); 
    }

}
  • 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-07T06:15:42+00:00Added an answer on June 7, 2026 at 6:15 am

    A profiler would be best here, but I think your problem is isProbablePrime. You’re passing in 20, which basically means you want a .9999999 certainty. Try a smaller parameter or use other method for primality testing…

    isProbablePrime
    
    public boolean isProbablePrime(int certainty)
    
        Returns true if this BigInteger is probably prime, false if it's definitely composite. If certainty is <= 0, true is returned.
    
        Parameters:
            certainty - a measure of the uncertainty that the caller is willing to tolerate: if the call returns true the probability that this BigInteger is prime exceeds (1 - 1/2certainty). The execution time of this method is proportional to the value of this parameter. 
        Returns:
            true if this BigInteger is probably prime, false if it's definitely composite.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know why, but this code worked for me a month ago... maybe
Don't know why this is happening, but after submitting a form via JS (using
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
don't know better title for this, but here's my code. I have class user
Don't know how to frase this but I found this code wich works as
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know if anyone can help me with this or if it's even possible.
Don't know why people do not practice AJAX implementation for authentication systems. Is it
I don't know: if this works. if it's a good idea. what it is

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.