PKCS12 KeyStore Store And Reload
Store a generated AES key in a PKCS12 KeyStore, write it to disk, reload it with the same password, and compare key material.
Code
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.util.Arrays;
public class KeyStoreDemo {
public static void main(String[] args) throws Exception {
char[] password = "changeit".toCharArray();
KeyStore store = KeyStore.getInstance("PKCS12");
store.load(null, password);
// 生成 AES-256 密钥并以别名存入
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(256);
SecretKey key = generator.generateKey();
store.setEntry("session-key",
new KeyStore.SecretKeyEntry(key),
new KeyStore.PasswordProtection(password));
Path path = Files.createTempFile("keystore-demo", ".p12");
try (OutputStream out = Files.newOutputStream(path)) {
store.store(out, password);
}
System.out.println("keystore bytes: " + Files.size(path));
// 重新加载并取出同一条目
KeyStore reloaded = KeyStore.getInstance("PKCS12");
try (InputStream in = Files.newInputStream(path)) {
reloaded.load(in, password);
}
KeyStore.Entry entry = reloaded.getEntry("session-key",
new KeyStore.PasswordProtection(password));
SecretKey restored = ((KeyStore.SecretKeyEntry) entry).getSecretKey();
System.out.println("alias present: " + reloaded.containsAlias("session-key"));
System.out.println("algorithm: " + restored.getAlgorithm());
System.out.println("same key material: "
+ Arrays.equals(key.getEncoded(), restored.getEncoded()));
Files.deleteIfExists(path);
}
}How it works
The working surface is the class KeyStoreDemo — everything else is wiring.
Resources live in the try header, so closing is compiler-generated and exception-safe.
Applying it
The demo output is labeled (keystore bytes:, alias present:, algorithm:), so every printed line maps back to its originating operation.
Statements that matter
SecretKey restored = ((KeyStore.SecretKeyEntry) entry).getSecretKey();
Path path = Files.createTempFile("keystore-demo", ".p12");
KeyGenerator generator = KeyGenerator.getInstance("AES");
Interactive Tools for This Topic
HMAC GeneratorCompute an HMAC-SHA-256, SHA-384, or SHA-512 signature for a message and key using the Web Crypto API, with hex and Base64 output.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.SHA-256 Hash GeneratorGenerate SHA-256 cryptographic hashes from text input using the Web Crypto API. Compare hashes and verify data integrity with hexadecimal digest output.JWT Signature InspectorInspect a JWT signature: decode the header and payload, show the algorithm and signature bytes, and verify HS256/384/512 HMAC signatures with a secret.
FAQ
- What does PKCS12 KeyStore Store And Reload print when run?
- Invoke the file as-is — the `keystore bytes:`, `alias present:`, `algorithm:` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.