public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, g, x, y; int qLength = param.getStrength() - 1; ElGamalParameters elParams = param.getParameters(); p = elParams.getP(); g = elParams.getG(); // // calculate the private key // x = new BigInteger(qLength, param.getRandom()); // // calculate the public key. // y = g.modPow(x, p); return new AsymmetricCipherKeyPair( new ElGamalPublicKeyParameters(y, elParams), new ElGamalPrivateKeyParameters(x, elParams)); }
private void testInvalidP() { ElGamalParameters dhParams = new ElGamalParameters(pPgpBogusPSamp, gPgpBogusPSamp, lPgpBogusPSamp); ElGamalPublicKeyParameters pu = new ElGamalPublicKeyParameters(yPgpBogusPSamp, dhParams); ElGamalPrivateKeyParameters pv = new ElGamalPrivateKeyParameters(xPgpBogusPSamp, dhParams); ElGamalEngine e = new ElGamalEngine(); e.init(true, pu); byte[] message = Hex.decode("5468697320697320612074657374"); byte[] pText = message; byte[] cText = e.processBlock(pText, 0, pText.length); e.init(false, pv); pText = e.processBlock(cText, 0, cText.length); if (Arrays.areEqual(message, pText)) { fail("invalid test failed"); } }
@Override public AsymmetricCipherKeyPairGenerator createAsymmetricCipherKeyPairGenerator(final boolean initWithDefaults) { final ElGamalKeyPairGenerator generator = new ElGamalKeyPairGenerator(); if (initWithDefaults) { /* * How certain do we want to be that the chosen primes are really primes. * <p> * The higher this number, the more tests are done to make sure they are primes (and not composites). * <p> * See: <a href="http://crypto.stackexchange.com/questions/3114/what-is-the-correct-value-for-certainty-in-rsa-key-pair-generation">What is the correct value for “certainty” in RSA key pair generation?</a> * and * <a href="http://crypto.stackexchange.com/questions/3126/does-a-high-exponent-compensate-for-a-low-degree-of-certainty?lq=1">Does a high exponent compensate for a low degree of certainty?</a> */ final int certainty = 12; final SecureRandom random = new SecureRandom(); ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); pGen.init(4096, certainty, random); ElGamalParameters elGamalParameters = pGen.generateParameters(); generator.init(new ElGamalKeyGenerationParameters(random, elGamalParameters)); } return generator; }
private ElGamalKeyGenerationParameters createElGamalKeyGenerationParameters(final CreatePgpKeyParam createPgpKeyParam) { /* * How certain do we want to be that the chosen primes are really primes. * <p> * The higher this number, the more tests are done to make sure they are primes (and not composites). * <p> * See: <a href="http://crypto.stackexchange.com/questions/3114/what-is-the-correct-value-for-certainty-in-rsa-key-pair-generation">What is the correct value for “certainty” in RSA key pair generation?</a> * and * <a href="http://crypto.stackexchange.com/questions/3126/does-a-high-exponent-compensate-for-a-low-degree-of-certainty?lq=1">Does a high exponent compensate for a low degree of certainty?</a> */ final int certainty = 8; // 12 takes ages - and DSA+El-Gamal is anyway a bad idea and discouraged. Reducing this to make it bearable. final SecureRandom random = getSecureRandom(); ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); pGen.init(createPgpKeyParam.getStrength(), certainty, random); ElGamalParameters elGamalParameters = pGen.generateParameters(); // Maybe we should generate our "DH safe primes" only once and store them somewhere? Or maybe we should provide a long list // of them in the resources? DHParametersHelper.generateSafePrimes(size, certainty, random); takes really really very long. // BUT BEWARE: Attacks on El Gamal can re-use expensively calculated stuff, if p (one of the "safe primes) is the same. // However, it is still not *so* easy. Hmmm... don't know. Security is really important here. return new ElGamalKeyGenerationParameters(random, elGamalParameters); }
/** * which generates the p and g values from the given parameters, * returning the ElGamalParameters object. * <p> * Note: can take a while... */ public ElGamalParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random); BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g = DHParametersHelper.selectGenerator(p, q, random); return new ElGamalParameters(p, g); }
public AsymmetricCipherKeyPair generateKeyPair() { DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE; ElGamalParameters egp = param.getParameters(); DHParameters dhp = new DHParameters(egp.getP(), egp.getG(), null, egp.getL()); BigInteger x = helper.calculatePrivate(dhp, param.getRandom()); BigInteger y = helper.calculatePublic(dhp, x); return new AsymmetricCipherKeyPair( new ElGamalPublicKeyParameters(y, egp), new ElGamalPrivateKeyParameters(x, egp)); }
protected AlgorithmParameters engineGenerateParameters() { ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); if (random != null) { pGen.init(strength, 20, random); } else { pGen.init(strength, 20, new SecureRandom()); } ElGamalParameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = AlgorithmParameters.getInstance("ElGamal", BouncyCastleProvider.PROVIDER_NAME); params.init(new DHParameterSpec(p.getP(), p.getG(), l)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; }
static public AsymmetricKeyParameter generatePublicKeyParameter( PublicKey key) throws InvalidKeyException { if (key instanceof ElGamalPublicKey) { ElGamalPublicKey k = (ElGamalPublicKey)key; return new ElGamalPublicKeyParameters(k.getY(), new ElGamalParameters(k.getParams().getP(), k.getParams().getG())); } throw new InvalidKeyException("can't identify ElGamal public key."); }
static public AsymmetricKeyParameter generatePrivateKeyParameter( PrivateKey key) throws InvalidKeyException { if (key instanceof ElGamalPrivateKey) { ElGamalPrivateKey k = (ElGamalPrivateKey)key; return new ElGamalPrivateKeyParameters(k.getX(), new ElGamalParameters(k.getParams().getP(), k.getParams().getG())); } throw new InvalidKeyException("can't identify ElGamal private key."); }
protected AlgorithmParameters engineGenerateParameters() { ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); if ( random != null ) { pGen.init(strength, 20, random); } else { pGen.init(strength, 20, new SecureRandom()); } ElGamalParameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = AlgorithmParameters.getInstance("ElGamal", BouncyCastleProvider.PROVIDER_NAME); params.init(new ElGamalParameterSpec(p.getP(), p.getG())); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; }
protected AlgorithmParameters engineGenerateParameters() { ElGamalParametersGenerator pGen = new ElGamalParametersGenerator(); if (random != null) { pGen.init(strength, 20, random); } else { pGen.init(strength, 20, new SecureRandom()); } ElGamalParameters p = pGen.generateParameters(); AlgorithmParameters params; try { params = createParametersInstance("ElGamal"); params.init(new DHParameterSpec(p.getP(), p.getG(), l)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return params; }
@Override public byte[] encryptWithPublicKey(byte[] data) { ElGamalParameters params = new ElGamalParameters(keys.getP(), keys.getG()); ElGamalPublicKeyParameters pubKey = new ElGamalPublicKeyParameters(keys.getPublicKey(), params); ElGamalEngine e = new ElGamalEngine(); e.init(true, pubKey); return e.processBlock(data, 0, data.length) ; }
public ElGamalEncrypt encryptForContract(byte[] data) { ElGamalParameters params = new ElGamalParameters(keys.getP(), keys.getG()); ElGamalPublicKeyParameters pubKey = new ElGamalPublicKeyParameters(keys.getPublicKey(), params); ElGamalEngine e = new ElGamalEngine(); e.init(true, pubKey); byte[] m = e.processBlock(data, 0, data.length); BigInteger k = e.getK(); BigInteger u = keys.getG().modPow(k,keys.getP()); BigInteger v = (keys.getPublicKey().modPow(e.getK(), keys.getP()).multiply(new BigInteger(data))); return new ElGamalEncrypt (u,v,k, m); }
@Override public byte[] decryptWithPrivateKey(byte[] data) { ElGamalParameters params = new ElGamalParameters(keys.getP(), keys.getG()); ElGamalPrivateKeyParameters privKey = new ElGamalPrivateKeyParameters(keys.getPrivateKey(), params); ElGamalEngine e = new ElGamalEngine(); e.init(false, privKey); return e.processBlock(data, 0, data.length) ; }
/** * This method is used to generate Public Key and Private Key * @param params */ private void GenerateKeys(ElGamalParameters params, String password){ ElGamalKeyGenerationParameters elGP = new ElGamalKeyGenerationParameters(random,params); ElGamalKeyPairGenerator KeyPair = new ElGamalKeyPairGenerator(); KeyPair.init(elGP); AsymmetricCipherKeyPair cipher1 = KeyPair.generateKeyPair(); publicKey = ((ElGamalPublicKeyParameters) cipher1.getPublic()).getY(); privateKey = ((ElGamalPrivateKeyParameters)cipher1.getPrivate()).getX(); wellGenerated = true; encryptPrivateKey(password); }
@Override public boolean generate(String password) { ElGamalParameters params; params = GeneratePG(); GenerateKeys(params, password); return wellGenerated; }
/** * which generates the p and g values from the given parameters, * returning the ElGamalParameters object. * <p> * Note: can take a while... */ public ElGamalParameters generateParameters() { BigInteger g, p, q; int qLength = size - 1; // // find a safe prime p where p = 2*q + 1, where p and q are prime. // for (;;) { q = new BigInteger(qLength, 1, random); if (q.bitLength() != qLength) { continue; } if (!q.isProbablePrime(certainty)) { continue; } p = q.multiply(TWO).add(ONE); if (p.isProbablePrime(certainty)) { break; } } // // calculate the generator g - the advantage of using the 2q+1 // approach is that we know the prime factorisation of (p - 1)... // for (;;) { g = new BigInteger(qLength, random); if (g.modPow(TWO, p).equals(ONE)) { continue; } if (g.modPow(q, p).equals(ONE)) { continue; } break; } return new ElGamalParameters(p, g); }
public void generateTest() throws Exception { char[] passPhrase = "hello".toCharArray(); DSAParametersGenerator dsaPGen = new DSAParametersGenerator(); dsaPGen.init(512, 10, new SecureRandom()); DSAKeyPairGenerator dsaKpg = new DSAKeyPairGenerator(); dsaKpg.init(new DSAKeyGenerationParameters(new SecureRandom(), dsaPGen.generateParameters())); // // this takes a while as the key generator has to generate some DSA params // before it generates the key. // AsymmetricCipherKeyPair dsaKp = dsaKpg.generateKeyPair(); ElGamalKeyPairGenerator elgKpg = new ElGamalKeyPairGenerator(); BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16); BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16); ElGamalParameters elParams = new ElGamalParameters(p, g); elgKpg.init(new ElGamalKeyGenerationParameters(new SecureRandom(), elParams)); // // this is quicker because we are using pregenerated parameters. // AsymmetricCipherKeyPair elgKp = elgKpg.generateKeyPair(); PGPKeyPair dsaKeyPair = new BcPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); PGPKeyPair elgKeyPair = new BcPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, "test", null, null, null, new BcPGPContentSignerBuilder(PGPPublicKey.DSA, HashAlgorithmTags.SHA1), new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256).build(passPhrase)); keyRingGen.addSubKey(elgKeyPair); PGPSecretKeyRing keyRing = keyRingGen.generateSecretKeyRing(); keyRing.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase)); PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing(); PGPPublicKey vKey = null; PGPPublicKey sKey = null; Iterator it = pubRing.getPublicKeys(); while (it.hasNext()) { PGPPublicKey pk = (PGPPublicKey)it.next(); if (pk.isMasterKey()) { vKey = pk; } else { sKey = pk; } } Iterator sIt = sKey.getSignatures(); while (sIt.hasNext()) { PGPSignature sig = (PGPSignature)sIt.next(); if (sig.getKeyID() == vKey.getKeyID() && sig.getSignatureType() == PGPSignature.SUBKEY_BINDING) { sig.init(new BcPGPContentVerifierBuilderProvider(), vKey); if (!sig.verifyCertification(vKey, sKey)) { fail("failed to verify sub-key signature."); } } } }
public void generateSha1Test() throws Exception { char[] passPhrase = "hello".toCharArray(); DSAParametersGenerator dsaPGen = new DSAParametersGenerator(); dsaPGen.init(512, 10, new SecureRandom()); DSAKeyPairGenerator dsaKpg = new DSAKeyPairGenerator(); dsaKpg.init(new DSAKeyGenerationParameters(new SecureRandom(), dsaPGen.generateParameters())); // // this takes a while as the key generator has to generate some DSA params // before it generates the key. // AsymmetricCipherKeyPair dsaKp = dsaKpg.generateKeyPair(); ElGamalKeyPairGenerator elgKpg = new ElGamalKeyPairGenerator(); BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16); BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16); ElGamalParameters elParams = new ElGamalParameters(p, g); elgKpg.init(new ElGamalKeyGenerationParameters(new SecureRandom(), elParams)); // // this is quicker because we are using pregenerated parameters. // AsymmetricCipherKeyPair elgKp = elgKpg.generateKeyPair(); PGPKeyPair dsaKeyPair = new BcPGPKeyPair(PGPPublicKey.DSA, dsaKp, new Date()); PGPKeyPair elgKeyPair = new BcPGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elgKp, new Date()); PGPDigestCalculator chkSumCalc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1); PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaKeyPair, "test", chkSumCalc, null, null, new BcPGPContentSignerBuilder(PGPPublicKey.DSA, HashAlgorithmTags.SHA1), new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256).build(passPhrase)); keyRingGen.addSubKey(elgKeyPair); PGPSecretKeyRing keyRing = keyRingGen.generateSecretKeyRing(); keyRing.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase)); PGPPublicKeyRing pubRing = keyRingGen.generatePublicKeyRing(); PGPPublicKey vKey = null; PGPPublicKey sKey = null; Iterator it = pubRing.getPublicKeys(); while (it.hasNext()) { PGPPublicKey pk = (PGPPublicKey)it.next(); if (pk.isMasterKey()) { vKey = pk; } else { sKey = pk; } } Iterator sIt = sKey.getSignatures(); while (sIt.hasNext()) { PGPSignature sig = (PGPSignature)sIt.next(); if (sig.getKeyID() == vKey.getKeyID() && sig.getSignatureType() == PGPSignature.SUBKEY_BINDING) { sig.init(new BcPGPContentVerifierBuilderProvider(), vKey); if (!sig.verifyCertification(vKey, sKey)) { fail("failed to verify sub-key signature."); } } } }