fork download
  1. import java.util.*;
  2. import java.math.BigInteger;
  3. class dsaAlg
  4. {
  5. final static BigInteger one = new BigInteger("1");
  6. final static BigInteger zero = new BigInteger("0");
  7. public static BigInteger getNextPrime(String ans)
  8. {
  9. BigInteger test = new BigInteger(ans);
  10. while (!test.isProbablePrime(99))
  11. e:
  12. {
  13. test = test.add(one);
  14. }
  15. return test;
  16. }
  17. public static BigInteger findQ(BigInteger n)
  18. {
  19. BigInteger start = new BigInteger("2");
  20. while (!n.isProbablePrime(99))
  21. {
  22. while (!((n.mod(start)).equals(zero)))
  23. {
  24. start = start.add(one);
  25. }
  26. n = n.divide(start);
  27. }
  28. return n;
  29. }
  30. public static BigInteger getGen(BigInteger p, BigInteger q,Random r)
  31. {
  32. BigInteger h = new BigInteger(p.bitLength(), r);
  33. h = h.mod(p);
  34. return h.modPow((p.subtract(one)).divide(q), p);
  35. }
  36. public static void main (String[] args) throws
  37. java.lang.Exception
  38. {
  39. Random randObj = new Random();
  40. BigInteger p = getNextPrime("10600"); /* approximate
  41. prime */
  42. BigInteger q = findQ(p.subtract(one));
  43. BigInteger g = getGen(p,q,randObj);
  44. System.out.println(" \n simulation of Digital Signature Algorithm \n");
  45. System.out.println(" \n global public key components are:\n");
  46. System.out.println("\np is: " + p);
  47. System.out.println("\nq is: " + q);
  48. System.out.println("\ng is: " + g);
  49. BigInteger x = new BigInteger(q.bitLength(), randObj);
  50. x = x.mod(q);
  51. BigInteger y = g.modPow(x,p);
  52. BigInteger k = new BigInteger(q.bitLength(), randObj);
  53. k = k.mod(q);
  54. BigInteger r = (g.modPow(k,p)).mod(q);
  55. BigInteger hashVal = new BigInteger(p.bitLength(),randObj);
  56. BigInteger kInv = k.modInverse(q);
  57. BigInteger s = kInv.multiply(hashVal.add(x.multiply(r)));
  58. s = s.mod(q);
  59. System.out.println("\nsecret information are:\n");
  60. System.out.println("x (private) is:" + x);
  61. System.out.println("k (secret) is: " + k);
  62. System.out.println("y (public) is: " + y);
  63. System.out.println("h (rndhash) is: " + hashVal);
  64. System.out.println("\n generating digital signature:\n");
  65. System.out.println("r is : " + r);
  66. System.out.println("s is : " + s);
  67. BigInteger w = s.modInverse(q);
  68. BigInteger u1 = (hashVal.multiply(w)).mod(q);
  69. BigInteger u2 = (r.multiply(w)).mod(q);
  70. BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
  71. v = (v.mod(p)).mod(q);
  72. System.out.println("\nverifying digital signature (checkpoints)\n:");
  73. System.out.println("w is : " + w);
  74.  
  75. System.out.println("u1 is : " + u1);
  76. System.out.println("u2 is : " + u2);
  77. System.out.println("v is : " + v);
  78. if (v.equals(r))
  79. {
  80. System.out.println("\nsuccess: digital signature is verified!\n " + r);
  81. }
  82. else
  83. {
  84. System.out.println("\n error: incorrect digital signature\n ");
  85. }
  86. }
  87. }
  88.  
Success #stdin #stdout 0.12s 36676KB
stdin
Standard input is empty
stdout
 
 simulation of Digital Signature Algorithm 

 
 global public key components are:


p is: 10601

q is: 53

g is: 1672

secret information are:

x (private) is:28
k (secret) is: 17
y (public) is: 566
h (rndhash) is: 372

 generating digital signature:

r is : 36
s is : 50

verifying digital signature (checkpoints)
:
w is : 35
u1 is : 35
u2 is : 41
v is : 36

success: digital signature is verified!
 36