Java XML Data Type Converter readKey(Reader reader, String algorithm)

Here you can find the source of readKey(Reader reader, String algorithm)

Description

Load PEM string from the reader and extract the public key.

License

Apache License

Parameter

Parameter Description
reader Reader contining Base 64 encoded Public Key (as listed above)
algorithm The algorithm of the key

Exception

Parameter Description
InvalidKeySpecException if the given key specification is inappropriate for this keyfactory to produce a public key.
NoSuchAlgorithmException if no Provider supports a KeyFactorySpi implementation forthe specified algorithm.

Return

instance of

Declaration

public static PublicKey readKey(Reader reader, String algorithm)
        throws NoSuchAlgorithmException, InvalidKeySpecException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.Reader;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Scanner;
import javax.xml.bind.DatatypeConverter;

public class Main {
    private final static String PUBLIC_KEY_PREFIX = "-----BEGIN PUBLIC KEY-----";
    private final static String PUBLIC_KEY_SUFFIX = "-----END PUBLIC KEY-----";

    /**//w w w.  j av  a2 s.c  o m
     * Load PEM string from the reader and extract the public key.
     * 
     * @param reader
     *            Reader contining Base 64 encoded Public Key (as listed
     *            above)
     * @param algorithm
     *            The algorithm of the key
     * @return instance of {@link PublicKey}
     * @throws InvalidKeySpecException
     *             if the given key specification is inappropriate for this key
     *             factory to produce a public key.
     * @throws NoSuchAlgorithmException
     *             if no Provider supports a KeyFactorySpi implementation for
     *             the specified algorithm.
     */
    public static PublicKey readKey(Reader reader, String algorithm)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(reader);

        scanner.useDelimiter("\n");

        StringBuilder builder = new StringBuilder();

        boolean prefixFound = false;

        String line;
        while (scanner.hasNext()) {
            line = scanner.next();
            if (line.equals(PUBLIC_KEY_PREFIX)) {
                prefixFound = true;
                break;
            }
        }

        if (!prefixFound) {
            throw new InvalidKeySpecException("Missing " + PUBLIC_KEY_PREFIX);
        }

        boolean suffixFound = false;
        while (scanner.hasNext()) {
            line = scanner.next();
            if (line.equals(PUBLIC_KEY_SUFFIX)) {
                suffixFound = true;
                break;
            }
            builder.append(line);
        }

        if (!suffixFound) {
            throw new InvalidKeySpecException("Missing " + PUBLIC_KEY_SUFFIX);
        }

        // all whitespace if any
        String pem = builder.toString().replaceAll("\\s", "");

        // generated public key
        return generatePublicKey(pem, algorithm);
    }

    private static PublicKey generatePublicKey(String base64, String algorithm)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        KeyFactory keyfactory = KeyFactory.getInstance(algorithm);
        byte[] data = DatatypeConverter.parseBase64Binary(base64);
        X509EncodedKeySpec keyspec = new X509EncodedKeySpec(data);
        return keyfactory.generatePublic(keyspec);
    }
}

Related

  1. printPositiveDecimal(BigDecimal value)
  2. printPriceType(Long value)
  3. printZahl20(BigInteger value)
  4. printZahl31(BigDecimal value)
  5. printZimmeranzahl(BigDecimal value)
  6. sendJson(URL url, String method, String data, String user, String password)
  7. serializeLogic(Serializable logic)
  8. serializeToString(Object object)
  9. serializeURLSafe(String string)