DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 经验分享 > Java开发

Java读取证书、公钥、私钥

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Enumeration;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import com.help.Conf_Info;

public class VerifiSign2
{
    public void main(String[] args)
    {
	getPrivateKeyInfo();
	getPublicKeyInfo();
    }

    /**
     * 获取私钥别名等信息
     */
    public String getPrivateKeyInfo()
    {
	String privKeyFileString = Conf_Info.PrivatePath;
	String privKeyPswdString = "" + Conf_Info.password;
	String keyAlias = null;
	try
	{
	    KeyStore keyStore = KeyStore.getInstance("PKCS12");
	    FileInputStream fileInputStream = new FileInputStream(privKeyFileString);
	    char[] nPassword = null;
	    if ((privKeyPswdString == null) || privKeyPswdString.trim().equals(""))
	    {
		nPassword = null;
	    } else
	    {
		nPassword = privKeyPswdString.toCharArray();
	    }
	    keyStore.load(fileInputStream, nPassword);
	    fileInputStream.close();
	    System.out.println("keystore type=" + keyStore.getType());

	    Enumeration enumeration = keyStore.aliases();

	    if (enumeration.hasMoreElements())
	    {
		keyAlias = (String) enumeration.nextElement();
		System.out.println("alias=[" + keyAlias + "]");
	    }
	    System.out.println("is key entry=" + keyStore.isKeyEntry(keyAlias));
	    PrivateKey prikey = (PrivateKey) keyStore.getKey(keyAlias, nPassword);
	    Certificate cert = keyStore.getCertificate(keyAlias);
	    PublicKey pubkey = cert.getPublicKey();
	    System.out.println("cert class = " + cert.getClass().getName());
	    System.out.println("cert = " + cert);
	    System.out.println("public key = " + pubkey);
	    System.out.println("private key = " + prikey);

	} catch (Exception e)
	{
	    System.out.println(e);
	}
	return keyAlias;
    }

    /**
     * 获取公钥信息
     */
    public void getPublicKeyInfo()
    {
	String tmp0 = Conf_Info.Public_cer;
	String tmp1 = "";
	try
	{
	    tmp1 = getPublicKey(Conf_Info.PublicCerPath).replaceAll("\n", "");
	    System.out.println("商户公钥字符串:\n" + tmp1);
	    System.out.println("\n商户公钥字符给定串:\n" + tmp0);
	    if (tmp0.equals(tmp1))
		System.out.println("=========");
	    else
	    {
		System.out.println("**************");
	    }

	} catch (CertificateException e)
	{
	    e.printStackTrace();
	    System.out.println(e);
	} catch (IOException e)
	{
	    e.printStackTrace();
	    System.out.println(e);
	}
    }

    /**
     * 读取公钥cer
     * 
     * @param path
     *            .cer文件的路径 如:c:/abc.cer
     * @return base64后的公钥串
     * @throws IOException
     * @throws CertificateException
     */
    public static String getPublicKey(String path) throws IOException, CertificateException
    {
	InputStream inStream = new FileInputStream(path);
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	int ch;
	String res = "";
	while ((ch = inStream.read()) != -1)
	{
	    out.write(ch);
	}
	byte[] result = out.toByteArray();
	// res = Base64.byteArrayToBase64(result);
	res = Base64.encode(result);
	return res;
    }

}
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号