Hi All,

I have a java code of goldwasser-micali encryption (attached), and I need 
your help to implement it in android studio. can anybody help me?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/3fb98097-bd94-418f-99e4-dd841874f74a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package publickeyalgorithms;

import java.math.BigInteger;
import java.security.SecureRandom;

public class GoldwasserMicali {

	/**
	 * SYSTEM PARAMETERS
	 */
	//Bitsize
	private int bitSize;
	
	/**
	 * KEYS
	 */
	//Private key
	private BigInteger p,q;
	//Public key
	private BigInteger x,n;
	
	//Random number generator
	public SecureRandom rand;
	
	/**
	 * Constructor generating key parameters
	 * @param bitSize: size in bits of p and q, this number must be a power of 2, for 1024 security bitSize must be 512.
	 */
	public GoldwasserMicali(int bitSize){
		this.bitSize=bitSize;
		this.rand=new SecureRandom();
		generateKeys();
	}
	/**
	 * Key parameters of Benaloh's cryptosystem 
	 */
	public void generateKeys(){
		BigInteger one=BigInteger.ONE,two=new BigInteger("2");
		p=randomNumber(true,bitSize);
		do{
			q=randomNumber(true,bitSize);
		}while(p.equals(q));
		n=p.multiply(q);
		do{
			x=randomNumber(false,bitSize*2);
		}while(x.compareTo(n)>=0 || x.modPow(p.subtract(one).divide(two), p).equals(one) || x.modPow(q.subtract(one).divide(two), q).equals(one));
	}

	/**
	 * BigInteger random number generator
	 * @param prime true if the number must be prime, false otherwise
	 * @param size: size in bits
	 * @return random number with the criteria selected.
	 */
	private BigInteger randomNumber(boolean prime, int size){
		if(prime)
			return BigInteger.probablePrime(size, rand);
		BigInteger number=null;
		byte bNumber[]=new byte[(int)Math.ceil(size/8.0)];
		do{
			rand.nextBytes(bNumber);
			number=new BigInteger(bNumber);
		}while(number.compareTo(BigInteger.ZERO)<=0);
		return number;
	}
	
	public BigInteger encrypt(boolean bit){
		BigInteger y;
		do{
			y=randomNumber(false, bitSize*2);
		}while(y.compareTo(n)>=0);
		return y.modPow(new BigInteger("2"), n).multiply(bit?x:BigInteger.ONE).remainder(n);
	}
	
	public boolean decrypt(BigInteger c){
		boolean qr;
		qr=c.modPow(p.subtract(BigInteger.ONE).divide(new BigInteger("2")), p).equals(BigInteger.ONE);
		qr&=c.modPow(q.subtract(BigInteger.ONE).divide(new BigInteger("2")), q).equals(BigInteger.ONE);
		return qr;
	}
	
	public static void main(String[] args) {
		String m="101011101000011011111000000110101";
		GoldwasserMicali gm=new GoldwasserMicali(512);
		System.out.println(m);
		for(int i=0;i<m.length();i++){
			BigInteger c=gm.encrypt(m.charAt(i)=='1');
			System.out.print((gm.decrypt(c)?0:1));
		}
	}
}

Reply via email to