Java tutorial
//package com.java2s; import java.math.BigInteger; public class Main { private static BigInteger ONE = BigInteger.valueOf(1); private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) { BigInteger beta = null; if (p.mod(BigInteger.valueOf(4)).compareTo(BigInteger.valueOf(3)) == 0) { BigInteger k = p.shiftRight(2).add(ONE); beta = alpha.modPow(k, p); } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(5)) == 0) { System.out.println("p = 8 mod 5"); BigInteger k = p.subtract(BigInteger.valueOf(5)).divide(BigInteger.valueOf(8)); BigInteger gamma = alpha.multiply(BigInteger.valueOf(2)).modPow(k, p); BigInteger i = alpha.multiply(BigInteger.valueOf(2)).multiply(gamma.pow(2)).mod(p); beta = alpha.multiply(gamma).multiply(i.subtract(ONE)).mod(p); } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(1)) == 0) { beta = null; //TODO System.out.println("finding square root not fully implemented yet"); } return beta; } }