Java キーストア

王林
リリース: 2024-08-30 15:39:24
オリジナル
568 人が閲覧しました

キーストアは Java のデータベースです。データをキー形式で保存できるようになります。キーストアは Java クラス java.security.KeyStore クラスから拡張されており、ディスクに keyStore を書き込んだり、ディスク自体からそれを読み取ることができます。 Java でキーストアを使用する主な利点は、パスワードによる保護の形式でデータを保存する機能があるため、データを保護できることです。これらの種類の保存機能は、暗号化および復号化メカニズムを実装する必要がある場合の処理​​に最適であるため、これらのパスワードはそのパスワードです。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Java でキーストアを作成するには?

キーストアを作成する前に、そのタイプを理解する必要があります。利用可能な鍵メカニズムがいくつかあります。 keyStore には、公開鍵 (通常、この鍵には公開されている関連証明書が含まれます)、秘密鍵、および公開鍵 (非対称タイプの暗号化に使用されます) があります。 Java の getInstance() メソッドを使用して、Java でキーストアを作成できます。このメソッドはメイン クラスで定義された組み込みメソッドであり、これを超えています。以下は、Java でデフォルトの KeyStore タイプを作成する例です。関数内でパラメータを渡さないので、デフォルトとして言っています。カスタム キーストアを作成したい場合は、必要なタイプのキーストアのメソッドに引数を渡すことができます。

KeyStore customKeyStore = KeyStore.getInstance(custom-format);
ログイン後にコピー

ここでは、custom-format = PKCS12 を渡すことができます

注: PKCS12 (これは公開キーであり、暗号化の標準です) は、サーバーの証明書を保存する形式を定義します。また、秘密キーを単一の暗号化可能なファイルに保存することもできます。

以下は、メソッド getInstance() を使用してストアを作成する例です。

コード:

import java.io.FileInputStream;
import java.security.KeyStore;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class KeyCreate {
public static void main(String args[]) throws Exception {
//Creation of Keystore
KeyStore ks = KeyStore.getInstance("JCEKS");
System.out.println("Key Store Created");
}
}
ログイン後にコピー

出力:

Java キーストア

Java で KeyStore をロードするには?

キーストアのロードセクションについて言えば、ロードはキーストアにとって重要なメカニズムです。必要な場合にのみロードする必要があるため、この操作を実行するときは注意が必要です。 Keystore をロードしないと使用できないため、ロードすることは重要です。空のデータを使用してキーストアをロードすることもできます。一般に、キーストアは任意のファイルまたは他のストレージからロードできます。 load() というメソッドがあります。このメソッドはデータをロードするタスクを実行します。 2 つの属性をloadメソッドに渡すことができます。属性は

です
  • InputStream: これは、データが読み取られる入力ストリームです。ストリームは任意のファイルまたは他のソースから取得できます。
  • Char []: このセクションはセキュリティのためのものです。ここで、暗号化用の KeyStore パスワードの文字列を渡すことができます。

KeyStore.load(dataStream,password) をロードするための基本的なフローを理解しましょう。ここでは、dataStream として inputStream を使用し、パスワードとして任意の文字列を使用できます。前述したように、KyeStore の空のデータをロードすることもできます。これは、KeyStore.load(null, Keystore password) のように、キーストアの dataStream に任意の null 値を渡すことによって実行できます。データ ストリームとして null を渡すと、null 値を受け取り、空のキーストアが作成されます。キーストアに関する重要な点は、キーストアをロードしないと、呼び出すすべてのメソッドに対して例外がスローされるということです。コーディングの練習をより良くするためにも、使用を開始する前にキーストアをロードすることが非常に重要です。

以下の例では、null 値を含むキーストアをロードしています。

コード:

import java.io.FileInputStream;
import java.security.KeyStore;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class KeyLoad {
public static void main(String args[]) throws Exception {
//Creation of Keystore
KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(null);
System.out.println("Loading of an empty Keystore done");
}
}
ログイン後にコピー

出力:

Java キーストア

Java に KeyStore を保存するには?

Java でのキーストアのロードと作成については、多くの議論があります。ここでは保管に焦点を当ててみましょう。ここでの保存とは、将来の使用に備えて保存しておきたいデータを意味するため、選択に応じて、データベースまたはディスクのどこにでも保存を実行できます。 KeyStore にデータを保存する役割を果たすストアと呼ばれるメソッドがあります。KeyStore の store() メソッドに 2 つの属性を渡すことができます。以下はその簡単な構文例です。

構文

keyStore.store(streamOfOutputData ,password);
ログイン後にコピー

ここで、streamOfOutputData のストリームは任意のパスとファイルからデータを読み取ることができ、パスワードは保存されたデータを暗号化するための文字列パスワードです。保存したデータが将来必要になった場合は、再度ロードすることで、保存した場所からデータを取得できます。

コード:

import java.io.FileInputStream;
import java.security.KeyStore;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class KeyStoreExample {
public static void main(String args[]) throws Exception {
//Creation of Keystore
KeyStore ks = KeyStore.getInstance("JCEKS");
//Loading the KeyStore object
char[] ps = "ranjan".toCharArray();
String filePath = "./cacerts";
java.io.FileInputStream streamInput = new FileInputStream(filePath);
ks.load(null);
KeyStore.ProtectionParameter pp = new KeyStore.PasswordProtection(ps);
//Here we are creating an secret object
SecretKey ms = new SecretKeySpec("anypassword".getBytes(), "DSA");
//Creating SecretKeyEntry object
KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(ms);
ks.setEntry("secretKeyAlias", ske, pp);
//Storing the object
java.io.FileOutputStream storeData = null;
storeData = new java.io.FileOutputStream("nameOfNewKey");
ks.store(storeData, ps);
System.out.println("data stored");
}
}
ログイン後にコピー

出力:

Java キーストア

How to Get and Set Keys?

We can use two methods called getKey and setKey to get and set the keys. These methods are part of the KeyStore in Java. The method getEntry will allow us to get the value stored. Note that we have stored the value on the key with a password, so we need to pass the alias name of the key and the password used for storing the key. Similarly, we have a method called setKeyEntry.After getting the value from the keyStore again, we can access the data with various available methods like getPrivateKey(),getcertificate(),getPrivateKey(), and getCertificateChain(). These methods can be used to get the data according to our requirements from the KeyStore.

Example

In the example below, we check if the key is available from any KeyStore.

Code:

import java.security.*;
import java.security.cert.*;
import java.util.*;
import java.io.*;
public class SetAndGetKey {
public static void main(String[] argv)
{
try {
KeyStore store = KeyStore.getInstance("JCEKS");
store.load(null);
Boolean status
= store.isKeyEntry("test");
if (status)
System.out.println("The key available");
else
System.out.println("The key is not available");
}
catch (NoSuchAlgorithmException e) {
//catch code to handle exception
}
catch (NullPointerException e) {
//catch code to handle exception
}
catch (KeyStoreException e) {
//catch code to handle exception
}
catch (FileNotFoundException e) {
//catch code to handle exception
}
catch (IOException e) {
//catch code to handle exception
}
catch (CertificateException e) {
//catch code to handle exception
}
}
}
ログイン後にコピー

Output:

Java キーストア

Java KeyStore Methods

To perform various operations on the KeyStore, we have various methods below.

  • load(inputStream, password): It will load the keyStore data, and it needs two parameters, one as the input stream(file or any disk data) and password of string
  • store(outputStream, password): This method will be used for storing the data, and it will take two params one is output stream which from where data is going to read it could be file or disk and the second parameter is the password which will encrypt the data which we are storing.
  • getInstance(): This method is used to create a keyStore; if we pass nothing to this method, then it will create a default keyStore else, we can pass PKCS12 as the keyStore type.
  • getPrivateKey(): After getting keyStore, we can fetch private keys with this method.
  • getCertificate(): We can use this method to get the certificates.
  • getCertificateChain(): If we want to get a chain of certificates, we can use this method.

Conclusion

From this tutorial, we learned the basic concept of KeyStore in Java and about the creation, loading, and getting of various types of data from the Keystore data; we learned about the various available methods and their uses in Java for KeyStore.

以上がJava キーストアの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!