List of usage examples for org.bouncycastle.jce.spec ECPrivateKeySpec ECPrivateKeySpec
public ECPrivateKeySpec(BigInteger d, ECParameterSpec spec)
From source file:ch.dissem.bitmessage.cryptography.bc.BouncyCryptography.java
License:Apache License
@Override public byte[] getSignature(byte[] data, PrivateKey privateKey) { try {/* ww w . j ava 2 s .c o m*/ 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); } }
From source file:com.aelitis.azureus.core.security.CryptoECCUtils.java
License:Open Source License
public static PrivateKey rawdataToPrivkey(byte[] input) throws CryptoManagerException { BigInteger D = new BigInteger(input); KeySpec keyspec = new ECPrivateKeySpec(D, (ECParameterSpec) ECCparam); PrivateKey privkey = null;//from ww w . j av a 2 s . c o m try { privkey = KeyFactory.getInstance("ECDSA", "BC").generatePrivate(keyspec); return privkey; } catch (Throwable e) { throw (new CryptoManagerException("Failed to decode private key")); } }
From source file:com.distrimind.util.crypto.ASymmetricEncryptionType.java
License:Open Source License
static ECPrivateKeySpec deserializePrivateKey(byte[] privateKey, boolean lazy) { if (privateKey.length <= 32) { if (lazy) { return null; }/*from w ww .j a v a 2 s . com*/ BigInteger s = new BigInteger(privateKey); return new ECPrivateKeySpec(s, getCurve25519()); } else { throw new IllegalArgumentException("privateKey.length=" + privateKey.length); } }
From source file:com.google.u2f.TestUtils.java
License:Open Source License
public static PrivateKey parsePrivateKey(String keyBytesHex) { try {/*w ww .j a v a 2 s . c om*/ 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); } }
From source file:com.yahoo.athenz.auth.util.Crypto.java
License:Apache License
public static PrivateKey loadPrivateKey(Reader reader, String pwd) throws CryptoException { try (PEMParser pemReader = new PEMParser(reader)) { PrivateKey privKey = null; X9ECParameters ecParam = null;//from ww w. j a v a2s .co m Object pemObj = pemReader.readObject(); if (pemObj instanceof ASN1ObjectIdentifier) { // make sure this is EC Parameter we're handling. In which case // we'll store it and read the next object which should be our // EC Private Key ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj; ecParam = ECNamedCurveTable.getByOID(ecOID); if (ecParam == null) { throw new PEMException("Unable to find EC Parameter for the given curve oid: " + ((ASN1ObjectIdentifier) pemObj).getId()); } pemObj = pemReader.readObject(); } else if (pemObj instanceof X9ECParameters) { ecParam = (X9ECParameters) pemObj; pemObj = pemReader.readObject(); } if (pemObj instanceof PEMKeyPair) { PrivateKeyInfo pKeyInfo = ((PEMKeyPair) pemObj).getPrivateKeyInfo(); JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter(); privKey = pemConverter.getPrivateKey(pKeyInfo); } else if (pemObj instanceof PKCS8EncryptedPrivateKeyInfo) { PKCS8EncryptedPrivateKeyInfo pKeyInfo = (PKCS8EncryptedPrivateKeyInfo) pemObj; if (pwd == null) { throw new CryptoException("No password specified to decrypt encrypted private key"); } // Decrypt the private key with the specified password InputDecryptorProvider pkcs8Prov = new JceOpenSSLPKCS8DecryptorProviderBuilder() .setProvider(BC_PROVIDER).build(pwd.toCharArray()); PrivateKeyInfo privateKeyInfo = pKeyInfo.decryptPrivateKeyInfo(pkcs8Prov); JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter(); privKey = pemConverter.getPrivateKey(privateKeyInfo); } // if our private key is EC type and we have parameters specified // then we need to set it accordingly if (ecParam != null && ECDSA.equals(privKey.getAlgorithm())) { ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(), ecParam.getH(), ecParam.getSeed()); KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BC_PROVIDER); ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privKey).getS(), ecSpec); privKey = (PrivateKey) keyFactory.generatePrivate(keySpec); } return privKey; } catch (PEMException e) { LOG.error("loadPrivateKey: Caught PEMException, problem with format of key detected."); throw new CryptoException(e); } catch (NoSuchProviderException e) { LOG.error( "loadPrivateKey: Caught NoSuchProviderException, check to make sure the provider is loaded correctly."); throw new CryptoException(e); } catch (NoSuchAlgorithmException e) { LOG.error( "loadPrivateKey: Caught NoSuchAlgorithmException, check to make sure the algorithm is supported by the provider."); throw new CryptoException(e); } catch (InvalidKeySpecException e) { LOG.error("loadPrivateKey: Caught InvalidKeySpecException, invalid key spec is being used."); throw new CryptoException(e); } catch (OperatorCreationException e) { LOG.error( "loadPrivateKey: Caught OperatorCreationException when creating JceOpenSSLPKCS8DecryptorProviderBuilder."); throw new CryptoException(e); } catch (PKCSException e) { LOG.error("loadPrivateKey: Caught PKCSException when decrypting private key."); throw new CryptoException(e); } catch (IOException e) { LOG.error("loadPrivateKey: Caught IOException, while trying to read key."); throw new CryptoException(e); } }
From source file:de.tsenger.animamea.ta.TerminalAuthenticationECDSA.java
License:Open Source License
@Override public byte[] sign(byte[] dataToSign) throws TAException { Signature sig = null;// w w w. j a va 2 s .com try { sig = Signature.getInstance(signingAlgorithm); } catch (NoSuchAlgorithmException e) { throw new TAException(e); } byte[] dersig = null; try { KeyFactory kef = KeyFactory.getInstance("ECDSA", "BC"); ECPrivateKeySpec priKeySpec = new ECPrivateKeySpec(terminalSK, ecp); PrivateKey key = kef.generatePrivate(priKeySpec); sig.initSign(key); sig.update(dataToSign); dersig = sig.sign(); } catch (NoSuchAlgorithmException e) { throw new TAException(e); } catch (NoSuchProviderException e) { throw new TAException(e); } catch (InvalidKeySpecException e) { throw new TAException(e); } catch (InvalidKeyException e) { throw new TAException(e); } catch (SignatureException e) { throw new TAException(e); } // decompose signature to get r || s DLSequence seq = null; try { seq = (DLSequence) DLSequence.fromByteArray(dersig); } catch (IOException e) { throw new TAException(e); } DERInteger derIntR = (DERInteger) seq.getObjectAt(0); DERInteger derIntS = (DERInteger) seq.getObjectAt(1); BigInteger my_r = derIntR.getValue(); BigInteger my_s = derIntS.getValue(); byte[] r = Converter.bigIntToByteArray(my_r); byte[] s = Converter.bigIntToByteArray(my_s); byte[] signature = new byte[r.length + s.length]; System.arraycopy(r, 0, signature, 0, r.length); System.arraycopy(s, 0, signature, r.length, s.length); return signature; }
From source file:io.getlime.security.powerauth.crypto.lib.util.KeyConversionUtilsTest.java
License:Apache License
/** * Test of convertPublicKey method, of class KeyConversionUtils. * /*from w w w .jav a 2 s . com*/ * @throws Exception In case test fails * */ @Test public void testConvertPublicKey() throws Exception { System.out.println("convertPublicKeyToBytes"); KeyGenerator keyGenerator = new KeyGenerator(); CryptoProviderUtil instance = PowerAuthConfiguration.INSTANCE.getKeyConvertor(); PublicKey key = instance.convertBytesToPublicKey( BaseEncoding.base64().decode("AsUaehWpuZseHUprd9immCELf62TTtHUGlTIXyCxY7h2")); for (int i = 0; i < 1000; i++) { KeyPair kp = keyGenerator.generateKeyPair(); PublicKey publicKey = kp.getPublic(); byte[] originalBytes = instance.convertPublicKeyToBytes(publicKey); String originalBase64 = BaseEncoding.base64().encode(originalBytes); byte[] decodedBytes = BaseEncoding.base64().decode(originalBase64); PublicKey decodedPublicKey = instance.convertBytesToPublicKey(decodedBytes); assertEquals(publicKey, decodedPublicKey); PrivateKey privateKey = kp.getPrivate(); byte[] originalPrivateBytes = instance.convertPrivateKeyToBytes(privateKey); String originalPrivateBase64 = BaseEncoding.base64().encode(originalPrivateBytes); byte[] decodedPrivateBytes = BaseEncoding.base64().decode(originalPrivateBase64); PrivateKey decodedPrivateKey = instance.convertBytesToPrivateKey(decodedPrivateBytes); assertEquals(privateKey, decodedPrivateKey); KeyFactory kf = KeyFactory.getInstance("ECDH", PowerAuthConfiguration.INSTANCE.getKeyConvertor().getProviderName()); BigInteger keyInteger = new BigInteger("" + (12 * i)); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(keyInteger, ecSpec); ECPrivateKey privateKey2 = (ECPrivateKey) kf.generatePrivate(pubSpec); originalPrivateBytes = instance.convertPrivateKeyToBytes(privateKey2); originalPrivateBase64 = BaseEncoding.base64().encode(originalPrivateBytes); decodedPrivateBytes = BaseEncoding.base64().decode(originalPrivateBase64); PrivateKey decodedPrivateKey2 = instance.convertBytesToPrivateKey(decodedPrivateBytes); assertEquals(privateKey2, decodedPrivateKey2); } }
From source file:io.getlime.security.powerauth.provider.CryptoProviderUtilBouncyCastle.java
License:Apache License
/** * Convert a byte array to an EC private key by decoding the D number * parameter.// ww w . ja v a 2s . co m * * @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. */ public PrivateKey convertBytesToPrivateKey(byte[] keyBytes) throws InvalidKeySpecException { try { KeyFactory kf = KeyFactory.getInstance("ECDH", getProviderName()); BigInteger keyInteger = new BigInteger(keyBytes); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); ECPrivateKeySpec pubSpec = new ECPrivateKeySpec(keyInteger, ecSpec); ECPrivateKey privateKey = (ECPrivateKey) kf.generatePrivate(pubSpec); return privateKey; } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:jpcsp.crypto.ECDSA.java
License:Open Source License
public byte[] multiplyPublicKey(byte[] pub, byte[] priv) { PublicKey multPubKey = null;/*from w w w . j a v a 2 s . c o m*/ 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(); }
From source file:org.apache.pulsar.client.impl.MessageCrypto.java
License:Apache License
private PrivateKey loadPrivateKey(byte[] keyBytes) throws Exception { Reader keyReader = new StringReader(new String(keyBytes)); PrivateKey privateKey = null; try (PEMParser pemReader = new PEMParser(keyReader)) { X9ECParameters ecParam = null;/*from w w w . ja va 2s. c om*/ Object pemObj = pemReader.readObject(); if (pemObj instanceof ASN1ObjectIdentifier) { // make sure this is EC Parameter we're handling. In which case // we'll store it and read the next object which should be our // EC Private Key ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj; ecParam = ECNamedCurveTable.getByOID(ecOID); if (ecParam == null) { throw new PEMException("Unable to find EC Parameter for the given curve oid: " + ecOID.getId()); } pemObj = pemReader.readObject(); } else if (pemObj instanceof X9ECParameters) { ecParam = (X9ECParameters) pemObj; pemObj = pemReader.readObject(); } if (pemObj instanceof PEMKeyPair) { PrivateKeyInfo pKeyInfo = ((PEMKeyPair) pemObj).getPrivateKeyInfo(); JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter(); privateKey = pemConverter.getPrivateKey(pKeyInfo); } // if our private key is EC type and we have parameters specified // then we need to set it accordingly if (ecParam != null && ECDSA.equals(privateKey.getAlgorithm())) { ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(), 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); } } catch (IOException e) { throw new Exception(e); } return privateKey; }