Keystore是Java中的資料庫;它允許我們以關鍵格式儲存資料; Keystore擴展自Java類java.security.KeyStore類,我們可以將keyStore寫入磁碟並從磁碟本身讀取;在Java 中使用密鑰庫的主要好處是它允許我們保護數據,因為它具有以密碼保護的形式儲存資料的功能。這些密碼就是他們的密碼,因為這種儲存功能最適合處理我們需要實作加密和解密機制的情況。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
如何在 Java 中建立金鑰庫?
在建立Keystore之前,我們需要了解它的類型;有一些可用的金鑰機制keyStore 是公鑰(此金鑰通常包含公開的關聯憑證)、私有和公開(用於任何非對稱類型的加密)。我們可以使用Java的getInstance()方法來建立Java中的Keystore。該方法是主類別中定義的內建方法,我們已經超出了該方法。以下是在 Java 中建立預設 KeyStore 類型的範例。我之所以說它是預設值,是因為我們沒有在函數中傳遞任何參數。如果我們想要建立任何自訂 keyStore,我們可以在方法中傳遞我們所需類型的 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 中載入 KeyStore?
這裡,如果我們談論金鑰庫的載入部分,載入是金鑰庫的一個重要機制。執行此操作時我們必須小心,因為它應該僅在需要時載入。載入很重要,因為如果不載入Keystore,我們就無法使用它。也可以使用任何空資料載入金鑰庫。一般來說,我們可以從任何文件或任何其他儲存裝置載入金鑰庫。有一個方法叫load();該方法將執行載入資料的任務。我們可以將兩個屬性傳遞給 load 方法;屬性是
- InputStream: 這是將從中讀取資料的輸入流;流可以來自任何檔案或其他來源。
- Char []: 這部分是為了安全;在這裡,我們可以傳遞一個字串作為KeyStore密碼進行加密。
讓我們來了解載入KeyStore.load(dataStream,password)的基本流程。這裡我們可以將 inputStream 作為資料流,並使用任意字串作為密碼。正如我們所提到的,我們也可以為 KyeStore 載入空數據,這可以透過為 keyStore 的 dataStream 傳遞任何 null 值來實現,例如 KeyStore.load(null, Keystore password)。一旦我們將 null 作為資料流傳遞,它將採用 null 值並為我們建立一個空的 Keystore。關於金鑰庫的一個重要的事情是,如果我們不載入金鑰庫,它將為我們呼叫的任何方法拋出異常。為了更好的編碼實踐,在開始使用 keyStore 之前加載它非常重要。
範例
在下面的範例中,我們載入具有空值的金鑰庫。
代碼:
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中儲存KeyStore?
我們有很多關於在 Java 中載入和建立 KeyStore 的討論;現在讓我們專注於儲存。這裡的儲存是指我們想要儲存以供將來使用的數據,因此儲存可以在任何地方進行,可以在資料庫上,也可以在磁碟上,具體取決於我們的選擇。我們有一個名為 store 的方法,它將起到將資料儲存在 KeyStore 中的作用。我們可以將兩個屬性傳遞給 KeyStore store() 方法。下面是一個簡單的語法範例。
文法
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"); } }
輸出:
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 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中文網其他相關文章!

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解決Java應用程序中的平台特定問題,可以採取以下步驟:1.使用Java的System類查看系統屬性以了解運行環境。 2.利用File類或java.nio.file包處理文件路徑。 3.根據操作系統條件加載本地庫。 4.使用VisualVM或JProfiler優化跨平台性能。 5.通過Docker容器化確保測試環境與生產環境一致。 6.利用GitHubActions在多個平台上進行自動化測試。這些方法有助於有效地解決Java應用程序中的平台特定問題。

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境