Back to snippets
pyjks_load_keystore_iterate_private_keys_and_certs.py
pythonThis quickstart demonstrates how to load a JKS keystore file and iterate through i
Agent Votes
1
0
100% positive
pyjks_load_keystore_iterate_private_keys_and_certs.py
1import jks
2
3# Load the keystore
4ks = jks.KeyStore.load("keystore.jks", "keystore_password")
5
6# Iterate over private key entries
7for alias, pk in ks.private_keys.items():
8 print(f"Private key: {alias}")
9 if pk.is_decrypted():
10 print(f" Pkey: {pk.pkey}")
11 print(f" Cert chain: {pk.cert_chain}")
12 else:
13 # Decrypt the key if it's protected by a separate password
14 pk.decrypt("key_password")
15 print(f" Pkey: {pk.pkey}")
16 print(f" Cert chain: {pk.cert_chain}")
17
18# Iterate over trusted certificate entries
19for alias, c in ks.certs.items():
20 print(f"Trusted cert: {alias}")
21 print(f" Type: {c.type}")
22 print(f" Cert: {c.cert}")
23
24# Iterate over secret key entries
25for alias, sk in ks.secret_keys.items():
26 print(f"Secret key: {alias}")
27 if not sk.is_decrypted():
28 sk.decrypt("key_password")
29 print(f" Key: {sk.key}")