Skip to content

Instantly share code, notes, and snippets.

@stanleyxu2005
Last active July 10, 2025 09:44
Show Gist options
  • Save stanleyxu2005/df0f368d1f3a69971a92d8715cc96833 to your computer and use it in GitHub Desktop.
Save stanleyxu2005/df0f368d1f3a69971a92d8715cc96833 to your computer and use it in GitHub Desktop.
import jks
from cryptography.hazmat.primitives import serialization
# 加载 JKS 文件
keystore = jks.KeyStore.load('keystore.jks', 'password')
# 导出证书为 PEM
for alias, cert in keystore.certs.items():
pem_cert = cert.cert.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open(f"{alias}_cert.pem", "wb") as f:
f.write(pem_cert)
# 导出私钥为 PEM
for alias, private_key in keystore.private_keys.items():
pem_key = private_key.pkey.privateBytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
with open(f"{alias}_key.pem", "wb") as f:
f.write(pem_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment