JAVA加载keystore文件,获取公钥和私钥

时间:2025-02-14 19:34:39

总结java如何加载keystore文件,获取公私钥信息

    /**
     * Java密钥库(Java Key Store,JKS)KEY_STORE
     */
    private static final String KEY_STORE = "JKS";

    private static final String X509 = "X.509";

    /**
     * 从公钥文件当中读取公钥,公钥文件存储在设备某个特定位置
     * 
     * @param publicKeyFilePath 公钥文件存放路径
     * @return
     */
    public static String loadPublicKey(String publicKeyFilePath) throws Exception {
        String publicKey = "";
        File file = new File(publicKeyFilePath);
        if (()) {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            publicKey = ();
            // ("publicKey: " + publicKey);
            ();
        } else {
            throw new RuntimeException("PublicKey File not exist!");
        }
        return publicKey;
    }

    /**
     * 从私钥文件当中读取私钥,私钥文件存储在我们自己的签名工具当中
     * 
     * @param privateFilePath 私钥文件存储路径
     * @return
     */
    public static String loadPrivateKey(String privateFilePath) throws Exception {
        String privateKey = "";
        File file = new File(privateFilePath);
        if (()) {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            privateKey = ();
            // ("privateKey: " + privateKey);
            ();
        } else {
            throw new RuntimeException("PublicKey File not exist!");
        }
        return privateKey;
    }

    /**
     * 获得KeyStore
     * 
     * @param keyStorePath
     * @param password
     */
    private static KeyStore getKeyStore(String keyStorePath, String password)
            throws Exception {

        FileInputStream is = new FileInputStream(keyStorePath);
        KeyStore ks = (KEY_STORE);
        (is, ());
        ();
        return ks;
    }

    /**
     * 由KeyStore获得私钥
     * 
     * @param keyStorePath
     * @param alias
     * @param storePass
     */
    private static PrivateKey getPrivateKey(String keyStorePath, String alias, String storePass,
            String keyPass) throws Exception {
        KeyStore ks = getKeyStore(keyStorePath, storePass);
        PrivateKey key = (PrivateKey) (alias, ());
        return key;
    }

    /**
     * 由Certificate获得公钥
     * 
     * @param keyStorePath KeyStore路径
     * @param alias 别名
     * @param storePass KeyStore访问密码
     */
    private static PublicKey getPublicKey(String keyStorePath, String alias, String storePass)
            throws Exception {
        KeyStore ks = getKeyStore(keyStorePath, storePass);
        PublicKey key = (alias).getPublicKey();
        return key;
    }

    /**
     * 从KeyStore中获取公钥,并经BASE64编码
     * @param keyStorePath
     * @param alias
     * @param storePass
     */
    public static String getStrPublicKey(String keyStorePath, String alias, String storePass)
            throws Exception {
        PublicKey key = getPublicKey(keyStorePath, alias, storePass);
        String strKey = (());
        return strKey;
    }

    /**
     * 获取经BASE64编码后的私钥
     * @param alias
     * @param storePass
     * @param keyPass
     */
    public static String getStrPrivateKey(String keyStorePath, String alias, String storePass,
            String keyPass) throws Exception {

        PrivateKey key = getPrivateKey(keyStorePath, alias, storePass, keyPass);
        String strKey = (());
        return strKey;
    }

    public static void main(String args[]) throws Exception {
        // ("");
        // ("");

        String publicKey = ("", "appstore", "123456");
        ("publicKey: " + publicKey);
        String privateKey = ("", "appstore",
                "123456", "123456");
        ("privateKey: " + privateKey);
    }

Base64Utils的内容如下(import .Base64):

Base64来自apache包commons-codec-1.


    /**
     * 文件读取缓冲区大小
     */
    private static final int CACHE_SIZE = 1024;

    /**
     * BASE64字符串解码为二进制数据
     * 
     * @param base64
     */
    public static byte[] decode(String base64) throws Exception {
        return Base64.decodeBase64(());
    }

    /**
     * 二进制数据编码为BASE64字符串
     * @param bytes
     */
    public static String encode(byte[] bytes) throws Exception {
        return new String(Base64.encodeBase64(bytes));
    }

    /**
     * 将文件编码为BASE64字符串
     * 大文件慎用,可能会导致内存溢出
     * @param filePath 文件绝对路径
     */
    public static String encodeFile(String filePath) throws Exception {
        byte[] bytes = fileToByte(filePath);
        return encode(bytes);
    }

    /**
     * BASE64字符串转回文件
     * @param filePath 文件绝对路径
     * @param base64 编码字符串
     */
    public static void decodeToFile(String filePath, String base64) throws Exception {
        byte[] bytes = decode(base64);
        byteArrayToFile(bytes, filePath);
    }

    /**
     * 文件转换为二进制数组
     * @param filePath 文件路径
     */
    public static byte[] fileToByte(String filePath) throws Exception {
        byte[] data = new byte[0];
        File file = new File(filePath);
        if (()) {
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = (cache)) != -1) {
                (cache, 0, nRead);
                ();
            }
            ();
            ();
            data = ();
        }
        return data;
    }

    /**
     * 二进制数据写文件
     * @param bytes 二进制数据
     * @param filePath 文件生成目录
     */
    public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
        InputStream in = new ByteArrayInputStream(bytes);
        File destFile = new File(filePath);
        if (!().exists()) {
            ().mkdirs();
        }
        ();
        OutputStream out = new FileOutputStream(destFile);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = (cache)) != -1) {
            (cache, 0, nRead);
            ();
        }
        ();
        ();
    }