Java Cryptography - Storing keys

本文详细介绍了如何使用Java安全包中的KeyStore类来管理密钥库,包括创建、加载密钥库对象,生成和存储秘钥,以及将秘钥条目设置到密钥库中。通过具体步骤和示例代码,帮助读者理解密钥库的使用方法。

The Keys and certificates used/generated are stored in a data base called as keystore. By default this database is stored in a file named .keystore.You can access the contents of this database using the KeyStore class of the java.security package. This manages three different entries namely, PrivateKeyEntry, SecretKeyEntry, TrustedCertificateEntry.PrivateKeyEntrySecretKeyEntryTrustedCertificateEntryStoring a Key in keystoreIn this section, we will learn how to store a key in a keystore. To store a key in the keystore, follow the steps given below.Step 1: Create a KeyStore objectThe getInstance() method of the KeyStore class of the java.security package accepts a string value representing the type of the keystore and returns a KeyStore object.Create an object of the KeyStore class using the getInstance() method as shown below.//Creating the KeyStore object
KeyStore keyStore = KeyStore.getInstance(“JCEKS”);
Step 2: Load the KeyStore objectThe load() method of the KeyStore class accepts a FileInputStream object representing the keystore file and a String parameter specifying the password of the KeyStore.In general, the KeyStore is stored in the file named cacerts, in the location C:/Program Files/Java/jre1.8.0_101/lib/security/ and its default password is changeit, load it using the load() method as shown below.//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);
Step 3: Create the KeyStore.ProtectionParameter objectInstantiate the KeyStore.ProtectionParameter as shown below.//Creating the KeyStore.ProtectionParameter object
KeyStore.ProtectionParameter protectionParam = new KeyStore.PasswordProtection(password);
Step 4: Create a SecretKey objectCreate the SecretKey (interface) object by instantiating its Sub class SecretKeySpec. While instantiating you need to pass password and algorithm as parameters to its constructor as shown below.//Creating SecretKey object
SecretKey mySecretKey = new SecretKeySpec(new String(keyPassword).getBytes(), “DSA”);
Step 5: Create a SecretKeyEntry objectCreate an object of the SecretKeyEntry class by passing the SecretKey object created in the above step as shown below.//Creating SecretKeyEntry object
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(mySecretKey);
Step 6: Set an entry to the KeyStoreThe setEntry() method of the KeyStore class accepts a String parameter representing the keystore entry alias, a SecretKeyEntry object, a ProtectionParameter object and, stores the entry under the given alias.Set the entry to the keystore using the setEntry() method as shown below.//Set the entry to the keystore
keyStore.setEntry(“secretKeyAlias”, secretKeyEntry, protectionParam);
ExampleThe following example stores keys into the keystore existing in the “cacerts” file (windows 10 operating system).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");

}
}OutputThe above program generates the following output −System.out.println(“data stored”);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值