public KeyPair generateKeyPair() { if (!initialised) { param = new RainbowKeyGenerationParameters(random, new RainbowParameters(new RainbowParameterSpec().getVi())); engine.init(param); initialised = true; } AsymmetricCipherKeyPair pair = engine.generateKeyPair(); RainbowPublicKeyParameters pub = (RainbowPublicKeyParameters)pair.getPublic(); RainbowPrivateKeyParameters priv = (RainbowPrivateKeyParameters)pair.getPrivate(); return new KeyPair(new BCRainbowPublicKey(pub), new BCRainbowPrivateKey(priv)); }
/** * Generates a new signature key pair. Runs in a single thread. * * @return a key pair */ public AsymmetricCipherKeyPair generateKeyPairSingleThread() { List<NTRUSigningPrivateKeyParameters.Basis> basises = new ArrayList<NTRUSigningPrivateKeyParameters.Basis>(); NTRUSigningPublicKeyParameters pub = null; for (int k = params.B; k >= 0; k--) { NTRUSigningPrivateKeyParameters.Basis basis = generateBoundedBasis(); basises.add(basis); if (k == 0) { pub = new NTRUSigningPublicKeyParameters(basis.h, params.getSigningParameters()); } } NTRUSigningPrivateKeyParameters priv = new NTRUSigningPrivateKeyParameters(basises, pub); return new AsymmetricCipherKeyPair(pub, priv); }
/** * This function generates a Rainbow key pair. * * @return the generated key pair */ public AsymmetricCipherKeyPair genKeyPair() { RainbowPrivateKeyParameters privKey; RainbowPublicKeyParameters pubKey; if (!initialized) { initializeDefault(); } /* choose all coefficients at random */ keygen(); /* now marshall them to PrivateKey */ privKey = new RainbowPrivateKeyParameters(A1inv, b1, A2inv, b2, vi, layers); /* marshall to PublicKey */ pubKey = new RainbowPublicKeyParameters(vi[vi.length - 1] - vi[0], pub_quadratic, pub_singular, pub_scalar); return new AsymmetricCipherKeyPair(pubKey, privKey); }
public void generateClientKeyExchange(OutputStream output) throws IOException { if (agreementCredentials != null) { return; } AsymmetricCipherKeyPair ecAgreeClientKeyPair = TlsECCUtils.generateECKeyPair(context.getSecureRandom(), ecAgreeServerPublicKey.getParameters()); this.ecAgreeClientPrivateKey = (ECPrivateKeyParameters)ecAgreeClientKeyPair.getPrivate(); byte[] point = TlsECCUtils.serializeECPublicKey(serverECPointFormats, (ECPublicKeyParameters)ecAgreeClientKeyPair.getPublic()); TlsUtils.writeOpaque8(point, output); }
public KeyPair generateKeyPair() { if (!initialised) { DSAParametersGenerator pGen = new DSAParametersGenerator(); pGen.init(strength, certainty, random); param = new DSAKeyGenerationParameters(random, pGen.generateParameters()); engine.init(param); initialised = true; } AsymmetricCipherKeyPair pair = engine.generateKeyPair(); DSAPublicKeyParameters pub = (DSAPublicKeyParameters)pair.getPublic(); DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters)pair.getPrivate(); return new KeyPair(new BCDSAPublicKey(pub), new BCDSAPrivateKey(priv)); }
public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, q, g, x, y; DSAParameters dsaParams = param.getParameters(); SecureRandom random = param.getRandom(); q = dsaParams.getQ(); p = dsaParams.getP(); g = dsaParams.getG(); do { x = new BigInteger(160, random); } while (x.equals(ZERO) || x.compareTo(q) >= 0); // // calculate the public key. // y = g.modPow(x, p); return new AsymmetricCipherKeyPair( new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams)); }
public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, g, x, y; int qLength = param.getStrength() - 1; DHParameters dhParams = param.getParameters(); p = dhParams.getP(); g = dhParams.getG(); // // calculate the private key // x = new BigInteger(qLength, param.getRandom()); // // calculate the public key. // y = g.modPow(x, p); return new AsymmetricCipherKeyPair( new DHPublicKeyParameters(y, dhParams), new DHPrivateKeyParameters(x, dhParams)); }
/** * Given the domain parameters this routine generates an EC key * pair in accordance with X9.62 section 5.2.1 pages 26, 27. */ public AsymmetricCipherKeyPair generateKeyPair() { BigInteger n = params.getN(); int nBitLength = n.bitLength(); BigInteger d; do { d = new BigInteger(nBitLength, random); } while (d.equals(ZERO) || (d.compareTo(n) >= 0)); ECPoint Q = params.getG().multiply(d); return new AsymmetricCipherKeyPair( new ECPublicKeyParameters(Q, params), new ECPrivateKeyParameters(d, params)); }
public static X509CertificateHolder makeV1Certificate(AsymmetricCipherKeyPair subKP, String _subDN, AsymmetricCipherKeyPair issKP, String _issDN) throws IOException, OperatorCreationException { RSAKeyParameters lwPubKey = (RSAKeyParameters)subKP.getPublic(); X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder( new X500Name(_issDN), allocateSerialNumber(), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 100)), new X500Name(_subDN), new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(lwPubKey.getModulus(), lwPubKey.getExponent())) ); AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1WithRSAEncryption"); AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build((AsymmetricKeyParameter)issKP.getPrivate()); return v1CertGen.build(sigGen); }
public static X509CertificateHolder makeCertificate(AsymmetricCipherKeyPair subKP, String _subDN, AsymmetricCipherKeyPair issKP, String _issDN, boolean _ca) throws IOException, OperatorCreationException { RSAKeyParameters lwPubKey = (RSAKeyParameters)subKP.getPublic(); X509v3CertificateBuilder v3CertGen = new X509v3CertificateBuilder( new X500Name(_issDN), allocateSerialNumber(), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 100)), new X500Name(_subDN), new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(lwPubKey.getModulus(), lwPubKey.getExponent())) ); AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1WithRSAEncryption"); AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build((AsymmetricKeyParameter)issKP.getPrivate()); v3CertGen.addExtension( X509Extension.basicConstraints, false, new BasicConstraints(_ca)); return v3CertGen.build(sigGen); }
/************* * This method will create the ECDSA public and private key which is returned in a Byte array of Byte arrays, index 0 is the private key and * index 1 of the array will return the ECDSA public key * * -Wisdom: This guy didn't check whether the returned Private Key would be more than 32. * @return */ private byte[][] ECDSAgeneratePublicAndPrivateKey(){ int length = 0; byte[][] keys; do{ ECKeyPairGenerator gen = new ECKeyPairGenerator(); SecureRandom secureRandom = new SecureRandom(); X9ECParameters secnamecurves = SECNamedCurves.getByName("secp256k1"); ECDomainParameters ecParams = new ECDomainParameters(secnamecurves.getCurve(), secnamecurves.getG(), secnamecurves.getN(), secnamecurves.getH()); ECKeyGenerationParameters keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom); gen.init(keyGenParam); AsymmetricCipherKeyPair kp = gen.generateKeyPair(); ECPrivateKeyParameters privatekey = (ECPrivateKeyParameters)kp.getPrivate(); ECPoint dd = secnamecurves.getG().multiply(privatekey.getD()); byte[] publickey=new byte[65]; System.arraycopy(dd.getY().toBigInteger().toByteArray(), 0, publickey, 64-dd.getY().toBigInteger().toByteArray().length+1, dd.getY().toBigInteger().toByteArray().length); System.arraycopy(dd.getX().toBigInteger().toByteArray(), 0, publickey, 32-dd.getX().toBigInteger().toByteArray().length+1, dd.getX().toBigInteger().toByteArray().length); publickey[0]=4; length = privatekey.getD().toByteArray().length; keys = new byte[][]{privatekey.getD().toByteArray(),publickey}; }while(length != 32); return keys; }
/** * Generates a new signature key pair. Starts <code>B+1</code> threads. * * @return a key pair */ public AsymmetricCipherKeyPair generateKeyPair() { NTRUSigningPublicKeyParameters pub = null; ExecutorService executor = Executors.newCachedThreadPool(); List<Future<NTRUSigningPrivateKeyParameters.Basis>> bases = new ArrayList<Future<NTRUSigningPrivateKeyParameters.Basis>>(); for (int k = params.B; k >= 0; k--) { bases.add(executor.submit(new BasisGenerationTask())); } executor.shutdown(); List<NTRUSigningPrivateKeyParameters.Basis> basises = new ArrayList<NTRUSigningPrivateKeyParameters.Basis>(); for (int k = params.B; k >= 0; k--) { Future<NTRUSigningPrivateKeyParameters.Basis> basis = bases.get(k); try { basises.add(basis.get()); if (k == params.B) { pub = new NTRUSigningPublicKeyParameters(basis.get().h, params.getSigningParameters()); } } catch (Exception e) { throw new IllegalStateException(e); } } NTRUSigningPrivateKeyParameters priv = new NTRUSigningPrivateKeyParameters(basises, pub); AsymmetricCipherKeyPair kp = new AsymmetricCipherKeyPair(pub, priv); return kp; }
public byte[] generateServerKeyExchange() throws IOException { if (this.dhParameters == null) { throw new TlsFatalAlert(AlertDescription.internal_error); } ByteArrayOutputStream buf = new ByteArrayOutputStream(); DHKeyPairGenerator kpg = new DHKeyPairGenerator(); kpg.init(new DHKeyGenerationParameters(context.getSecureRandom(), this.dhParameters)); AsymmetricCipherKeyPair kp = kpg.generateKeyPair(); BigInteger Ys = ((DHPublicKeyParameters)kp.getPublic()).getY(); TlsDHUtils.writeDHParameter(dhParameters.getP(), buf); TlsDHUtils.writeDHParameter(dhParameters.getG(), buf); TlsDHUtils.writeDHParameter(Ys, buf); byte[] digestInput = buf.toByteArray(); Digest d = new CombinedHash(); SecurityParameters securityParameters = context.getSecurityParameters(); d.update(securityParameters.clientRandom, 0, securityParameters.clientRandom.length); d.update(securityParameters.serverRandom, 0, securityParameters.serverRandom.length); d.update(digestInput, 0, digestInput.length); byte[] hash = new byte[d.getDigestSize()]; d.doFinal(hash, 0); byte[] sigBytes = serverCredentials.generateCertificateSignature(hash); /* * TODO RFC 5246 4.7. digitally-signed element needs SignatureAndHashAlgorithm prepended from TLS 1.2 */ TlsUtils.writeOpaque16(sigBytes, buf); return buf.toByteArray(); }
public static AsymmetricCipherKeyPair generateDHKeyPair(SecureRandom random, DHParameters dhParams) { DHBasicKeyPairGenerator dhGen = new DHBasicKeyPairGenerator(); dhGen.init(new DHKeyGenerationParameters(random, dhParams)); return dhGen.generateKeyPair(); }
public static DHPrivateKeyParameters generateEphemeralClientKeyExchange(SecureRandom random, DHParameters dhParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair dhAgreeClientKeyPair = generateDHKeyPair(random, dhParams); DHPrivateKeyParameters dhAgreeClientPrivateKey = (DHPrivateKeyParameters)dhAgreeClientKeyPair .getPrivate(); BigInteger Yc = ((DHPublicKeyParameters)dhAgreeClientKeyPair.getPublic()).getY(); byte[] keData = BigIntegers.asUnsignedByteArray(Yc); TlsUtils.writeOpaque16(keData, output); return dhAgreeClientPrivateKey; }
public static AsymmetricCipherKeyPair generateECKeyPair(SecureRandom random, ECDomainParameters ecParams) { ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator(); ECKeyGenerationParameters keyGenerationParameters = new ECKeyGenerationParameters(ecParams, random); keyPairGenerator.init(keyGenerationParameters); return keyPairGenerator.generateKeyPair(); }
/** * calculate our initial message. */ public BigInteger calculateMessage() { DHKeyPairGenerator dhGen = new DHKeyPairGenerator(); dhGen.init(new DHKeyGenerationParameters(random, dhParams)); AsymmetricCipherKeyPair dhPair = dhGen.generateKeyPair(); this.privateValue = ((DHPrivateKeyParameters)dhPair.getPrivate()).getX(); return ((DHPublicKeyParameters)dhPair.getPublic()).getY(); }
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)); }
public AsymmetricCipherKeyPair generateKeyPair() { DSAParameters dsaParams = param.getParameters(); BigInteger x = generatePrivateKey(dsaParams.getQ(), param.getRandom()); BigInteger y = calculatePublicKey(dsaParams.getP(), dsaParams.getG(), x); return new AsymmetricCipherKeyPair( new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams)); }
public AsymmetricCipherKeyPair generateKeyPair() { DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE; DHParameters dhp = param.getParameters(); BigInteger x = helper.calculatePrivate(dhp, param.getRandom()); BigInteger y = helper.calculatePublic(dhp, x); return new AsymmetricCipherKeyPair( new DHPublicKeyParameters(y, dhp), new DHPrivateKeyParameters(x, dhp)); }
public EphemeralKeyPair generate() { AsymmetricCipherKeyPair eph = gen.generateKeyPair(); // Encode the ephemeral public key return new EphemeralKeyPair(eph, keyEncoder); }
public KeyPair generateKeyPair() { if (!initialised) { init(new GOST3410ParameterSpec(CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_A.getId()), new SecureRandom()); } AsymmetricCipherKeyPair pair = engine.generateKeyPair(); GOST3410PublicKeyParameters pub = (GOST3410PublicKeyParameters)pair.getPublic(); GOST3410PrivateKeyParameters priv = (GOST3410PrivateKeyParameters)pair.getPrivate(); return new KeyPair(new BCGOST3410PublicKey(pub, gost3410Params), new BCGOST3410PrivateKey(priv, gost3410Params)); }
public KeyPair generateKeyPair() { AsymmetricCipherKeyPair pair = engine.generateKeyPair(); RSAKeyParameters pub = (RSAKeyParameters)pair.getPublic(); RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters)pair.getPrivate(); return new KeyPair(new BCRSAPublicKey(pub), new BCRSAPrivateCrtKey(priv)); }
public ECPoint initEnc(SM2 sm2, ECPoint userKey) { AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.generateKeyPair(); ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) key.getPrivate(); ECPublicKeyParameters ecpub = (ECPublicKeyParameters) key.getPublic(); BigInteger k = ecpriv.getD(); ECPoint c1 = ecpub.getQ(); this.p2 = userKey.multiply(k); reset(); return c1; }
/** * Create a new PrivateKey using the platform provided secure random source. * * @param compressed set to false if you relly want legacy format * @return new PrivateKey */ public static PrivateKey createNew(boolean compressed) { ECKeyPairGenerator generator = new ECKeyPairGenerator(); ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain, secureRandom); generator.init(keygenParams); AsymmetricCipherKeyPair keypair = generator.generateKeyPair(); ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate(); ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic(); return new PrivateKey(privParams.getD(), compressed, pubParams.getQ().getEncoded(compressed)); }
public KeyPair generateKeyPair() { if (!initialised) { throw new IllegalStateException("EC Key Pair Generator not initialised"); } AsymmetricCipherKeyPair pair = engine.generateKeyPair(); ECPublicKeyParameters pub = (ECPublicKeyParameters)pair.getPublic(); ECPrivateKeyParameters priv = (ECPrivateKeyParameters)pair.getPrivate(); return new KeyPair(new JCEECPublicKey(algorithm, pub, ecParams), new JCEECPrivateKey(algorithm, priv, ecParams)); }
@BeforeClass public static void setUpClass() throws CoseException { X9ECParameters p = NISTNamedCurves.getByName("P-256"); ECDomainParameters parameters = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH()); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, null); pGen.init(genParam); AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); keyPublic = (ECPublicKeyParameters) p1.getPublic(); keyPrivate = (ECPrivateKeyParameters) p1.getPrivate(); byte[] rgbX = keyPublic.getQ().normalize().getXCoord().getEncoded(); byte[] rgbY = keyPublic.getQ().normalize().getYCoord().getEncoded(); boolean signY = true; byte[] rgbD = keyPrivate.getD().toByteArray(); CBORObject key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX); key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY); cnKeyPublic = new OneKey(key); key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_X.AsCBOR(), rgbX); key.Add(KeyKeys.EC2_Y.AsCBOR(), rgbY); cnKeyPublicCompressed = new OneKey(key); key = CBORObject.NewMap(); key.Add(KeyKeys.KeyType.AsCBOR(), KeyKeys.KeyType_EC2); key.Add(KeyKeys.EC2_Curve.AsCBOR(), KeyKeys.EC2_P256); key.Add(KeyKeys.EC2_D.AsCBOR(), rgbD); cnKeyPrivate = new OneKey(key); }
public static AsymmetricCipherKeyPair generateECPublicKey() { // Should we also generate random curves? X9ECParameters ecp = SECNamedCurves.getByName(getRandomCurveName()); ECKeyPairGenerator keygen = new ECKeyPairGenerator(); ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); keygen.init(new ECKeyGenerationParameters(domainParams, RandomHelper.getBadSecureRandom())); return keygen.generateKeyPair(); }
protected void setEcDhParams() { msg.prepareComputations(); generateNamedCurveList(msg); generatePointFormatList(msg); prepareCurveType(msg); prepareNamedCurve(msg); ECDomainParameters ecParams = generateEcParameters(msg); AsymmetricCipherKeyPair keyPair = TlsECCUtils.generateECKeyPair(chooser.getContext().getBadSecureRandom(), ecParams); pubEcParams = (ECPublicKeyParameters) keyPair.getPublic(); privEcParams = (ECPrivateKeyParameters) keyPair.getPrivate(); }
public static DHPrivateKeyParameters generateEphemeralClientKeyExchange(SecureRandom random, DHParameters dhParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair kp = generateDHKeyPair(random, dhParams); DHPublicKeyParameters dhPublic = (DHPublicKeyParameters) kp.getPublic(); writeDHParameter(dhPublic.getY(), output); return (DHPrivateKeyParameters) kp.getPrivate(); }
public static DHPrivateKeyParameters generateEphemeralServerKeyExchange(SecureRandom random, DHParameters dhParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair kp = generateDHKeyPair(random, dhParams); DHPublicKeyParameters dhPublic = (DHPublicKeyParameters)kp.getPublic(); new ServerDHParams(dhPublic).encode(output); return (DHPrivateKeyParameters)kp.getPrivate(); }
public static ECPrivateKeyParameters generateEphemeralClientKeyExchange(SecureRandom random, short[] ecPointFormats, ECDomainParameters ecParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair kp = generateECKeyPair(random, ecParams); ECPublicKeyParameters ecPublicKey = (ECPublicKeyParameters) kp.getPublic(); writeECPoint(ecPointFormats, ecPublicKey.getQ(), output); return (ECPrivateKeyParameters) kp.getPrivate(); }