[英]Convert java keystore to PFX through java programming
I need to support the same functionality of Keytool.exe to convert java keystore to PFX file through programming by using KeyTool class. 我需要支持Keytool.exe的相同功能,通过使用KeyTool类通过编程将java密钥库转换为PFX文件。 I cant use command prompt process from my application due to the project requirement limitation so through programming i cannot open command process either.
由于项目要求的限制,我无法从我的应用程序中使用命令提示进程,因此通过编程我也无法打开命令进程。
eg 例如
C:\\keytool -importkeystore -srckeystore .k eystore -srcstoretype JKS -destkeystore thekeystore.pfx -deststoretype PKCS12 C:\\ keytool -importkeystore -srckeystore .k eystore -srcstoretype JKS -destkeystore thekeystore.pfx -deststoretype PKCS12
I can create PFX file by keytool.exe by using above command but my requirement is to generate the PFX file by the keystore from my own application. 我可以使用上面的命令通过keytool.exe创建PFX文件,但我的要求是通过我自己的应用程序中的密钥库生成PFX文件。 I searched a lot on google and i could not find any helpful link which can give any reference or help regarding this issue.
我在谷歌搜索了很多,我找不到任何有用的链接,可以提供任何有关此问题的参考或帮助。 There is a class sun.security.tools.Keytool i searched this as well but i am not able to find any general programming help for this class.
有一个类sun.security.tools.Keytool我搜索了这个,但我无法找到任何一般编程帮助这个类。 Kindly if someone has any tip or idea then share it.
如果有人有任何提示或想法,请分享。
I don't know about the KeyTool class, and since it's not a public API I'd be averse to using it, but you can read and write keystores yourself using the KeyStore class. 我不知道KeyTool类,因为它不是公共API,我不愿意使用它,但你可以使用KeyStore类自己读写密钥库。 According to the documentation , Java supports, at a minimum, the
jks
and pkcs12
keystore types, so you can do something like: 根据文档 ,Java至少支持
jks
和pkcs12
密钥库类型,因此您可以执行以下操作:
public void convertKeystore(Path sourceKeystorePath,
char[] sourceKeystorePassword,
Path destKeystorePath,
char[] destKeystorePassword)
throws GeneralSecurityException, IOException {
KeyStore sourceKeystore = KeyStore.getInstance("jks");
try (InputStream stream =
new BufferedInputStream(
Files.newInputStream(sourceKeystorePath))) {
sourceKeystore.load(stream, sourceKeystorePassword);
}
KeyStore destKeystore = KeyStore.getInstance("pkcs12");
destKeystore.load(null, destKeystorePassword);
// Assume each alias in a keystore has the same password
// as the keystore itself.
KeyStore.ProtectionParameter sourceAliasPassword =
new KeyStore.PasswordProtection(sourceKeystorePassword);
KeyStore.ProtectionParameter destAliasPassword =
new KeyStore.PasswordProtection(destKeystorePassword);
Enumeration<String> aliasList = sourceKeystore.aliases();
while (aliasList.hasMoreElements()) {
String alias = aliasList.nextElement();
KeyStore.Entry entry =
sourceKeystore.getEntry(alias, sourceAliasPassword);
destKeystore.setEntry(alias, entry, destAliasPassword);
}
try (OutputStream stream =
new BufferedOutputStream(
Files.newOutputStream(destKeystorePath))) {
destKeystore.store(stream, destKeystorePassword);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.