java keystore pkcs12_Java密钥库的不同类型 -- PKCS12

Different types of keystore in Java -- PKCS12

Java密钥库的不同类型 -- PKCS12

JKCS12 is an active file format for storing cryptography objects as a single file. It can be used to store secret key, private key and certificate.It is a standardized format published by RSA Laboratories which means it can be used not only in Java but also in other libraries in C, C++ or C# etc. This file format is frequently used to import and export entries from or to other keystore types.

JKCS12是一种活动文件格式,用于将加密对象存储为单个文件。它可以用来存储密钥、私钥和证书。它是RSA实验室发布的标准格式,它不仅可以用于java,而且可以用于C、C++或C等的其他库。这种文件格式经常用于从和向其他密钥存储类型导入和导出条目。

Next we will explain the operations which can be performed on PKCS12 keystore.

接下来我们将解释可以在PKCS12密钥库上执行的操作。

Create PKCS12 keystore

创建PKCS12密钥库

Before storing an entry into a PKCS12 keystore, the keystore has to be loaded first. This means we have to have a keystore created first. The simplest way of creating a PKCS12 keystore is :

在将条目存储到PKCS12密钥库之前,必须先加载密钥库。这意味着我们必须首先创建一个密钥库。创建PKCS12密钥库的最简单方法是:

try{

KeyStore keyStore= KeyStore.getInstance("PKCS12");

keyStore.load(null, null);

keyStore.store(new FileOutputStream("output.p12"), "password".toCharArray());

}catch(Exception ex){

ex.printStackTrace();

}

Note, when calling keyStore.load(null, null), two nulls are passed as the input keystore stream and password. This is because we don't have the keystore available yet. After running this program, there should be a keystore file named output.p12 in current working directory.

注意,当调用keyStore.load(null, null),传递两个null作为输入密钥库流和密码。这是因为我们还没有密钥库。运行此程序后,当前工作目录中应该有一个名为output.p12的密钥库文件。

Store secret key

存储密钥

PKCS12 allows to store secret keys on a limited base. Secret keys are frequently used to encrypt/decrypt data. To transfer the keys conveniently, they can be stored in a keystore like PKCS12 and transferred.

PKCS12允许在有限的基础上存储密钥。密钥经常用于加密/解密数据。为了方便地传输密钥,可以将它们存储在PKCS12这样的密钥库中并进行传输。

try{

KeyStore keyStore= KeyStore.getInstance("PKCS12");

keyStore.load(null, null);

KeyGenerator keyGen= KeyGenerator.getInstance("AES");

keyGen.init(128);

Ke

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
KeyStore.TrustedCertificateEntry是JavaKeyStore类的一个内部类,用于表示一个可信证书(trusted certificate)的条目。在Java中,可信证书通常指的是由可信证书颁发机构(CA)签发的证书,这些证书可以用于验证其他证书或者TLS连接。 使用KeyStore.TrustedCertificateEntry可以将可信证书添加到KeyStore对象中,以便在需要时进行验证。以下是一个示例代码: ``` KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); // 加载KeyStore FileInputStream fis = new FileInputStream("keystore.jks"); keyStore.load(fis, "password".toCharArray()); // 添加可信证书 CertificateFactory cf = CertificateFactory.getInstance("X.509"); FileInputStream certFile = new FileInputStream("trusted_cert.crt"); X509Certificate cert = (X509Certificate) cf.generateCertificate(certFile); KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(cert); keyStore.setEntry("alias", entry, null); // 保存KeyStore FileOutputStream fos = new FileOutputStream("keystore.jks"); keyStore.store(fos, "password".toCharArray()); ``` 在上述示例中,我们首先加载了一个KeyStore对象,然后使用CertificateFactory类读取一个X.509格式的证书文件,并将其转换为X509Certificate对象。接下来,我们使用KeyStore.TrustedCertificateEntry类创建了一个可信证书的条目,并将其添加到KeyStore对象中。最后,我们将更新后的KeyStore对象保存到文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值