step1: D:\workspace\DsaDemo\app\src\main\java\com\mondor\dsademo\MainActivity.java
package com.mondor.dsademo;
import androidx.appcompat.app.AppCompatActivity;
import java.math.BigInteger;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String message = "Hi I think I have an A";
String message2 = "Hi I think I have an A";
Session session = Session.getInstance(true);
Pair<BigInteger, BigInteger> privateKeys = session.getPrivateKey();
Pair<BigInteger, BigInteger> sign = DSA.sign(true, message,
session.getGlobalKeyG(), session.getGlobalKeyP(),
session.getGlobalKeyQ(), privateKeys.getFirst());
boolean isPass = DSA.verify(true, message2, sign.getFirst(),
sign.getSecond(), session.getGlobalKeyG(),
session.getGlobalKeyP(), session.getGlobalKeyQ(),
privateKeys.getSecond());
session.destroy();
}
}
step2: D:\workspace\DsaDemo\app\src\main\java\com\mondor\dsademo\DSA.java
package com.mondor.dsademo;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;
public class DSA {
private static boolean debug;
public static Pair<BigInteger, BigInteger> sign(boolean deb,
String message, BigInteger publcG, BigInteger publicP,
BigInteger publicQ, BigInteger privateX) {
debug = deb;
debugMode("=== CREATING SIGNATURE ===", true);
debugMode("Creating auxiliar variable K .......... ", false);
BigInteger k = new BigInteger(publicQ.bitLength(), new SecureRandom());
while (k.compareTo(publicQ) != -1 && k.compareTo(BigInteger.ZERO) != 1) {
k = new BigInteger(publicQ.bitLength(), new SecureRandom());
}
debugMode("[OK]", true);
debugMode("Creating R .......... ", false);
BigInteger r = publcG.modPow(k, publicP).mod(publicQ);
debugMode("[OK]", true);
debugMode("Creating S .......... ", false);
MessageDigest md = null;
BigInteger s = BigInteger.ONE;
try {
md = MessageDigest.getInstance("SHA-1");
md.update(message.getBytes());
BigInteger hash = new BigInteger(md.digest());
s = (k.modInverse(publicQ).multiply(hash.add(privateX.multiply(r))))
.mod(publicQ);
} catch (Exception e) {
e.printStackTrace();
}
debugMode("[OK]", true);
Pair<BigInteger, BigInteger> result = new Pair<BigInteger, BigInteger>(
r, s);
return result;
}
public static Boolean verify(boolean deb, String message, BigInteger r,
BigInteger s, BigInteger publcG, BigInteger publicP,
BigInteger publicQ, BigInteger privateY) {
debugMode("=== VERIFYING SIGNATURE ===", true);
debug = deb;
MessageDigest md;
BigInteger v = BigInteger.ZERO;
try {
md = MessageDigest.getInstance("SHA-1");
md.update(message.getBytes());
BigInteger messagehash = new BigInteger(md.digest());
debugMode("Creating W .......... ", false);
BigInteger w = s.modInverse(publicQ);
debugMode("[OK]", true);
debugMode("Creating U1 .......... ", false);
BigInteger u1 = messagehash.multiply(w).mod(publicQ);
debugMode("[OK]", true);
debugMode("Creating U2 .......... ", false);
BigInteger u2 = r.multiply(w).mod(publicQ);
debugMode("[OK]", true);
debugMode("Creating V .......... ", false);
v = ((publcG.modPow(u1, publicP).multiply(privateY.modPow(u2,
publicP))).mod(publicP)).mod(publicQ);
debugMode("[OK]", true);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return v.compareTo(r) == 0;
}
private static void debugMode(String s, boolean ln) {
if (debug) {
if (ln) {
System.out.println(s);
} else {
System.out.print(s);
}
}
}
}
step3: D:\workspace\DsaDemo\app\src\main\java\com\mondor\dsademo\Pair.java
package com.mondor.dsademo;
public class Pair<F, S> {
private F first;
private S second;
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
}
step4: D:\workspace\DsaDemo\app\src\main\java\com\mondor\dsademo\Session.java
package com.mondor.dsademo;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
public class Session {
private static Session session;
private static boolean debug;
private BigInteger globalKeyP;
private BigInteger globalKeyQ;
private BigInteger globalKeyG;
private static int L = 512;
public static Session getInstance(boolean debugMode) {
if (session == null)
session = new Session(debugMode);
return session;
}
private Session(boolean b) {
if (b) {
debug = true;
} else {
debug = false;
}
debugMode("=== CREATION OF GLOBAL KEYS ===", true);
debugMode("Creating global key Q .......... ", false);
globalKeyQ = new BigInteger(160, 20, new SecureRandom());
debugMode("[OK]", true);
debugMode("Creating global key P (be patient).......... ", false);
BigInteger tempP;
BigInteger tempP2;
SecureRandom rand = new SecureRandom();
do {
tempP = new BigInteger(L, 20, rand);
tempP2 = tempP.subtract(BigInteger.ONE);
tempP = tempP.subtract(tempP2.remainder(globalKeyQ));
} while (!tempP.isProbablePrime(20) || tempP.bitLength() != L);
BigInteger p = tempP;
globalKeyP = p;
debugMode("[OK]", true);
debugMode("Creating global key G .......... ", false);
BigInteger p1 = globalKeyP.subtract(BigInteger.ONE);
BigInteger exp = p1.divide(globalKeyQ);
BigInteger tempg;
Random random = new Random();
do {
tempg = new BigInteger(p1.bitLength(), random);
} while (tempg.compareTo(p1) != -1
&& tempg.compareTo(BigInteger.ONE) != 1);
globalKeyG = tempg.modPow(exp, p);
debugMode("[OK]", true);
System.out.println("");
System.out.println("Q: " + globalKeyQ);
System.out.println("P: " + globalKeyP);
System.out.println("G: " + globalKeyG);
}
public BigInteger getGlobalKeyP() {
return globalKeyP;
}
public BigInteger getGlobalKeyQ() {
return globalKeyQ;
}
public BigInteger getGlobalKeyG() {
return globalKeyG;
}
private void debugMode(String s, boolean ln) {
if (debug) {
if (ln) {
System.out.println(s);
} else {
System.out.print(s);
}
}
}
public Pair<BigInteger, BigInteger> getPrivateKey() {
debugMode("=== CREATION OF PRIVATE KEY ===", true);
debugMode("Creating private key X .......... ", false);
BigInteger privK = new BigInteger(getGlobalKeyQ().bitLength(),
new SecureRandom());
while (privK.compareTo(globalKeyQ) != -1) {
privK = new BigInteger(getGlobalKeyQ().bitLength(),
new SecureRandom());
}
debugMode("[OK]", true);
debugMode("Creating private key Y .......... ", false);
BigInteger pubK = getGlobalKeyG().modPow(privK, getGlobalKeyP());
Pair<BigInteger, BigInteger> result = new Pair<BigInteger, BigInteger>(
privK, pubK);
debugMode("[OK]", true);
return result;
}
public void destroy() {
debugMode("=== DESTROYING SESSION ===", true);
globalKeyP = null;
globalKeyQ = null;
globalKeyG = null;
session = null;
debugMode("Session destroyed", true);
}
}
end
|