use of org.bouncycastle.math.ec.ECCurve in project robovm by robovm.
the class JCEECPublicKey method getEncoded.
public byte[] getEncoded() {
ASN1Encodable params;
SubjectPublicKeyInfo info;
// BEGIN android-removed
// if (algorithm.equals("ECGOST3410"))
// {
// if (gostParams != null)
// {
// params = gostParams;
// }
// else
// {
// if (ecSpec instanceof ECNamedCurveSpec)
// {
// params = new GOST3410PublicKeyAlgParameters(
// ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec)ecSpec).getName()),
// CryptoProObjectIdentifiers.gostR3411_94_CryptoProParamSet);
// }
// else
// { // strictly speaking this may not be applicable...
// ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
//
// X9ECParameters ecP = new X9ECParameters(
// curve,
// EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
// ecSpec.getOrder(),
// BigInteger.valueOf(ecSpec.getCofactor()),
// ecSpec.getCurve().getSeed());
//
// params = new X962Parameters(ecP);
// }
// }
//
// BigInteger bX = this.q.getX().toBigInteger();
// BigInteger bY = this.q.getY().toBigInteger();
// byte[] encKey = new byte[64];
//
// extractBytes(encKey, 0, bX);
// extractBytes(encKey, 32, bY);
//
// try
// {
// info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), new DEROctetString(encKey));
// }
// catch (IOException e)
// {
// return null;
// }
// }
// else
// END android-removed
{
if (ecSpec instanceof ECNamedCurveSpec) {
ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
if (curveOid == null) {
curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
}
params = new X962Parameters(curveOid);
} else if (ecSpec == null) {
params = new X962Parameters(DERNull.INSTANCE);
} else {
ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
X9ECParameters ecP = new X9ECParameters(curve, EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
params = new X962Parameters(ecP);
}
ECCurve curve = this.engineGetQ().getCurve();
ASN1OctetString p = (ASN1OctetString) new X9ECPoint(curve.createPoint(this.getQ().getX().toBigInteger(), this.getQ().getY().toBigInteger(), withCompression)).toASN1Primitive();
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
}
return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
use of org.bouncycastle.math.ec.ECCurve in project habot by ghys.
the class Utils method loadPublicKey.
/**
* Load the public key from a URL-safe base64 encoded string. Takes into
* account the different encodings, including point compression.
*
* @param encodedPublicKey
*/
public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
byte[] decodedPublicKey = base64Decode(encodedPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
ECCurve curve = parameterSpec.getCurve();
ECPoint point = curve.decodePoint(decodedPublicKey);
ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);
return keyFactory.generatePublic(pubSpec);
}
use of org.bouncycastle.math.ec.ECCurve in project openremote by openremote.
the class ProvisioningPublicKeyState method generateSharedECDHSecret.
private void generateSharedECDHSecret(final byte[] provisioneePublicKeyXYPDU) {
if (provisioneePublicKeyXYPDU.length != 66) {
throw new IllegalArgumentException("Invalid Provisionee Public Key PDU," + " length of the Provisionee public key must be 66 bytes, but was " + provisioneePublicKeyXYPDU.length);
}
final ByteBuffer buffer = ByteBuffer.allocate(provisioneePublicKeyXYPDU.length - 2);
buffer.put(provisioneePublicKeyXYPDU, 2, buffer.limit());
final byte[] xy = mTempProvisioneeXY = buffer.array();
mUnprovisionedMeshNode.setProvisioneePublicKeyXY(xy);
final byte[] xComponent = new byte[32];
System.arraycopy(xy, 0, xComponent, 0, xComponent.length);
final byte[] yComponent = new byte[32];
System.arraycopy(xy, 32, yComponent, 0, xComponent.length);
final byte[] provisioneeX = convertToLittleEndian(xComponent, ByteOrder.LITTLE_ENDIAN);
LOG.info("Provsionee X: " + MeshParserUtils.bytesToHex(provisioneeX, false));
final byte[] provisioneeY = convertToLittleEndian(yComponent, ByteOrder.LITTLE_ENDIAN);
LOG.info("Provsionee Y: " + MeshParserUtils.bytesToHex(provisioneeY, false));
final BigInteger x = BigIntegers.fromUnsignedByteArray(xy, 0, 32);
final BigInteger y = BigIntegers.fromUnsignedByteArray(xy, 32, 32);
final ECParameterSpec ecParameters = ECNamedCurveTable.getParameterSpec("secp256r1");
ECCurve curve = ecParameters.getCurve();
ECPoint ecPoint = curve.createPoint(x, y);
ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, ecParameters);
KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance("ECDH", "SC");
ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
KeyAgreement a = KeyAgreement.getInstance("ECDH", "SC");
a.init(mProvisionerPrivaetKey);
a.doPhase(publicKey, true);
final byte[] sharedECDHSecret = a.generateSecret();
mUnprovisionedMeshNode.setSharedECDHSecret(sharedECDHSecret);
LOG.info("ECDH Secret: " + MeshParserUtils.bytesToHex(sharedECDHSecret, false));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
use of org.bouncycastle.math.ec.ECCurve in project hutool by looly.
the class BCUtil method decodeECPoint.
/**
* 解码恢复EC压缩公钥,支持Base64和Hex编码,(基于BouncyCastle)
*
* @param encodeByte 压缩公钥
* @param curveName EC曲线名,例如{@link SmUtil#SM2_DOMAIN_PARAMS}
* @return 公钥
* @since 4.4.4
*/
public static PublicKey decodeECPoint(byte[] encodeByte, String curveName) {
final X9ECParameters x9ECParameters = ECUtil.getNamedCurveByName(curveName);
final ECCurve curve = x9ECParameters.getCurve();
final ECPoint point = EC5Util.convertPoint(curve.decodePoint(encodeByte));
// 根据曲线恢复公钥格式
final ECNamedCurveSpec ecSpec = new ECNamedCurveSpec(curveName, curve, x9ECParameters.getG(), x9ECParameters.getN());
return KeyUtil.generatePublicKey("EC", new ECPublicKeySpec(point, ecSpec));
}
Aggregations