Create a gist now

Instantly share code, notes, and snippets.

@asanso /MSDBRG
Last active Oct 10, 2017

What would you like to do?
package gist;
import java.math.BigInteger;
public class MSDBRG {
private static BigInteger TWO = new BigInteger("2");
public static void main(String[] args) {
BigInteger bk = TWO.pow(256);
BigInteger bkoutput = TWO.pow(384);
BigInteger bk128 = TWO.pow(128);
BigInteger c1 = new BigInteger("297");
BigInteger c2 = new BigInteger("357");
BigInteger p = bk.add(c1);
BigInteger q = bk.add(c2);
BigInteger n = p.multiply(q);
BigInteger e = new BigInteger("7");
BigInteger random = new BigInteger(128, new SecureRandom());
BigInteger ct = random.modPow(e, n);
BigInteger y = ct.mod(bkoutput);
BigInteger x = ct.divide(bkoutput);
System.out.println("output of the generator is "+ y.toString(2));
System.out.println("internal state of the generator is "+ x.toString(2));
String ys = y.toString(2);
//taking the 128 most significant bits of the output
BigInteger ym = new BigInteger(ys.substring(0,128),2);
//taking the 256 less significant bits of the output
BigInteger yl = new BigInteger(ys.substring(128),2);
System.out.println("CT mod p is "+ ct.mod(p).toString(2));
System.out.println("CT mod p is "+crandalMod(ym, yl, bk, c1).toString(2));
System.out.println("CT mod p (128 lbt) "+ ct.mod(p).mod(bk128).toString(2));
System.out.println("CT mod p (128 lbt) "+crandalMod(ym, yl, bk, c1).mod(bk128).toString(2));
public static BigInteger crandalMod(BigInteger ym, BigInteger yl, BigInteger bk, BigInteger c) {
BigInteger r = yl;
BigInteger r1 = ym.multiply(c);
r = r.subtract(r1);
r = r.add(c);
return r;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment