Java-cryptography-storing-keys

提供:Dev Guides
移動先:案内検索

Java暗号化-キーの保存

使用/生成されるキーと証明書は、キーストアと呼ばれるデータベースに保存されます。 デフォルトでは、このデータベースは .keystore という名前のファイルに保存されます。

*java.security* パッケージの *KeyStore* クラスを使用して、このデータベースのコンテンツにアクセスできます。 これは、PrivateKeyEntry、SecretKeyEntry、TrustedCertificateEntryの3つの異なるエントリを管理します。
  • 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オブジェクトと、キーストアのパスワードを指定するStringパラメーターを受け入れます。

通常、キーストアは、 C:/Program Files/Java/jre1.8.0_101/lib/security/ の場所にある cacerts という名前のファイルに保存され、デフォルトのパスワードは 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オブジェクトを作成する

サブクラス 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* クラスの* setEntry()*メソッドは、キーストアエントリエイリアス、 *SecretKeyEntry* オブジェクト、ProtectionParameterオブジェクトを表すStringパラメータを受け入れ、指定されたエイリアスの下にエントリを格納します。

以下に示す* setEntry()*メソッドを使用して、キーストアにエントリを設定します。

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

次の例では、キーを「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");
   }
}

出力

上記のプログラムは、次の出力を生成します-

System.out.println("data stored");