Java Code Examples for org.bouncycastle.util.BigIntegers#fromUnsignedByteArray()

The following examples show how to use org.bouncycastle.util.BigIntegers#fromUnsignedByteArray() . 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: ECPointsCompact.java    From InflatableDonkey with MIT License 6 votes vote down vote up
@Deprecated
public static ECPoint decodeFPPoint(ECCurve curve, byte[] data) {
    // Patched org.bouncycastle.math.ec.ECCurve#decodePoint code.
    int expectedLength = (curve.getFieldSize() + 7) / 8;
    if (expectedLength != data.length) {
        throw new IllegalArgumentException("incorrect data length for compact encoding");
    }

    BigInteger X = BigIntegers.fromUnsignedByteArray(data, 0, expectedLength);
    ECPoint p = decompressFPPoint(curve, X);

    if (!satisfiesCofactor(curve, p)) {
        throw new IllegalArgumentException("invalid point");
    }

    return p;
}
 
Example 2
Source File: ECPublicKeyImportX963.java    From InflatableDonkey with MIT License 6 votes vote down vote up
@Override
public Optional<ECPublicKey> importKey(String curveName, byte[] data) {
    int fieldLength = ECAssistant.fieldLength(curveName);
    if (fieldLength(data.length) != fieldLength) {
        logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}",
                data.length, curveName, Hex.toHexString(data));
    }

    if (!checkType(data[0])) {
        logger.warn("-- importKey() - bad data type: 0x{}", Integer.toHexString(data[0]));
    }

    BigInteger x = BigIntegers.fromUnsignedByteArray(data, 1, fieldLength);
    BigInteger y = BigIntegers.fromUnsignedByteArray(data, 1 + fieldLength, fieldLength);

    return ECKeyFactories.publicKeyFactory()
            .createECPublicKey(x, y, curveName);
}
 
Example 3
Source File: ECPrivateKeyImportCompact.java    From InflatableDonkey with MIT License 6 votes vote down vote up
@Override
public Optional<ECPrivateKey> importKey(String curveName, byte[] data) {
    X9ECParameters x9ECParameters = ECAssistant.x9ECParameters(curveName);
    int fieldLength = ECAssistant.fieldLength(x9ECParameters);
    if (fieldLength(data.length) != fieldLength) {
        logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}",
                data.length, curveName, Hex.toHexString(data));
    }


    BigInteger x = BigIntegers.fromUnsignedByteArray(data, 0, fieldLength);
    BigInteger y = ECPointsCompact.y(x9ECParameters.getCurve(), x);
    BigInteger d = BigIntegers.fromUnsignedByteArray(data, fieldLength, fieldLength);

    return ECKeyFactories.privateKeyFactory()
            .createECPrivateKey(x, y, d, curveName);
}
 
Example 4
Source File: Utils.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Load the private key from a URL-safe base64 encoded string
 *
 * @param encodedPrivateKey
 * @return
 * @throws NoSuchProviderException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static PrivateKey loadPrivateKey(String encodedPrivateKey)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPrivateKey = base64Decode(encodedPrivateKey);
    BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey);
    ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);

    return keyFactory.generatePrivate(privateKeySpec);
}
 
Example 5
Source File: ECPublicKeyImportCompact.java    From InflatableDonkey with MIT License 5 votes vote down vote up
@Override
public Optional<ECPublicKey> importKey(String curveName, byte[] data) {
    X9ECParameters x9ECParameters = ECAssistant.x9ECParameters(curveName);
    int fieldLength = ECAssistant.fieldLength(x9ECParameters);
    if (fieldLength(data.length) != fieldLength) {
        logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}",
                data.length, curveName, Hex.toHexString(data));
    }

    BigInteger x = BigIntegers.fromUnsignedByteArray(data);
    BigInteger y = ECPointsCompact.y(x9ECParameters.getCurve(), x);

    return ECKeyFactories.publicKeyFactory()
            .createECPublicKey(x, y, curveName);
}
 
Example 6
Source File: ECPrivateKeyImport.java    From InflatableDonkey with MIT License 5 votes vote down vote up
@Override
public Optional<ECPrivateKey> importKey(String curveName, byte[] data) {
    int fieldLength = ECAssistant.fieldLength(curveName);
    if (fieldLength(data.length) != fieldLength) {
        logger.warn("-- importKey() - bad data length: {} curve: {} data:0x{}",
                data.length, curveName, Hex.toHexString(data));
    }

    BigInteger d = BigIntegers.fromUnsignedByteArray(data, 0, fieldLength);

    return ECKeyFactories.privateKeyFactory()
            .createECPrivateKey(d, curveName);
}
 
Example 7
Source File: KeyUtils.java    From aerogear-unifiedpush-server with Apache License 2.0 5 votes vote down vote up
public static PrivateKey loadPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPrivateKey = Base64Encoder.decode(privateKey);
    BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER);

    return keyFactory.generatePrivate(privateKeySpec);
}
 
Example 8
Source File: Utils.java    From webpush-java with MIT License 3 votes vote down vote up
/**
 * Load the private key from a byte array
 *
 * @param decodedPrivateKey
 * @return
 * @throws NoSuchProviderException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static PrivateKey loadPrivateKey(byte[] decodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);

    return keyFactory.generatePrivate(privateKeySpec);
}