Different types of keystore in Java -- JCEKS

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

JCEKS, or Java Cryptography Extension KeyStore, provides an alternative format for securely storing encryption keys and certificates within Java applications. Its operational flow for managing entries closely resembles JKS, primarily differing by requiring "JCEKS" when initializing a KeyStore instance. A key focus is on securely handling secret keys, which are sealed and protected by a password within the keystore. Developers can store a generated secret key using `keyStore.setKeyEntry()` and persist it, then later retrieve it with `keyStore.getKey()` for use in cryptographic operations. This approach helps prevent the exposure of sensitive key data.

JCEKS ย่อมาจาก Java Cryptography Extension KeyStore และเป็นรูปแบบ keystore อีกแบบหนึ่งสำหรับแพลตฟอร์ม Java การเก็บ keys ใน KeyStore อาจเป็นมาตรการเพื่อป้องกันไม่ให้ encryption keys ของคุณถูกเปิดเผย Java KeyStores เก็บ certificates และ keys แต่ละรายการอย่างปลอดภัย ซึ่งสามารถอ้างอิงได้โดย alias เพื่อใช้ในโปรแกรม Java

กระบวนการจัดเก็บและโหลด entries ต่างๆ ใน JCEKS คล้ายกับสิ่งที่ JKS ทำ ดังนั้นโปรดดูบทความ Different types of keystore in Java -- JKS เปลี่ยนชนิด keystore จาก JKS เป็น JCEKS ตามลำดับเมื่อเรียกใช้ KeyStore.getInstance()

ในโพสต์นี้ เราจะกล่าวถึงเฉพาะกระบวนการจัดเก็บ secret keys ใน keystore JCEKS เท่านั้น รายการ secret key จะถูกปิดผนึกและจัดเก็บไว้ใน keystore เพื่อปกป้องข้อมูล key โปรดระบุรหัสผ่านที่แข็งแกร่งเมื่อจัดเก็บรายการลงใน keystore

จัดเก็บ secret key

secret key สามารถจัดเก็บใน keystore JCEKS ด้วยโค้ดด้านล่าง

try{
	KeyStore keyStore = KeyStore.getInstance("JCEKS");
	keyStore.load(null, null);
	
	KeyGenerator keyGen = KeyGenerator.getInstance("DES");
	keyGen.init(56);;
	Key key = keyGen.generateKey();
	
	keyStore.setKeyEntry("secret", key, "password".toCharArray(), null);
	
	keyStore.store(new FileOutputStream("output.jceks"), "password".toCharArray());
} catch (Exception ex) {
	ex.printStackTrace();
}

โหลด secret key

secret key ที่จัดเก็บไว้สามารถดึงออกมาจาก keystore JCEKS ใน Java ได้ key ที่ดึงออกมาสามารถใช้ในการเข้ารหัส/ถอดรหัสข้อมูลได้ตามปกติ

try{
	KeyStore keyStore = KeyStore.getInstance("JCEKS");
	keyStore.load(new FileInputStream("output.jceks"), "password".toCharArray());
	
	Key key = keyStore.getKey("secret", "password".toCharArray());
	
	System.out.println(key.toString());
} catch (Exception ex) {
	ex.printStackTrace();
}

ผลลัพธ์คือ :

javax.crypto.spec.SecretKeySpec@fffe7b9b

สำหรับ keystores ชนิดต่างๆ โปรดดู Different types of keystore in Java -- Overview

JAVA TUTORIAL KEYSTORE JCEKS

  RELATED

  COMMENT

1
Anonymous
Mar 4, 2021 at 3:02 pm

why using "password" twice just after you said "please use strong password" :(