Example usage for java.security.spec InvalidKeySpecException InvalidKeySpecException

List of usage examples for java.security.spec InvalidKeySpecException InvalidKeySpecException

Introduction

In this page you can find the example usage for java.security.spec InvalidKeySpecException InvalidKeySpecException.

Prototype

public InvalidKeySpecException() 

Source Link

Document

Constructs an InvalidKeySpecException with no detail message.

Usage

From source file:Main.java

private static String getSignatureAlgorithm(Key key) throws Exception {
    if ("EC".equals(key.getAlgorithm())) {
        int curveSize;
        KeyFactory factory = KeyFactory.getInstance("EC");

        if (key instanceof PublicKey) {
            ECPublicKeySpec spec = factory.getKeySpec(key, ECPublicKeySpec.class);
            curveSize = spec.getParams().getCurve().getField().getFieldSize();
        } else if (key instanceof PrivateKey) {
            ECPrivateKeySpec spec = factory.getKeySpec(key, ECPrivateKeySpec.class);
            curveSize = spec.getParams().getCurve().getField().getFieldSize();
        } else {// w w  w.ja  v  a2s .co  m
            throw new InvalidKeySpecException();
        }

        if (curveSize <= 256) {
            return "SHA256withECDSA";
        } else if (curveSize <= 384) {
            return "SHA384withECDSA";
        } else {
            return "SHA512withECDSA";
        }
    } else if ("RSA".equals(key.getAlgorithm())) {
        return "SHA256withRSA";
    } else {
        throw new IllegalArgumentException("Unsupported key type " + key.getAlgorithm());
    }
}