ecParam.getH(), ecParam.getSeed()); KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME); ECPublicKeySpec keySpec = new ECPublicKeySpec(((BCECPublicKey) publicKey).getQ(), ecSpec); publicKey = (PublicKey) keyFactory.generatePublic(keySpec);
public byte[] multiplyPublicKey(byte[] pub, byte[] priv) { PublicKey multPubKey = null; ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(priv), spec); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(curve.decodePoint(pub), spec); ECPublicKeySpec newPublicKeySpec = new ECPublicKeySpec(publicKeySpec.getQ().multiply(privateKeySpec.getD()), spec); try { multPubKey = f.generatePublic(newPublicKeySpec); } catch (Exception e) { } return multPubKey.getEncoded(); } }
public BCECGOST3410_2012PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } }
public BCDSTU4145PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } }
private PublicKey loadPublicKey(byte[] data) throws Exception { ECParameterSpec params = ECNamedCurveTable.getParameterSpec("prime192v1"); ECPublicKeySpec pubKey = new ECPublicKeySpec(params.getCurve().decodePoint(data), params); KeyFactory kf = KeyFactory.getInstance("ECDH", "BC"); return kf.generatePublic(pubKey); }
public BCECGOST3410PublicKey( org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } }
/** * Load the public key from a URL-safe base64 encoded string. Takes into * account the different encodings, including point compression. * * @param encodedPublicKey */ public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { byte[] decodedPublicKey = Base64Encoder.decode(encodedPublicKey); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME); ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE); ECCurve curve = parameterSpec.getCurve(); ECPoint point = curve.decodePoint(decodedPublicKey); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec); return keyFactory.generatePublic(pubSpec); }
public BCECPublicKey( String algorithm, org.bouncycastle.jce.spec.ECPublicKeySpec spec, ProviderConfiguration configuration) { this.algorithm = algorithm; if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); // this may seem a little long-winded but it's how we pick up the custom curve. this.ecPublicKey = new ECPublicKeyParameters( spec.getQ(), ECUtil.getDomainParameters(configuration, spec.getParams())); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { org.bouncycastle.jce.spec.ECParameterSpec s = configuration.getEcImplicitlyCa(); this.ecPublicKey = new ECPublicKeyParameters(s.getCurve().createPoint(spec.getQ().getAffineXCoord().toBigInteger(), spec.getQ().getAffineYCoord().toBigInteger()), EC5Util.getDomainParameters(configuration, (ECParameterSpec)null)); this.ecSpec = null; } this.configuration = configuration; }
@Override public PublicKey publicKeyFromPrivateKey(PrivateKey privateKey, KeyFactory keyFactory) throws InvalidKeySpecException { ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey; ECParameterSpec ecParams = ecPrivateKey.getParameters(); ECPoint q = ecParams.getG().multiply(ecPrivateKey.getD()); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(q, ecParams); return keyFactory.generatePublic(publicKeySpec); } }
public JCEECPublicKey( String algorithm, org.bouncycastle.jce.spec.ECPublicKeySpec spec) { this.algorithm = algorithm; this.q = spec.getQ(); if (spec.getParams() != null) // can be null if implictlyCa { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { if (q.getCurve() == null) { org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa(); q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false); } this.ecSpec = null; } }
public static PublicKey createPubKeyEcdsa(String name, BigInteger x, BigInteger y) throws NoSuchAlgorithmException, InvalidKeySpecException { final String curveName = EC_NAME_TO_CURVE_NAME.get(name); ECNamedCurveParameterSpec curveParaSpecBc = ECNamedCurveTable.getParameterSpec(curveName); ECPoint point = curveParaSpecBc.getCurve().createPoint(x, y); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, curveParaSpecBc); return new JCEECPublicKey("EC", pubKeySpec); } }
public static BCECPublicKey getBCECPublicKey(X509Certificate sm2Cert) { ECPublicKey pubKey = (ECPublicKey) sm2Cert.getPublicKey(); ECPoint q = pubKey.getQ(); ECParameterSpec parameterSpec = new ECParameterSpec(SM2Util.CURVE, SM2Util.G_POINT, SM2Util.SM2_ECC_N, SM2Util.SM2_ECC_H); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(q, parameterSpec); return new BCECPublicKey(pubKey.getAlgorithm(), pubKeySpec, BouncyCastleProvider.CONFIGURATION); }
/** * @param data to check * @param signature the signature of the message * @param pubkey the sender's public key * @return true if the signature is valid, false otherwise */ public static boolean isSignatureValid(byte[] data, byte[] signature, Pubkey pubkey) { try { ECParameterSpec spec = new ECParameterSpec( EC_CURVE_PARAMETERS.getCurve(), EC_CURVE_PARAMETERS.getG(), EC_CURVE_PARAMETERS.getN(), EC_CURVE_PARAMETERS.getH(), EC_CURVE_PARAMETERS.getSeed() ); ECPoint Q = keyToPoint(pubkey.getSigningKey()); KeySpec keySpec = new ECPublicKeySpec(Q, spec); PublicKey publicKey = KeyFactory.getInstance("ECDSA", "BC").generatePublic(keySpec); Signature sig = Signature.getInstance("ECDSA", "BC"); sig.initVerify(publicKey); sig.update(data); return sig.verify(signature); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public boolean isSignatureValid(byte[] data, byte[] signature, Pubkey pubkey) { try { ECParameterSpec spec = new ECParameterSpec( EC_CURVE_PARAMETERS.getCurve(), EC_CURVE_PARAMETERS.getG(), EC_CURVE_PARAMETERS.getN(), EC_CURVE_PARAMETERS.getH(), EC_CURVE_PARAMETERS.getSeed() ); ECPoint Q = keyToPoint(pubkey.getSigningKey()); KeySpec keySpec = new ECPublicKeySpec(Q, spec); PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider).generatePublic(keySpec); Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider); sig.initVerify(publicKey); sig.update(data); return sig.verify(signature); } catch (GeneralSecurityException e) { throw new ApplicationException(e); } }
public static PublicKey extractPublicKey(PrivateKey privateKey) { String algorithm = privateKey.getAlgorithm(); try { if (algorithm.equals(RSA.getAlgorithmName())) { KeyFactory keyFactory = KeyFactory.getInstance(RSA.getAlgorithmName(), BouncyCastleProviderHolder.getInstance()); RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey; RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent()); return keyFactory.generatePublic(keySpec); } else if (algorithm.equals(EC.getAlgorithmName())) { KeyFactory keyFactory = KeyFactory.getInstance(EC.getAlgorithmName(), BouncyCastleProviderHolder.getInstance()); BCECPrivateKey ecPrivateKey = (BCECPrivateKey) privateKey; ECParameterSpec ecParameterSpec = ecPrivateKey.getParameters(); ECPoint ecPoint = new FixedPointCombMultiplier().multiply(ecParameterSpec.getG(), ecPrivateKey.getD()); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec); return keyFactory.generatePublic(keySpec); } else { throw new IllegalArgumentException("Unexpected key algorithm: " + algorithm); } } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
ECParameterSpec spec = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN()); ECPublicKeySpec publicSpec = new ECPublicKeySpec(pPublicPoint, spec);
/** * Converts byte array to an EC public key, by decoding the Q point * parameter. * * @param keyBytes Bytes to be converted to EC public key. * @return An instance of the EC public key on success, or null on failure. * @throws InvalidKeySpecException When provided bytes are not a correct key * representation. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ public PublicKey convertBytesToPublicKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); if (ecSpec == null) { // can happen with incorrectly initialized crypto provider. throw new CryptoProviderException("Crypto provider does not support the secp256r1 curve"); } ECPoint point = ecSpec.getCurve().decodePoint(keyBytes); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec); return kf.generatePublic(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
/** * Converts byte array to an EC public key, by decoding the Q point * parameter. * * @param keyBytes Bytes to be converted to EC public key. * @return An instance of the EC public key on success, or null on failure. * @throws InvalidKeySpecException When provided bytes are not a correct key * representation. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ public PublicKey convertBytesToPublicKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); if (ecSpec == null) { // can happen with incorrectly initialized crypto provider. throw new CryptoProviderException("Crypto provider does not support the secp256r1 curve"); } ECPoint point = ecSpec.getCurve().decodePoint(keyBytes); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec); return kf.generatePublic(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
/** * Load a public key from the private key. * * @param privateKey * @return */ public static ECPublicKey loadPublicKey(ECPrivateKey privateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE); ECPoint Q = ecSpec.getG().multiply(privateKey.getD()); byte[] publicDerBytes = Q.getEncoded(false); ECPoint point = ecSpec.getCurve().decodePoint(publicDerBytes); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, ecSpec); return (ECPublicKey) keyFactory.generatePublic(pubSpec); }
public static ECPublicKey generateECPublicKey(final ASN1ObjectIdentifier curveOid, final byte[] encodedQ) throws InvalidKeySpecException { ParamUtil.requireNonNull("curveOid", curveOid); ParamUtil.requireNonNull("encoded", encodedQ); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveOid.getId()); ECPoint pointQ = spec.getCurve().decodePoint(encodedQ); ECPublicKeySpec keySpec = new ECPublicKeySpec(pointQ, spec); KeyFactory kf = getKeyFactory("EC"); synchronized (kf) { return (ECPublicKey) kf.generatePublic(keySpec); } }