如何使用java.security.KeyStore类存储和加载密钥 [英] How to store and load keys using java.security.KeyStore class

查看:190
本文介绍了如何使用java.security.KeyStore类存储和加载密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建密钥后,如何使用Keystore类的方法存储它们?如何加载密钥?

After creating secret keys, how do I store them using the Keystore class' methods and how do I load the keys?

推荐答案

存储:

KeyStore ks = KeyStore.getInstance("JKS");
ks.setKeyEntry("keyAlias", key, passwordForKeyCharArray, certChain);
OutputStream writeStream = new FileOutputStream(filePathToStore);
ks.store(writeStream, keystorePasswordCharArray);
writeStream.close();

注意,certChain可能为null,除非您传递 PrivateKey

Note thet certChain might be null, unless you are passing PrivateKey

正在加载:

KeyStore ks = KeyStore.getInstance("JKS");
InputStream readStream = new FileInputStream(filePathToStore);
ks.load(readStream, keystorePasswordCharArray);
Key key = ks.getKey("keyAlias", passwordForKeyCharArray);
readStream.close();

阅读 javadocs

编辑:

请注意,如果要存储SecretKey或使用SunJCE提供程序的任何部分(Java Cryptography Extension),则需要将KeyStore类型设置为JCEKS。

Note that if you are storing a SecretKey or using any part of the SunJCE provider (Java Cryptography Extension), you will need to set your KeyStore type to JCEKS.

KeyStore ks = KeyStore.getInstance("JCEKS");

我很感激,如果你解释我如何在我的ssl / tls应用程序中使用它(sslserversocketfactory)我需要给它一个CA证书的路径

I'd appreciate if you explain How can i use this with my ssl/tls application (sslserversocketfactory) i need to give it the path of a CA certificate

这篇关于如何使用java.security.KeyStore类存储和加载密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆