Code example for X509EncodedKeySpec

0
 
 
 
    private static final HashMap<String, KeySpec> keys = new HashMap<String, KeySpec>();
    static { 
        keys.put("DH_public", new X509EncodedKeySpec(DH_public));
        keys.put("DH_private", new PKCS8EncodedKeySpec(DH_private));
        keys.put("DSA_public", new X509EncodedKeySpec(DSA_public));
        keys.put("DSA_private", new PKCS8EncodedKeySpec(DSA_private));
        keys.put("RSA_public", new X509EncodedKeySpec(RSA_public));
        keys.put("RSA_private", new PKCS8EncodedKeySpec(RSA_private));
    } 
 
    public static PrivateKey getPrivateKey(String algorithmName) throws NoSuchAlgorithmException, InvalidKeySpecException
    { 
        KeyFactory factory = KeyFactory.getInstance(algorithmName);
        return factory.generatePrivate(keys.get(algorithmName + "_private"));
    } 
 
    public static PublicKey getPublicKey(String algorithmName) throws NoSuchAlgorithmException, InvalidKeySpecException
    {