Example usage for java.security.spec InvalidKeySpecException toString

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:umu.eadmin.servicios.umu2stork.UtilesRsa.java

public PrivateKey readPrivateKey(String filename) throws IOException {
    try {//from w  ww  .j  a  v a  2  s  .co  m
        logger.info("Start decoding RSA key in PEM format: " + filename);
        File keyFile = new File(filename);

        BufferedReader br = new BufferedReader(new FileReader(keyFile));
        StringBuffer keyBase64 = new StringBuffer();
        String line = br.readLine();
        while (line != null) {
            if (!(line.startsWith("-----BEGIN")) && !(line.startsWith("-----END"))) {
                keyBase64.append(line);
            }
            line = br.readLine();
        }
        br.close();
        logger.info("Decode RSA Key");
        byte[] fileBytes = new BASE64Decoder().decodeBuffer(keyBase64.toString());

        PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(fileBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");

        logger.info("RSA Decode End: " + filename);
        pk = kf.generatePrivate(ks);
        return (pk);
    } catch (InvalidKeySpecException ex1) {
        throw new IOException("Invalid Key Spec: " + ex1.toString());
    } catch (NoSuchAlgorithmException ex2) {
        throw new IOException("No Such Algorithm: " + ex2.toString());
    }
}