Java Key Public getPublicKey(String algo)

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

Description

get Public Key

License

Apache License

Declaration

public static PublicKey getPublicKey(String algo) throws InvalidKeySpecException, NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
/**//from  ww w .  j ava 2s .  c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.math.BigInteger;
import java.security.KeyFactory;

import java.security.NoSuchAlgorithmException;

import java.security.PublicKey;

import java.security.spec.DSAPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import java.security.spec.RSAPublicKeySpec;

public class Main {
    private static final String DSA_Y = "07066284216756577193658833512863439617178933165631848358445549382240081120085333137303066923542492834619027404463194956043802393462371310375123430985057160";
    private static final String DSA_P = "013232376895198612407547930718267435757728527029623408872245156039757713029036368719146452186041204237350521785240337048752071462798273003935646236777459223";
    private static final String DSA_Q = "0857393771208094202104259627990318636601332086981";
    private static final String DSA_G = "05421644057436475141609648488325705128047428394380474376834667300766108262613900542681289080713724597310673074119355136085795982097390670890367185141189796";
    private static final String DSA_2048_Y = "15119007057343785981993995134621348945077524760182795513668325877793414638620983617627033248732235626178802906346261435991040697338468329634416089753032362617771631199351767336660070462291411472735835843440140283101463231807789628656218830720378705090795271104661936237385140354825159080766174663596286149653433914842868551355716015585570827642835307073681358328172009941968323702291677280809277843998510864653406122348712345584706761165794179850728091522094227603562280855104749858249588234915206290448353957550635709520273178475097150818955098638774564910092913714625772708285992586894795017709678223469405896699928";
    private static final String DSA_2048_P = "18111848663142005571178770624881214696591339256823507023544605891411707081617152319519180201250440615163700426054396403795303435564101919053459832890139496933938670005799610981765220283775567361483662648340339405220348871308593627647076689407931875483406244310337925809427432681864623551598136302441690546585427193224254314088256212718983105131138772434658820375111735710449331518776858786793875865418124429269409118756812841019074631004956409706877081612616347900606555802111224022921017725537417047242635829949739109274666495826205002104010355456981211025738812433088757102520562459649777989718122219159982614304359";
    private static final String DSA_2048_Q = "19689526866605154788513693571065914024068069442724893395618704484701";
    private static final String DSA_2048_G = "2859278237642201956931085611015389087970918161297522023542900348087718063098423976428252369340967506010054236052095950169272612831491902295835660747775572934757474194739347115870723217560530672532404847508798651915566434553729839971841903983916294692452760249019857108409189016993380919900231322610083060784269299257074905043636029708121288037909739559605347853174853410208334242027740275688698461842637641566056165699733710043802697192696426360843173620679214131951400148855611740858610821913573088059404459364892373027492936037789337011875710759208498486908611261954026964574111219599568903257472567764789616958430";
    private static final String RSA_MOD = "010800185049102889923150759252557522305032794699952150943573164381936603255999071981574575044810461362008102247767482738822150129277490998033971789476107463";
    private static final String RSA_PUB = "065537";

    public static PublicKey getPublicKey(String algo) throws InvalidKeySpecException, NoSuchAlgorithmException {
        if (algo.equalsIgnoreCase("DSA")) {
            return getPublicKey("DSA", 1024);
        } else if (algo.equalsIgnoreCase("RSA")) {
            return getPublicKey("RSA", 512);
        } else {
            throw new RuntimeException("Unsupported key algorithm " + algo);
        }
    }

    public static PublicKey getPublicKey(String algo, int keysize)
            throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory kf = KeyFactory.getInstance(algo);
        KeySpec kspec;
        if (algo.equalsIgnoreCase("DSA")) {
            if (keysize == 1024) {
                kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y), new BigInteger(DSA_P), new BigInteger(DSA_Q),
                        new BigInteger(DSA_G));
            } else if (keysize == 2048) {
                kspec = new DSAPublicKeySpec(new BigInteger(DSA_2048_Y), new BigInteger(DSA_2048_P),
                        new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
            } else {
                throw new RuntimeException("Unsupported keysize:" + keysize);
            }
        } else if (algo.equalsIgnoreCase("RSA")) {
            if (keysize == 512) {
                kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PUB));
            } else {
                throw new RuntimeException("Unsupported keysize:" + keysize);
            }
        } else {
            throw new RuntimeException("Unsupported key algorithm " + algo);
        }
        return kf.generatePublic(kspec);
    }
}

Related

  1. getPublicKey(KeyPair kp)
  2. getPublicKey(KeyStore keyStore, String alias)
  3. getPublicKey(KeyStore keyStore, String alias)
  4. getPublicKey(KeyStore keyStore, String alias, char[] password)
  5. getPublicKey(KeyStore ks, String alias, char[] password)
  6. getPublicKey(String alias)
  7. getPublicKey(String certificatePath)
  8. getPublicKey(String certPath)
  9. getPublicKey(String filename)