java KeyStore 使用

來源:互聯網
上載者:User

public class KeyStore

extends Object

此類表示密鑰和認證的儲存設施。

KeyStore 管理不同類型的條目。每種類型的條目都實現 KeyStore.Entry 介面。提供了三種基本的 KeyStore.Entry 實現: KeyStore.PrivateKeyEntry

此類型的條目儲存一個加密的 PrivateKey,可以選擇用受保護格式儲存該私密金鑰,以防止未授權訪問。它還隨附一個相應公開金鑰的憑證鏈結。

給定條目使用私密金鑰和憑證鏈結進行自驗證 (self-authentication)。應用此驗證的包括軟體發布組織,它們將 JAR 檔案簽名為發布和/或許可軟體的一部分。 KeyStore.SecretKeyEntry

此類型的條目儲存一個加密的 SecretKey,可以選擇用受保護格式儲存該密鑰,以防止未授權訪問。 KeyStore.TrustedCertificateEntry

此類型的條目包含一個屬於另一方的單個公開金鑰 Certificate。它被稱為可信認證,因為 keystore 的所有者相信認證中的公開金鑰確實屬於該認證的 subject(所有者)所標識的身份。

此類型的條目可用於驗證其他方。

KeyStore 中的每一條目都用 “alias” 字串標識。對於私密金鑰及其關聯的憑證鏈結,這些字串用於區分實體驗證自身可以採用的不同方式。例如,實體可以使用不同的認證授權或不同的公開金鑰演算法來驗證自身。

別名是否區分大小寫與實現有關。為了避免出現問題,建議不要在 KeyStore 中使用只有大小寫區別的別名。

在這裡沒有指定 keystore 是否是持久性的,也沒有指定 keystore 是持久性時所使用的機制。這允許使用各種技術保護敏感的(例如,私人的或秘密的)密鑰。一種選擇是使用智慧卡或其他整合加密引擎 (SafeKeyper),也可以(以各種格式)使用檔案之類更為簡單的機制。

請求 KeyStore 對象的典型方式包括使用預設類型和提供一個特定的 keystore 類型。 使用預設類型:

·                    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

 

系統將返回預設類型的 keystore 實現。 提供特定的 keystore 類型:

·                      KeyStore ks = KeyStore.getInstance("JKS");

 

系統將返迴環境中可用的指定 keystore 類型的首選實現。

必須先載入 keystore 才能對其進行訪問。

    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

 

    // get user password and file input stream

    char[] password = getPassword();

 

    java.io.FileInputStream fis = null;

    try {

        fis = new java.io.FileInputStream("keyStoreName");

        ks.load(fis, password);

    } finally {

        if (fis != null) {

            fis.close();

        }

    }

 

要使用上述 load 方法建立一個空 keystore,傳遞 null 作為 InputStream 的參數。

一旦載入了 keystore,就能夠從 keystore 讀取現有條目,或向 keystore 寫入新條目:

    // get my private key

    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)

        ks.getEntry("privateKeyAlias", password);

    PrivateKey myPrivateKey = pkEntry.getPrivateKey();

 

    // save my secret key

    javax.crypto.SecretKey mySecretKey;

    KeyStore.SecretKeyEntry skEntry =

        new KeyStore.SecretKeyEntry(mySecretKey);

    ks.setEntry("secretKeyAlias", skEntry,

        new KeyStore.PasswordProtection(password));

 

    // store away the keystore

    java.io.FileOutputStream fos = null;

    try {

        fos = new java.io.FileOutputStream("newKeyStoreName");

        ks.store(fos, password);

    } finally {

        if (fos != null) {

            fos.close();

        }

    }

 

注意,可以使用相同的密碼載入 keystore、保護私密金鑰條目、保護秘密密鑰條目以及儲存 keystore(如上文範例程式碼所示),也可以使用不同的密碼或其他保護參數。

 

例子

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.security.InvalidKeyException;

import java.security.KeyStore;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.NoSuchProviderException;

import java.security.PrivateKey;

import java.security.SignatureException;

import java.security.UnrecoverableKeyException;

import java.security.cert.Certificate;

import java.security.cert.CertificateException;

import java.security.cert.CertificateFactory;

 

public class KeyReader {

 

       public static void main(String[] args) throws KeyStoreException,

                     NoSuchProviderException, NoSuchAlgorithmException,

                     CertificateException, IOException, UnrecoverableKeyException,

                     InvalidKeyException, SignatureException {

 

              // Open an input stream on the keystore file

              String cerFileName = "d:/certA.cer";

              String p12FileName = "d:/certA.p12";

              String pfxPassword = "openssl";

 

              InputStream fis = new FileInputStream(p12FileName);

 

              // Create a keystore object

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

              // Load the file into the keystore

              keyStore.load(fis, pfxPassword.toCharArray());

 

              String aliaesName = "abcd";

              PrivateKey priKey = (PrivateKey) (keyStore.getKey(aliaesName, null));

              System.out.println("private key:/n" + priKey);

 

              // public key

              InputStream is = new FileInputStream(cerFileName);

              CertificateFactory cf = CertificateFactory.getInstance("x509");

              Certificate cerCert = cf.generateCertificate(is);

              System.out.println("public key:/n" + cerCert);

       }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.