import java.math.*; public class DH { public static void main(String arg[]) { try { BigInteger p = new BigInteger(arg[0]); BigInteger g = new BigInteger(arg[1]); BigInteger a = new BigInteger(arg[2]); BigInteger b = new BigInteger(arg[3]); BigInteger x = g.pow(a.intValue()); System.out.println(g+"^"+a+" = "+x); x = g.modPow(a, p); System.out.println(g+"^"+a+" mod "+p+" = "+x); BigInteger y = x.pow(b.intValue()); System.out.println("("+g+"^"+a+" mod "+p+")^"+b+" = "+y); x = x.modPow(b, p); System.out.println("("+g+"^"+a+" mod "+p+")^"+b+" mod "+p+" = "+x); System.out.println(); x = g.pow(b.intValue()); System.out.println(g+"^"+b+" = "+x); x = g.modPow(b, p); System.out.println(g+"^"+b+" mod "+p+" = "+x); y = x.pow(b.intValue()); System.out.println("("+g+"^"+b+" mod "+p+")^"+a+" = "+y); x = x.modPow(a, p); System.out.println("("+g+"^"+b+" mod "+p+")^"+a+" mod "+p+" = "+x); } catch (Exception e) { System.out.println("Usage: java DH p g a b"); } } }