ecParam.getH(), ecParam.getSeed()); KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME); ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec); privateKey = (PrivateKey) keyFactory.generatePrivate(keySpec);
private PrivateKey loadPrivateKey(byte[] data) throws Exception { ECParameterSpec params = ECNamedCurveTable.getParameterSpec("prime192v1"); ECPrivateKeySpec prvkey = new ECPrivateKeySpec(new BigInteger(data), params); KeyFactory kf = KeyFactory.getInstance("ECDH", "BC"); return kf.generatePrivate(prvkey); } }
private static PrivateKey privateKeyFromBigInteger(BigInteger priv) { if (priv == null) { return null; } else { try { return ECKeyFactory .getInstance(SpongyCastleProvider.getInstance()) .generatePrivate(new ECPrivateKeySpec(priv, CURVE_SPEC)); } catch (InvalidKeySpecException ex) { throw new AssertionError("Assumed correct key spec statically"); } } }
/** * Convert a byte array to an EC private key by decoding the D number * parameter. * * @param keyBytes Bytes to be converted to the EC private key. * @return An instance of EC private key decoded from the input bytes. * @throws InvalidKeySpecException The provided key bytes are not a valid EC * private key. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ public PrivateKey convertBytesToPrivateKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); BigInteger keyInteger = new BigInteger(keyBytes); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(keyInteger, ecSpec); return kf.generatePrivate(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
/** * Convert a byte array to an EC private key by decoding the D number * parameter. * * @param keyBytes Bytes to be converted to the EC private key. * @return An instance of EC private key decoded from the input bytes. * @throws InvalidKeySpecException The provided key bytes are not a valid EC * private key. * @throws CryptoProviderException When crypto provider is incorrectly initialized. */ public PrivateKey convertBytesToPrivateKey(byte[] keyBytes) throws InvalidKeySpecException, CryptoProviderException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); BigInteger keyInteger = new BigInteger(keyBytes); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(keyInteger, ecSpec); return kf.generatePrivate(pubSpec); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new CryptoProviderException(ex.getMessage(), ex); } }
/** * Load the private key from a URL-safe base64 encoded string * * @param encodedPrivateKey * @return * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { byte[] decodedPrivateKey = Base64Encoder.decode(encodedPrivateKey); BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey); ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE); ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME); return keyFactory.generatePrivate(privateKeySpec); }
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(); } }
/** * Calculate the signature of data, using the given private key. * * @param data to be signed * @param privateKey to be used for signing * @return the signature */ public static byte[] getSignature(byte[] data, ch.dissem.bitmessage.entity.valueobject.PrivateKey privateKey) { 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() ); BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey()); KeySpec keySpec = new ECPrivateKeySpec(d, spec); PrivateKey privKey = KeyFactory.getInstance("ECDSA", "BC").generatePrivate(keySpec); Signature sig = Signature.getInstance("ECDSA", "BC"); sig.initSign(privKey); sig.update(data); return sig.sign(); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public byte[] getSignature(byte[] data, PrivateKey privateKey) { 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() ); BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey()); KeySpec keySpec = new ECPrivateKeySpec(d, spec); java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider) .generatePrivate(keySpec); Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider); sig.initSign(privKey); sig.update(data); return sig.sign(); } catch (GeneralSecurityException e) { throw new ApplicationException(e); } }
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()); } }
public static PrivateKey parsePrivateKey(String keyBytesHex) { try { KeyFactory fac = KeyFactory.getInstance("ECDSA"); X9ECParameters curve = SECNamedCurves.getByName("secp256r1"); ECParameterSpec curveSpec = new ECParameterSpec( curve.getCurve(), curve.getG(), curve.getN(), curve.getH()); ECPrivateKeySpec keySpec = new ECPrivateKeySpec( new BigInteger(keyBytesHex, 16), curveSpec); return fac.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } }
ecParam.getH(), ecParam.getSeed()); KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME); ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec); privateKey = (PrivateKey) keyFactory.generatePrivate(keySpec);
return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams(), false)); return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams(), false)); return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams(), false)); return new org.bouncycastle.jce.spec.ECPrivateKeySpec(k.getS(), implicitSpec);
return new ECPrivateKeySpec(k.getS(), EC5Util.convertSpec(k.getParams(), false)); return new ECPrivateKeySpec(k.getS(), implicitSpec);