keystore生成

1、生成keystore 

PS D:\Java\jdk1.8.0_131\bin> .\keytool.exe -genkeypair -alias dongbao-alias -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:\cer2\dongbao-alias.keystore -storepass 123456

public class ReadKeyStoreUtils {

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

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

	/**
	 * BASE64解密
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decryptBASE64(String key) throws Exception {

		return(new BASE64Decoder()).decodeBuffer(key);

	}

	/**
	 * BASE64加密
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptBASE64(byte[] key) throws Exception {

		return(new BASE64Encoder()).encodeBuffer(key).replace("\r","").replace("\n","");

	}

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

			throws Exception {

		FileInputStream is = new FileInputStream(keyStorePath);

		KeyStore ks = KeyStore.getInstance(KEY_STORE);

		ks.load(is, password.toCharArray());

		is.close();

		return ks;

	}

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

		KeyStore ks = getKeyStore(keyStorePath, storePass);

		PrivateKey key = (PrivateKey) ks.getKey(alias, keyPass.toCharArray());

		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 = ks.getCertificate(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 = encryptBASE64(key.getEncoded());

		return strKey;

	}

	/**
	 * 获取经BASE64编码后的私钥
	 *
	 * @param keyStorePath
	 * @param alias
	 * @param storePass
	 * @param keyPass
	 * @return
	 * @throws Exception
	 * @author 奔跑的蜗牛
	 */

	public static String getStrPrivateKey(String keyStorePath, String alias, String storePass, String keyPass) throws Exception {

		PrivateKey key = getPrivateKey(keyStorePath, alias, storePass, keyPass);

		String strKey = encryptBASE64(key.getEncoded());

		return strKey;

	}



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

		String keyStorePath = "D:\\cer2\\dongbao-alias.keystore";

		String strPublicKey = getStrPublicKey(keyStorePath, "dongbao-alias", "123456");
		System.out.println("公钥:"+strPublicKey);

		String strPrivateKey = getStrPrivateKey(keyStorePath, "dongbao-alias", "123456", "123456");
		System.out.println("私钥:"+strPrivateKey);

	}

}

2、查看keystore 

PS D:\Java\jdk1.8.0_131\bin> .\keytool.exe -list -v -keystore D:\cer2\dongbao-alias.keystore
输入密钥库口令: 

PS D:\Java\jdk1.8.0_131\bin> .\keytool.exe -list -rfc -keystore D:\cer2\dongbao-alias.keystore
输入密钥库口令: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值