Java Key Public getPublicKey(String publicKeyContents)

Here you can find the source of getPublicKey(String publicKeyContents)

Description

Returns the public key for the given key.

License

Open Source License

Parameter

Parameter Description
publicKeyContents a parameter

Declaration

public static PublicKey getPublicKey(String publicKeyContents) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) Devamatre Inc. 2009 - 2018. All rights reserved.
 * //from   ww w  .ja  v a2 s  . c  om
 * This code is licensed to Devamatre under one or more contributor license 
 * agreements. The reproduction, transmission or use of this code, in source 
 * and binary forms, with or without modification, are permitted provided 
 * that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * Devamatre reserves the right to modify the technical specifications and or 
 * features without any prior notice.
 *****************************************************************************/

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.Base64;

public class Main {
    public static final String ALGO_RSA = "RSA";

    /**
     * Returns the public key for the given key.
     * 
     * @param publicKeyContents
     * @return
     */
    public static PublicKey getPublicKey(String publicKeyContents) {
        PublicKey publicKey = null;
        try {
            byte[] decodeKeyBytes = decodeToBase64Bytes(publicKeyContents);
            KeyFactory keyFactory = KeyFactory.getInstance(ALGO_RSA);
            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeKeyBytes);
            publicKey = keyFactory.generatePublic(publicKeySpec);
        } catch (NoSuchAlgorithmException ex) {
            System.err.println(ex);
        } catch (InvalidKeySpecException ex) {
            System.err.println(ex);
        }

        return publicKey;
    }

    /**
     * Decode the encrypted string using Base64 decoding.
     * 
     * @param encrypted
     * @return
     */
    public static byte[] decodeToBase64Bytes(String encodedString) {
        return Base64.getDecoder().decode(encodedString);
    }
}

Related

  1. getPublicKey(String filename)
  2. getPublicKey(String filename)
  3. getPublicKey(String filename)
  4. getPublicKey(String key)
  5. getPublicKey(String modulus, String exponent)
  6. getPublicKey(String publicKeyFile)
  7. getPublicKey(String publicKeyFilepath, String algorithm)
  8. getPublicKeyFromBytes(final String algorithm, final byte[] publicKeyBytes)
  9. getPublicKeyFromFile(File cert, String alias, String password)