List of usage examples for org.bouncycastle.jcajce.provider.asymmetric.ec BCECPublicKey setPointFormat
public void setPointFormat(String style)
From source file:ca.trustpoint.m2m.util.KeyConversionUtils.java
License:Apache License
/** * Constructs a PublicKey object from raw EC public key data. * * @param rawKey Raw EC public key data. * @return A PublicKey object constructed from the raw EC public key data. * * @throws InvalidKeyException if key type or length is unrecognized. * @throws IOException if raw data reading error. *///from ww w .j av a 2s . c o m public static PublicKey convertRawBytestoEcPublicKey(byte[] rawKey) throws IllegalArgumentException, InvalidKeyException, IOException { if (rawKey == null) { throw new IllegalArgumentException("rawKey cannot be null."); } if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { Security.addProvider(new BouncyCastleProvider()); } boolean isCompressed = isCompressedEcPoint(rawKey); X962Parameters params = null; int keyCompressedLength = 0; // Obtain the length of the compressed public key if (isCompressed) { keyCompressedLength = rawKey.length; } else { keyCompressedLength = (rawKey.length - 1) / 2 + 1; } switch (keyCompressedLength) { case 25: // compressed 192 curve params = new X962Parameters(X9ObjectIdentifiers.prime192v1); break; case 29: // compressed 224 curve params = new X962Parameters(SECObjectIdentifiers.secp224r1); break; case 31: // compressed 233 curve params = new X962Parameters(SECObjectIdentifiers.sect233r1); break; case 33: // compressed 256 curve params = new X962Parameters(X9ObjectIdentifiers.prime256v1); break; case 49: // compressed 384 curve params = new X962Parameters(SECObjectIdentifiers.secp384r1); break; case 67: // compressed 521 curve params = new X962Parameters(SECObjectIdentifiers.secp521r1); break; default: throw new InvalidKeyException("unrecognized public key length: (" + keyCompressedLength + ")"); } AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.toASN1Primitive()); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(algId, rawKey); BCECPublicKey key = (BCECPublicKey) BouncyCastleProvider.getPublicKey(info); if (isCompressed) { key.setPointFormat("COMPRESSED"); } return key; }