Java Key Public getPublicKey()

Here you can find the source of getPublicKey()

Description

get Public Key

License

Open Source License

Declaration

public static String getPublicKey() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.security.Key;
import java.security.KeyFactory;

import java.security.PublicKey;

import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;

import sun.misc.BASE64Encoder;

public class Main {
    public static String KEY_PAIRGENO = "RSA";
    public static String PUBLIC_KEY = "PUBLIC_KEY";
    public static HashMap<String, Object> keyMap;

    public static String getPublicKey() {
        Key key = (Key) keyMap.get(PUBLIC_KEY);
        byte[] bytes = key.getEncoded();
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(bytes);
    }//  ww  w. j av  a2s .co  m

    public static PublicKey getPublicKey(byte[] keyBytes) throws Exception {

        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIRGENO);
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        return publicKey;

    }
}

Related

  1. getPublicBytes(KeyPair keyPair)
  2. getPublicDeclaredMethods(Class clz)
  3. getPublicDeclaredMethods(Class clz)
  4. getPublicEncoded(KeyPair kp)
  5. getPublicExponent(PublicKey pubk)
  6. getPublicKey(BigInteger modulus, BigInteger exponent)
  7. getPublicKey(byte[] der)
  8. getPublicKey(byte[] keyBytes, String algorithm)
  9. getPublicKey(final byte[] keyData)