Search in sources :

Example 1 with ECPrivateKey

use of com.github.zhenwei.provider.jce.interfaces.ECPrivateKey in project LinLong-Java by zhenwei1108.

the class ECUtil method generatePrivateKeyParameter.

public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
    if (key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = WeGooProvider.CONFIGURATION.getEcImplicitlyCa();
        }
        if (k.getParameters() instanceof ECNamedCurveParameterSpec) {
            String name = ((ECNamedCurveParameterSpec) k.getParameters()).getName();
            return new ECPrivateKeyParameters(k.getD(), new ECNamedDomainParameters(ECNamedCurveTable.getOID(name), s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        } else {
            return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        }
    } else if (key instanceof java.security.interfaces.ECPrivateKey) {
        java.security.interfaces.ECPrivateKey privKey = (java.security.interfaces.ECPrivateKey) key;
        ECParameterSpec s = EC5Util.convertSpec(privKey.getParams());
        return new ECPrivateKeyParameters(privKey.getS(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else {
        // see if we can build a key from key.getEncoded()
        try {
            byte[] bytes = key.getEncoded();
            if (bytes == null) {
                throw new InvalidKeyException("no encoding for EC private key");
            }
            PrivateKey privateKey = WeGooProvider.getPrivateKey(PrivateKeyInfo.getInstance(bytes));
            if (privateKey instanceof java.security.interfaces.ECPrivateKey) {
                return ECUtil.generatePrivateKeyParameter(privateKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC private key: " + e.toString());
        }
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(com.github.zhenwei.provider.jce.interfaces.ECPrivateKey) ECPrivateKey(com.github.zhenwei.provider.jce.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) ECDomainParameters(com.github.zhenwei.core.crypto.params.ECDomainParameters) ECNamedDomainParameters(com.github.zhenwei.core.crypto.params.ECNamedDomainParameters) InvalidKeyException(java.security.InvalidKeyException) InvalidKeyException(java.security.InvalidKeyException) ECPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters) ECParameterSpec(com.github.zhenwei.provider.jce.spec.ECParameterSpec) ECNamedCurveParameterSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec)

Aggregations

ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)1 ECNamedDomainParameters (com.github.zhenwei.core.crypto.params.ECNamedDomainParameters)1 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)1 ECPrivateKey (com.github.zhenwei.provider.jce.interfaces.ECPrivateKey)1 ECNamedCurveParameterSpec (com.github.zhenwei.provider.jce.spec.ECNamedCurveParameterSpec)1 ECParameterSpec (com.github.zhenwei.provider.jce.spec.ECParameterSpec)1 InvalidKeyException (java.security.InvalidKeyException)1 PrivateKey (java.security.PrivateKey)1