java 1.8 KeyStore
来源:互联网 发布:行业数据库 编辑:程序博客网 时间:2024/05/22 21:29
public class KeyStoreextends Object
A KeyStore
manages different types of entries. Each type of entry implements theKeyStore.Entry
interface. Three basic KeyStore.Entry
implementations are provided:
- KeyStore.PrivateKeyEntry
This type of entry holds a cryptographic
PrivateKey
, which is optionally stored in a protected format to prevent unauthorized access. It is also accompanied by a certificate chain for the corresponding public key.Private keys and certificate chains are used by a given entity for self-authentication. Applications for this authentication include software distribution organizations which sign JAR files as part of releasing and/or licensing software.
- KeyStore.SecretKeyEntry
This type of entry holds a cryptographic
SecretKey
, which is optionally stored in a protected format to prevent unauthorized access. - KeyStore.TrustedCertificateEntry
This type of entry contains a single public key
Certificate
belonging to another party. It is called atrusted certificate because the keystore owner trusts that the public key in the certificate indeed belongs to the identity identified by thesubject (owner) of the certificate.This type of entry can be used to authenticate other parties.
Each entry in a keystore is identified by an "alias" string. In the case of private keys and their associated certificate chains, these strings distinguish among the different ways in which the entity may authenticate itself. For example, the entity may authenticate itself using different certificate authorities, or using different public key algorithms.
Whether aliases are case sensitive is implementation dependent. In order to avoid problems, it is recommended not to use aliases in a KeyStore that only differ in case.
Whether keystores are persistent, and the mechanisms used by the keystore if it is persistent, are not specified here. This allows use of a variety of techniques for protecting sensitive (e.g., private or secret) keys. Smart cards or other integrated cryptographic engines (SafeKeyper) are one option, and simpler mechanisms such as files may also be used (in a variety of formats).
Typical ways to request a KeyStore object include relying on the default type and providing a specific keystore type.
- To rely on the default type:
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
The system will return a keystore implementation for the default type. - To provide a specific keystore type:
KeyStore ks = KeyStore.getInstance("JKS");
The system will return the most preferred implementation of the specified keystore type available in the environment.
Before a keystore can be accessed, it must be loaded
.
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); // get user password and file input stream char[] password = getPassword(); try (FileInputStream fis = new FileInputStream("keyStoreName")) { ks.load(fis, password); }To create an empty keystore using the above
load
method, pass null
as the InputStream
argument.Once the keystore has been loaded, it is possible to read existing entries from the keystore, or to write new entries into the keystore:
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(password); // get my private key KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry("privateKeyAlias", protParam); PrivateKey myPrivateKey = pkEntry.getPrivateKey(); // save my secret key javax.crypto.SecretKey mySecretKey; KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(mySecretKey); ks.setEntry("secretKeyAlias", skEntry, protParam); // store away the keystore try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) { ks.store(fos, password); }Note that although the same password may be used to load the keystore, to protect the private key entry, to protect the secret key entry, and to store the keystore (as is shown in the sample code above), different passwords or other protection parameters may also be used.
Every implementation of the Java platform is required to support the following standardKeyStore
type:
PKCS12
- java 1.8 KeyStore
- JAVA keystore
- Java Keystore
- JAVA keystore
- Java KeyStore
- java KeyStore 使用
- Java KeyStore的类型
- keystore相关 (java)
- java keytool keystore 常用命令
- Java - 生成keystore
- Java密钥库(KeyStore)
- 【Java】keystore转换pfx
- Java keytool and keystore tutorials
- Java支持的keystore格式
- 【JAVA】JDK KeyStore 如何使用
- Java Keytool 生成 keystore 命令
- (KeyStore)
- keystore
- windows 内核文档化API/导出但未文档化API/未导出API
- Mybatis解析动态sql原理分析
- 如何更好的使用Oracle全文索引
- ListView优化方法
- android studio中使用svn汇总
- java 1.8 KeyStore
- pod install vs. pod update (心得总结)
- 数据库编译约束
- document.write和 innerHTML的区别,待完善
- 自定义类做成员变量
- UIViewContentModeScaleAspectFill不凑效的解决办法
- mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
- mysql全文索引中文问题的解决
- Rabbitmq的helloworld