Java Code Examples for org.bouncycastle.math.ec.ECCurve#decodePoint()

The following examples show how to use org.bouncycastle.math.ec.ECCurve#decodePoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * @param xBytes           十六进制形式的公钥x分量,如果是SM2算法,应该是32字节
 * @param yBytes           十六进制形式的公钥y分量,如果是SM2算法,应该是32字节
 * @param curve            EC曲线参数,一般是固定的,如果是SM2算法的可参考{@link SM2Util#CURVE}
 * @param domainParameters EC Domain参数,一般是固定的,如果是SM2算法的可参考{@link SM2Util#DOMAIN_PARAMS}
 * @return
 */
public static ECPublicKeyParameters createECPublicKeyParameters(
        byte[] xBytes, byte[] yBytes, ECCurve curve, ECDomainParameters domainParameters) {
    final byte uncompressedFlag = 0x04;
    int curveLength = getCurveLength(domainParameters);
    xBytes = fixToCurveLengthBytes(curveLength, xBytes);
    yBytes = fixToCurveLengthBytes(curveLength, yBytes);
    byte[] encodedPubKey = new byte[1 + xBytes.length + yBytes.length];
    encodedPubKey[0] = uncompressedFlag;
    System.arraycopy(xBytes, 0, encodedPubKey, 1, xBytes.length);
    System.arraycopy(yBytes, 0, encodedPubKey, 1 + xBytes.length, yBytes.length);
    return new ECPublicKeyParameters(curve.decodePoint(encodedPubKey), domainParameters);
}
 
Example 2
Source File: BCECUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
public static ECPublicKeyParameters createEcPublicKey(byte[] xBytes, byte[] yBytes,
                                                      ECCurve curve, ECDomainParameters domainParameters) {
    final byte uncompressedFlag = 0x04;
    byte[] encodedPubKey = new byte[1 + xBytes.length + yBytes.length];
    encodedPubKey[0] = uncompressedFlag;
    System.arraycopy(xBytes, 0, encodedPubKey, 1, xBytes.length);
    System.arraycopy(yBytes, 0, encodedPubKey, 1 + xBytes.length, yBytes.length);
    return new ECPublicKeyParameters(curve.decodePoint(encodedPubKey), domainParameters);
}
 
Example 3
Source File: Utils.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: Utils.java    From webpush-java with MIT License 5 votes vote down vote up
/**
 * Load the public key from a byte array. 
 *
 * @param decodedPublicKey
 */
public static PublicKey loadPublicKey(byte[] decodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    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);
}
 
Example 5
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 5 votes vote down vote up
public static ECPublicKeyParameters createECPublicKeyParameters(byte[] xBytes, byte[] yBytes,
    ECCurve curve, ECDomainParameters domainParameters) {
    final byte uncompressedFlag = 0x04;
    int curveLength = getCurveLength(domainParameters);
    xBytes = fixToCurveLengthBytes(curveLength, xBytes);
    yBytes = fixToCurveLengthBytes(curveLength, yBytes);
    byte[] encodedPubKey = new byte[1 + xBytes.length + yBytes.length];
    encodedPubKey[0] = uncompressedFlag;
    System.arraycopy(xBytes, 0, encodedPubKey, 1, xBytes.length);
    System.arraycopy(yBytes, 0, encodedPubKey, 1 + xBytes.length, yBytes.length);
    return new ECPublicKeyParameters(curve.decodePoint(encodedPubKey), domainParameters);
}
 
Example 6
Source File: RFC6637.java    From InflatableDonkey with MIT License 5 votes vote down vote up
ECPoint decodePoint(byte[] data) {
    ECCurve curve = ECNamedCurveTable.getByName(curveName).getCurve();
    int compactExportSize = (curve.getFieldSize() + 7) / 8;

    return data.length == compactExportSize
            ? ECPointsCompact.decodeFPPoint(curve, data) // Compact keys support, non RFC6636 compliant.
            : curve.decodePoint(data);
}
 
Example 7
Source File: KeyUtils.java    From aerogear-unifiedpush-server with Apache License 2.0 5 votes vote down vote up
public static PublicKey loadPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPublicKey = Base64Encoder.decode(publicKey);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECCurve curve = parameterSpec.getCurve();
    ECPoint point = curve.decodePoint(decodedPublicKey);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);

    return keyFactory.generatePublic(pubSpec);

}