/** * which generates the p and g values from the given parameters, returning * the CramerShoupParameters object. * <p> * Note: can take a while... * </p> */ public CramerShoupParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = ParametersHelper.generateSafePrimes(size, certainty, random); // BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g1 = ParametersHelper.selectGenerator(q, random); BigInteger g2 = ParametersHelper.selectGenerator(q, random); while (g1.equals(g2)) { g2 = ParametersHelper.selectGenerator(q, random); } return new CramerShoupParameters(q, g1, g2, new SHA256Digest()); }
private CramerShoupCiphertext encrypt(BigInteger message, String label) { CramerShoupKeyPairGenerator kpGen = new CramerShoupKeyPairGenerator(); CramerShoupParametersGenerator pGen = new CramerShoupParametersGenerator(); pGen.init(2048, 1, RND); CramerShoupParameters params = pGen.generateParameters(DHStandardGroups.rfc3526_2048); CramerShoupKeyGenerationParameters param = new CramerShoupKeyGenerationParameters(RND, params); kpGen.init(param); keyPair = kpGen.generateKeyPair(); CramerShoupCoreEngine engine = new CramerShoupCoreEngine(); if (label != null) { engine.init(true, keyPair.getPublic(), label); } else { engine.init(true, keyPair.getPublic()); } CramerShoupCiphertext ciphertext = engine.encryptBlock(message); return ciphertext; }
public AsymmetricCipherKeyPair generateKeyPair() { CramerShoupParameters csParams = param.getParameters(); CramerShoupPrivateKeyParameters sk = generatePrivateKey(param.getRandom(), csParams); CramerShoupPublicKeyParameters pk = calculatePublicKey(csParams, sk); sk.setPk(pk); return new AsymmetricCipherKeyPair(pk, sk); }
private CramerShoupPrivateKeyParameters generatePrivateKey(SecureRandom random, CramerShoupParameters csParams){ BigInteger p = csParams.getP(); CramerShoupPrivateKeyParameters key = new CramerShoupPrivateKeyParameters(csParams, generateRandomElement(p, random), generateRandomElement(p, random), generateRandomElement(p, random), generateRandomElement(p, random), generateRandomElement(p, random)); return key; }
private CramerShoupPublicKeyParameters calculatePublicKey(CramerShoupParameters csParams, CramerShoupPrivateKeyParameters sk) { BigInteger g1 = csParams.getG1(); BigInteger g2 = csParams.getG2(); BigInteger p = csParams.getP(); BigInteger c = g1.modPow(sk.getX1(), p).multiply(g2.modPow(sk.getX2(), p)); BigInteger d = g1.modPow(sk.getY1(), p).multiply(g2.modPow(sk.getY2(), p)); BigInteger h = g1.modPow(sk.getZ(), p); return new CramerShoupPublicKeyParameters(csParams, c, d, h); }
public CramerShoupParameters generateParameters(DHParameters dhParams) { BigInteger p = dhParams.getP(); BigInteger g1 = dhParams.getG(); // now we just need a second generator BigInteger g2 = ParametersHelper.selectGenerator(p, random); while (g1.equals(g2)) { g2 = ParametersHelper.selectGenerator(p, random); } return new CramerShoupParameters(p, g1, g2, new SHA256Digest()); }