目录

Java密码术 - 存储密钥( Storing keys)

使用/生成的密钥和证书存储在称为密钥库的数据库中。 默认情况下,此数据库存储在名为.keystore的文件中。

您可以使用java.security包的KeyStore类访问此数据库的内容。 它管理三个不同的条目,即PrivateKeyEntry,SecretKeyEntry,TrustedCertificateEntry。

  • PrivateKeyEntry
  • SecretKeyEntry
  • TrustedCertificateEntry

在密钥库中存储密钥

在本节中,我们将学习如何在密钥库中存储密钥。 要在密钥库中存储密钥,请按照以下步骤操作。

第1步:创建KeyStore对象

java.security包的KeyStore类的getInstance()方法接受表示密钥库类型的字符串值,并返回KeyStore对象。

使用getInstance()方法创建KeyStore类的对象,如下所示。

//Creating the KeyStore object
KeyStore keyStore = KeyStore.getInstance("JCEKS");

第2步:加载KeyStore对象

KeyStore类的load()方法接受表示密钥库文件的FileInputStream对象和指定KeyStore密码的String参数。

通常,KeyStore存储在名为cacerts的文件中,位于C:/Program Files/Java/jre1.8.0_101/lib/security/ ,其默认密码为changeit ,使用load()方法加载它,如图所示下面。

//Loading the KeyStore object
char[] password = "changeit".toCharArray();
String path = "C:/Program Files/Java/jre1.8.0_101/lib/security/cacerts";
java.io.FileInputStream fis = new FileInputStream(path);
keyStore.load(fis, password);

第3步:创建KeyStore.ProtectionParameter对象

实例化KeyStore.ProtectionParameter,如下所示。

//Creating the KeyStore.ProtectionParameter object
KeyStore.ProtectionParameter protectionParam = new KeyStore.PasswordProtection(password);

第4步:创建一个SecretKey对象

通过实例化其Sub类SecretKeySpec创建SecretKey (接口)对象。 在实例化时,您需要将密码和算法作为参数传递给其构造函数,如下所示。

//Creating SecretKey object
SecretKey mySecretKey = new SecretKeySpec(new String(keyPassword).getBytes(), "DSA");

第5步:创建一个SecretKeyEntry对象

通过传递在上面步骤中创建的SecretKey对象来创建SecretKeyEntry类的对象,如下所示。

//Creating SecretKeyEntry object
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(mySecretKey);

步骤6:设置KeyStore的条目

KeyStore类的setEntry()方法接受表示密钥库条目别名的String参数, SecretKeyEntry对象,ProtectionParameter对象,并将条目存储在给定别名下。

使用setEntry()方法将条目设置为密钥库,如下所示。

//Set the entry to the keystore
keyStore.setEntry("secretKeyAlias", secretKeyEntry, protectionParam);

Example

以下示例将密钥存储到“cacerts”文件(Windows 10操作系统)中存在的密钥库中。

import java.io.FileInputStream;
import java.security.KeyStore;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class StoringIntoKeyStore{
   public static void main(String args[]) throws Exception {
      //Creating the KeyStore object
      KeyStore keyStore = KeyStore.getInstance("JCEKS");
      //Loading the KeyStore object
      char[] password = "changeit".toCharArray();
      String path = "C:/Program Files/Java/jre1.8.0_101/lib/security/cacerts";
      java.io.FileInputStream fis = new FileInputStream(path);
      keyStore.load(fis, password);
      //Creating the KeyStore.ProtectionParameter object
      KeyStore.ProtectionParameter protectionParam = new KeyStore.PasswordProtection(password);
      //Creating SecretKey object
      SecretKey mySecretKey = new SecretKeySpec("myPassword".getBytes(), "DSA");
      //Creating SecretKeyEntry object
      KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(mySecretKey);
      keyStore.setEntry("secretKeyAlias", secretKeyEntry, protectionParam);
      //Storing the KeyStore object
      java.io.FileOutputStream fos = null;
      fos = new java.io.FileOutputStream("newKeyStoreName");
      keyStore.store(fos, password);
      System.out.println("data stored");
   }
}

Output

上述程序生成以下输出 -

System.out.println("data stored");
↑回到顶部↑
WIKI教程 @2018