我用Java创建了一个pkcs12密钥库,其中我存储了两个密钥,现在我想使用python检索这些密钥。在Java中,加载密钥库并使用keystore.getkey(keyalias)。我怎么用python做到这一点?
# load OpenSSL.crypto
from OpenSSL import crypto
# open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open("/path/to/cert.p12", 'rb').read(), passwd)
# get various properties of said file.
# note these are PyOpenSSL objects, not strings although you
# can convert them to PEM-encoded strings.
p12.get_certificate() # (signed) certificate object
p12.get_privatekey() # private key.
p12.get_ca_certificates() # ca chain.
但是get_privatekey()不接受args,这意味着无法设置l想要以与java中相同的方式检索键别名:
public static SecretKey getEntry(KeyStore keyStore, String keyAlias, Optional<String> password) {
SecretKey key = null;
try {
key = (SecretKey) keyStore.getKey(keyAlias, password.orElse("").toCharArray());
} catch (Exception e) {
e.printStackTrace();
}
return key;
}
我开始之前的一个问题,您是用“秘密密钥”(又名对称密钥)开头的两个密钥,还是“私钥”?
JKS格式的密钥库不支持“秘密密钥”条目类型。由于pyjks不支持PKCS12,请尝试将密钥库从PKCS12格式创建/转换为JCEKS格式的密钥库。 JCEKS格式既支持“秘密密钥”条目类型,又被pyjks支持。
要从RHEL7上的命令行转换密钥库格式,请使用以下命令:
keytool -importkeystore -srckeystore <path_to_your_pkcs12_keystore> -srcstoretype PKCS12 -deststoretype JCEKS -destkeystore <path_to_new_jceks_keystore>
此新的JCEKS格式密钥库现在应该能够使用pyjks通过python读取