Java Key Public getPublicKey(final String algorithm, final File publicKeyFile)

Here you can find the source of getPublicKey(final String algorithm, final File publicKeyFile)

Description

get Public Key

License

Open Source License

Declaration

private static PublicKey getPublicKey(final String algorithm, final File publicKeyFile) 

Method Source Code

//package com.java2s;
/**// w ww .  ja v a2s .  co m
 * Copyright (C) 2010-2016 Structr GmbH
 *
 * This file is part of Structr <http://structr.org>.
 *
 * Structr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Structr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Structr.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.nio.file.Files;
import java.security.KeyFactory;

import java.security.PublicKey;

import java.security.spec.X509EncodedKeySpec;

public class Main {
    private static PublicKey getPublicKey(final String algorithm, final File publicKeyFile) {
        return getPublicKeyFromBytes(algorithm, readBytes(publicKeyFile));
    }

    private static PublicKey getPublicKeyFromBytes(final String algorithm, final byte[] publicKeyBytes) {

        try {

            final KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            final X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);

            return keyFactory.generatePublic(spec);

        } catch (Throwable t) {
            t.printStackTrace();
        }

        return null;
    }

    private static byte[] readBytes(final File file) {

        try {

            return Files.readAllBytes(file.toPath());

        } catch (Throwable t) {
            t.printStackTrace();
        }

        return null;
    }
}

Related

  1. getPublicKey(BigInteger modulus, BigInteger exponent)
  2. getPublicKey(byte[] der)
  3. getPublicKey(byte[] keyBytes, String algorithm)
  4. getPublicKey(final byte[] keyData)
  5. getPublicKey(final byte[] modulus, final byte[] exponent)
  6. getPublicKey(KeyPair keyPair)
  7. getPublicKey(KeyPair keyPair)
  8. getPublicKey(KeyPair kp)
  9. getPublicKey(KeyStore keyStore, String alias)