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(); } }
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); }
/** * 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 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); }
@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 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); } }
/** * @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); } }
/** * 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); } }
public ECKeyPair(byte[] privateKey) { this.privateKey = Arrays.copyOf(privateKey, privateKey.length); ECPrivateKey ecPrivateKey; try { ECDomainParameters domain = ECKeyPairGenerator.getDomain(((this.privateKey.length - 1) * 8)); ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec( new BigInteger(1, this.privateKey), new ECParameterSpec(domain.getCurve(), domain.getG(), domain.getN(), domain.getH()) ); ecPrivateKey = (ECPrivateKey) KeyFactory.getInstance("EC", "BC").generatePrivate(privateKeySpec); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } try { ECDomainParameters domain = ECKeyPairGenerator.getDomain((this.privateKey.length - 1) * 8); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec( domain.getG().multiply(ecPrivateKey.getD()), new ECParameterSpec(domain.getCurve(), domain.getG(), domain.getN(), domain.getH()) ); this.publicKey = new ECPublicKey( ((org.bouncycastle.jce.interfaces.ECPublicKey) KeyFactory.getInstance("EC", "BC") .generatePublic(publicKeySpec)).getQ().getEncoded(true)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
@Override public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2fBadInputException { try { X9ECParameters curve = SECNamedCurves.getByName("secp256r1"); ECPoint point; try { point = curve.getCurve().decodePoint(encodedPublicKey); } catch (RuntimeException e) { throw new U2fBadInputException("Could not parse user public key", e); } return KeyFactory.getInstance("ECDSA").generatePublic( new ECPublicKeySpec(point, new ECParameterSpec( curve.getCurve(), curve.getG(), curve.getN(), curve.getH() ) ) ); } catch (GeneralSecurityException e) { //This should not happen throw new RuntimeException( "Failed to decode public key: " + U2fB64Encoding.encode(encodedPublicKey), e ); } }
@Override public PublicKey decodePublicKey(byte[] encodedPublicKey) throws U2fBadInputException { try { X9ECParameters curve = SECNamedCurves.getByName("secp256r1"); ECPoint point; try { point = curve.getCurve().decodePoint(encodedPublicKey); } catch (RuntimeException e) { throw new U2fBadInputException("Could not parse user public key", e); } return KeyFactory.getInstance("ECDSA").generatePublic( new ECPublicKeySpec(point, new ECParameterSpec( curve.getCurve(), curve.getG(), curve.getN(), curve.getH() ) ) ); } catch (GeneralSecurityException e) { //This should not happen throw new RuntimeException( "Failed to decode public key: " + U2fB64Encoding.encode(encodedPublicKey), e ); } }
@Override public PublicKey decodePublicKey(byte[] encodedPublicKey) throws SignatureException { X9ECParameters curve = SECNamedCurves.getByName("secp256r1"); ECPoint point = curve.getCurve().decodePoint(encodedPublicKey); try { return KeyFactory.getInstance("ECDSA").generatePublic( new ECPublicKeySpec(point, new ECParameterSpec( curve.getCurve(), curve.getG(), curve.getN(), curve.getH() ) ) ); } catch (GeneralSecurityException ex) { throw new SignatureException(ex); } }
public BigInteger keyAgreement(ECPoint otherParty) { if (privKey == null) { throw new MissingPrivateKeyException(); } else if (privKey instanceof BCECPrivateKey) { final ECDHBasicAgreement agreement = new ECDHBasicAgreement(); agreement.init(new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE)); return agreement.calculateAgreement(new ECPublicKeyParameters(otherParty, CURVE)); } else { try { final KeyAgreement agreement = ECKeyAgreement.getInstance(this.provider); agreement.init(this.privKey); agreement.doPhase( ECKeyFactory.getInstance(this.provider) .generatePublic(new ECPublicKeySpec(otherParty, CURVE_SPEC)), /* lastPhase */ true); return new BigInteger(1, agreement.generateSecret()); } catch (IllegalStateException | InvalidKeyException | InvalidKeySpecException ex) { throw new RuntimeException("ECDH key agreement failure", ex); } } }
public static PublicKey parsePublicKey(byte[] keyBytes) { try { X9ECParameters curve = SECNamedCurves.getByName("secp256r1"); ECParameterSpec curveSpec = new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH()); ECPoint point = curve.getCurve().decodePoint(keyBytes); return KeyFactory.getInstance("ECDSA").generatePublic( new ECPublicKeySpec(point, curveSpec)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } }