private static PublicKey toPublicKey(KeyFactory kf, ECPoint point, ECParameterSpec keySpec) { try { return kf.generatePublic(new ECPublicKeySpec(point, keySpec)); } catch (InvalidKeySpecException e) { throw new JwtException("Failed to generate EC public key", e); } }
@Override public void computeK(byte[] f) throws GeneralSecurityException { KeyFactory keyFactory = SecurityUtils.getKeyFactory("EC"); ECPublicKeySpec keySpec = new ECPublicKeySpec(getDecoded(f, ecParameterSpec.getCurve()), ecParameterSpec); PublicKey yourPubKey = keyFactory.generatePublic(keySpec); agreement.doPhase(yourPubKey, true); setK(new BigInteger(1, agreement.generateSecret())); }
ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec(name, ecParams.getCurve(), ecParams.getG(), ecParams.getN()); ECPoint p = new ECPoint(bigX, bigY); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(p, ecCurveSpec);
private static ECPublicKey cvtToJavaECKey(SigningPublicKey pk) throws GeneralSecurityException { SigType type = pk.getType(); BigInteger[] xy = split(pk.getData()); ECPoint w = new ECPoint(xy[0], xy[1]); // see ECConstants re: casting ECPublicKeySpec ks = new ECPublicKeySpec(w, (ECParameterSpec) type.getParams()); KeyFactory kf = KeyFactory.getInstance("EC"); return (ECPublicKey) kf.generatePublic(ks); }
ECPoint g = spec.getGenerator(); ECPoint w = ECUtil.scalarMult(g, s, curve); ECPublicKeySpec ecks = new ECPublicKeySpec(w, ecpriv.getParams()); KeyFactory eckf = KeyFactory.getInstance("EC"); ECPublicKey ecpub = (ECPublicKey) eckf.generatePublic(ecks);
private static PublicKey getECPublicKey(ECPoint ecPoint, ECParameterSpec curveSpec, Provider provider) { // Create public key spec with given point try { ECPublicKeySpec pubSpec = new ECPublicKeySpec(ecPoint, curveSpec); KeyFactory kf = provider != null ? KeyFactory.getInstance("EC", provider) : KeyFactory.getInstance("EC", "SunEC"); return (ECPublicKey) kf.generatePublic(pubSpec); } catch (GeneralSecurityException e) { throw new IllegalStateException(e); } }
ECParameterSpec ecParameters = ...; BigInteger x = ...; BigInteger y = ...; ECPoint ecPoint = new ECPoint(x, y); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, ecParameters);
private PublicKey getPublicKeyFromBytes(byte[] pubKey) throws NoSuchAlgorithmException, InvalidKeySpecException { ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1"); KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider()); ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(), spec.getN()); ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey); ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params); ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec); return pk; }
public byte[] getSecret(byte[] r, byte[] s) throws Exception{ KeyFactory kf = KeyFactory.getInstance("EC"); ECPoint w = new ECPoint(new BigInteger(1, r), new BigInteger(1, s)); ECPublicKeySpec spec = new ECPublicKeySpec(w, publicKey.getParams()); PublicKey theirPublicKey = kf.generatePublic(spec); myKeyAgree.doPhase(theirPublicKey, true); return myKeyAgree.generateSecret(); }
private static PublicKey generateEcPublicKey(GOSTCurve curve, CustomECPoint point, String keyFactoryAlg) { try { ECParameterSpec ecParameterSpec = getEcParameterSpec(curve); ECPoint ecPoint = new ECPoint(point.getX(), point.getY()); ECPublicKeySpec privateKeySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec); return KeyFactory.getInstance(keyFactoryAlg).generatePublic(privateKeySpec); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { LOGGER.error("Could not generate GOST public key", e); return null; } }
public static ECPublicKey getECPublicKey(String curve, byte[] xPoint, byte[] yPoint) { try { ECParameterSpec params = getECParameterSpec(curve, false); ECPoint ecPoint = new ECPoint(toBigInteger(xPoint), toBigInteger(yPoint)); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, params); KeyFactory kf = KeyFactory.getInstance("EC"); return (ECPublicKey) kf.generatePublic(keySpec); } catch (Exception ex) { throw new SecurityException(ex); } } private static BigInteger toBigInteger(byte[] bytes) {
@Override protected byte[] calculateK() throws Exception { Objects.requireNonNull(params, "No ECParameterSpec(s)"); Objects.requireNonNull(f, "Missing 'f' value"); ECPublicKeySpec keySpec = new ECPublicKeySpec(f, params); KeyFactory myKeyFac = SecurityUtils.getKeyFactory(KeyUtils.EC_ALGORITHM); PublicKey yourPubKey = myKeyFac.generatePublic(keySpec); myKeyAgree.doPhase(yourPubKey, true); return stripLeadingZeroes(myKeyAgree.generateSecret()); }
ECPoint pubPoint = new ECPoint(new BigInteger(1, x),new BigInteger(1, y)); AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC", "SunEC"); parameters.init(new ECGenParameterSpec("secp256r1")); ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class); ECPublicKeySpec pubSpec = new ECPublicKeySpec(pubPoint, ecParameters); KeyFactory kf = KeyFactory.getInstance("EC"); return (ECPublicKey)kf.generatePublic(pubSpec);
/** * Returns an {@link ECPublicKey} from {@code publicKey} that is a public key in point format * {@code pointFormat} on {@code curve}. */ public static ECPublicKey getEcPublicKey( ECParameterSpec spec, PointFormatType pointFormat, final byte[] publicKey) throws GeneralSecurityException { ECPoint point = pointDecode(spec.getCurve(), pointFormat, publicKey); ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, spec); KeyFactory kf = EngineFactory.KEY_FACTORY.getInstance("EC"); return (ECPublicKey) kf.generatePublic(pubSpec); }
@Override protected byte[] calculateK() throws Exception { Objects.requireNonNull(params, "No ECParameterSpec(s)"); KeyFactory myKeyFac = SecurityUtils.getKeyFactory(KeyUtils.EC_ALGORITHM); ECPublicKeySpec keySpec = new ECPublicKeySpec(f, params); PublicKey yourPubKey = myKeyFac.generatePublic(keySpec); myKeyAgree.doPhase(yourPubKey, true); return stripLeadingZeroes(myKeyAgree.generateSecret()); }
@Override protected byte[] calculateK() throws Exception { Objects.requireNonNull(params, "No ECParameterSpec(s)"); KeyFactory myKeyFac = SecurityUtils.getKeyFactory(KeyUtils.EC_ALGORITHM); ECPublicKeySpec keySpec = new ECPublicKeySpec(f, params); PublicKey yourPubKey = myKeyFac.generatePublic(keySpec); myKeyAgree.doPhase(yourPubKey, true); return stripLeadingZeroes(myKeyAgree.generateSecret()); }
private static PublicKey toECGOSTPublicKey(KEYBase r, ECKeyInfo keyinfo) throws IOException, GeneralSecurityException, MalformedKeyException { DNSInput in = new DNSInput(r.getKey()); BigInteger x = readBigIntegerLittleEndian(in, keyinfo.length); BigInteger y = readBigIntegerLittleEndian(in, keyinfo.length); ECPoint q = new ECPoint(x, y); KeyFactory factory = KeyFactory.getInstance("ECGOST3410"); return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec)); }
private static PublicKey toECDSAPublicKey(KEYBase r, ECKeyInfo keyinfo) throws IOException, GeneralSecurityException, MalformedKeyException { DNSInput in = new DNSInput(r.getKey()); // RFC 6605 Section 4 BigInteger x = readBigInteger(in, keyinfo.length); BigInteger y = readBigInteger(in, keyinfo.length); ECPoint q = new ECPoint(x, y); KeyFactory factory = KeyFactory.getInstance("EC"); return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec)); }
private static PublicKey toECDSAPublicKey(KEYBase r, ECKeyInfo keyinfo) throws IOException, GeneralSecurityException, MalformedKeyException { DNSInput in = new DNSInput(r.getKey()); // RFC 6605 Section 4 BigInteger x = readBigInteger(in, keyinfo.length); BigInteger y = readBigInteger(in, keyinfo.length); ECPoint q = new ECPoint(x, y); KeyFactory factory = KeyFactory.getInstance("EC"); return factory.generatePublic(new ECPublicKeySpec(q, keyinfo.spec)); }
@Override public void computeK(byte[] f) throws GeneralSecurityException { KeyFactory keyFactory = SecurityUtils.getKeyFactory("EC"); ECPublicKeySpec keySpec = new ECPublicKeySpec(getDecoded(f, ecParameterSpec.getCurve()), ecParameterSpec); PublicKey yourPubKey = keyFactory.generatePublic(keySpec); agreement.doPhase(yourPubKey, true); setK(new BigInteger(1, agreement.generateSecret())); }