List of usage examples for org.bouncycastle.jce ECPointUtil decodePoint
public static ECPoint decodePoint(EllipticCurve curve, byte[] encoded)
From source file:com.facebook.delegatedrecovery.DelegatedRecoveryConfiguration.java
License:Open Source License
/** * Turn the JSON public key array from a configuration into a set of usable * public keys for ECDSA on secp256r1//from w w w . j a va 2 s . c o m * * @param array The JSON public key array * @return array of public keys decoded from the JSON array of base64 encoded * strings */ protected static ECPublicKey[] keysFromJsonArray(final JsonArray array) { try { final ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1"); final KeyFactory kf = KeyFactory.getInstance("EC", new BouncyCastleProvider()); final ECNamedCurveSpec params = new ECNamedCurveSpec("prime256v1", spec.getCurve(), spec.getG(), spec.getN()); final ArrayList<ECPublicKey> pubKeys = new ArrayList<ECPublicKey>(array.size()); for (int i = 0; i < array.size(); i++) { final String b64 = array.getString(i); final byte[] pubKeyAsn1 = Base64.getDecoder().decode(b64); final byte[] pubKey = new byte[pubKeyAsn1.length - PEM_ASN1_PREFIX.length]; // trim // PEM // ASN.1 // prefix System.arraycopy(pubKeyAsn1, PEM_ASN1_PREFIX.length, pubKey, 0, pubKey.length); final ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey); final ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params); try { final ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec); pubKeys.add(pk); } catch (InvalidKeySpecException e) { System.err.println("InvalidKeySpecException while processing " + b64); } } return pubKeys.toArray(new ECPublicKey[pubKeys.size()]); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); System.err.println("Unable to initialize ECDSA key factor for prime256v1. Cannot continue."); System.exit(1); return null; // unreachable but Eclipse complier wants me to return // something. :P } }
From source file:com.vvote.thirdparty.ximix.util.BLSKeyStore.java
License:Apache License
/** * Return the key store object as a PKCS#12 byte array. * * @param password the password to use to encrypt the key data. * @return an array of bytes representing the encoding. * @throws IOException on a conversion to ASN.1 encoding error. * @throws GeneralSecurityException if there is an issue encrypting the key data. *//*from www . j av a 2 s . co m*/ public synchronized byte[] getEncoded(char[] password) throws IOException, GeneralSecurityException { KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC"); EllipticCurve curve = new EllipticCurve( new ECFieldFp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839")), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECParameterSpec spec = new ECParameterSpec(curve, ECPointUtil.decodePoint(curve, Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"), // n 1); // h // TODO: neeed an EC key for the node ECPrivateKeySpec priKeySpec = new ECPrivateKeySpec( new BigInteger("876300101507107567501066130761671078357010671067781776716671676178726717"), // d spec); try { OutputEncryptor encOut = new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC) .setProvider("BC").build(password); JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils(); PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder(); for (String keyID : sharedPrivateKeyMap.keySet()) { PrivateKey sigKey = fact.generatePrivate(priKeySpec); SubjectPublicKeyInfo pubKey = this.fetchPublicKey(keyID); PKCS12SafeBagBuilder eeCertBagBuilder = new PKCS12SafeBagBuilder( createCertificate(keyID, sequenceNoMap.get(keyID), sigKey)); eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID)); SubjectKeyIdentifier pubKeyId = extUtils.createSubjectKeyIdentifier(pubKey); eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId); PKCS12SafeBagBuilder keyBagBuilder = new PKCS12SafeBagBuilder(PrivateKeyInfoFactory .createPrivateKeyInfo(sharedPrivateKeyMap.get(keyID), paramsMap.get(keyID)), encOut); keyBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID)); keyBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId); builder.addEncryptedData( new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC) .setProvider("BC").build(password), new PKCS12SafeBag[] { eeCertBagBuilder.build() }); builder.addData(keyBagBuilder.build()); } PKCS12PfxPdu pfx = builder.build(new JcePKCS12MacCalculatorBuilder(NISTObjectIdentifiers.id_sha256), password); return pfx.getEncoded(ASN1Encoding.DL); } catch (PKCSException e) { throw new GeneralSecurityException("Unable to create key store: " + e.getMessage(), e); } catch (OperatorCreationException e) { throw new GeneralSecurityException("Unable to create operator: " + e.getMessage(), e); } }
From source file:cvc.TestPublicKey.java
License:Open Source License
private KeyPair createECKeyPair() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC"); EllipticCurve curve = new EllipticCurve( new ECFieldFp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839")), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECParameterSpec spec = new ECParameterSpec(curve, ECPointUtil.decodePoint(curve, Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"), // n 3); // h keyGen.initialize(spec);// w ww . ja v a2s. c om return keyGen.generateKeyPair(); }
From source file:edu.tamu.tcat.crypto.bouncycastle.ASN1SeqKeyImpl.java
License:Apache License
private static PrivateKey decodeECKey(byte[] encodedKey) throws EncodingException { try {/*w w w . j av a 2s . co m*/ ECPrivateKey priv = ECPrivateKey.getInstance(encodedKey); ASN1Sequence parameters = (ASN1Sequence) priv.getParameters(); ASN1Integer version = (ASN1Integer) parameters.getObjectAt(0); if (version.getPositiveValue().intValue() != 1) throw new EncodingException("Only know how to decode version 1"); ASN1Sequence fieldId = (ASN1Sequence) parameters.getObjectAt(1); ASN1Encodable fieldType = fieldId.getObjectAt(0); ECField field; if (fieldType.toString().equals("1.2.840.10045.1.1")) { ASN1Integer primeObject = (ASN1Integer) fieldId.getObjectAt(1); field = new ECFieldFp(primeObject.getPositiveValue()); } else throw new EncodingException("Only know how to decode prime fields"); ASN1Sequence curveSeq = (ASN1Sequence) parameters.getObjectAt(2); ASN1OctetString a = (ASN1OctetString) curveSeq.getObjectAt(0); ASN1OctetString b = (ASN1OctetString) curveSeq.getObjectAt(1); EllipticCurve curve; if (curveSeq.size() > 2) { DERBitString seed = (DERBitString) curveSeq.getObjectAt(2); curve = new EllipticCurve(field, getInteger(a.getOctets()), getInteger(b.getOctets()), seed.getBytes()); } else curve = new EllipticCurve(field, getInteger(a.getOctets()), getInteger(b.getOctets())); ASN1OctetString gEncoded = (ASN1OctetString) parameters.getObjectAt(3); ECPoint g = ECPointUtil.decodePoint(curve, gEncoded.getOctets()); ASN1Integer n = (ASN1Integer) parameters.getObjectAt(4); ASN1Integer h = (ASN1Integer) parameters.getObjectAt(5); ECParameterSpec paramSpec = new ECParameterSpec(curve, g, n.getPositiveValue(), h.getPositiveValue().intValue()); ECPrivateKeySpec spec = new ECPrivateKeySpec(priv.getKey(), paramSpec); KeyFactory factory = KeyFactory.getInstance("EC", Activator.getDefault().getBouncyCastleProvider()); PrivateKey key = factory.generatePrivate(spec); return key; } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new EncodingException("Failed decoding type [EC]", e); } }
From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java
License:Apache License
/** * Decode based on X, Y 32 byte integers * /*from ww w .j a v a 2s .c o m*/ * @param pubKey * @param curveName * - Example secp256r1 * @return * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws NoSuchProviderException */ public static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curveName); KeyFactory kf = KeyFactory.getInstance("ECDSA", new BouncyCastleProvider()); ECNamedCurveSpec params = new ECNamedCurveSpec(curveName, 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; }