使用Java从MacOS X密钥存储区获取私钥
问题描述:
我试图访问我的个人MacOS X钥匙串存储区以检索特定的私钥以使用Java对一些数据进行加密和签名。加密和签名部分是功能性的,但我无法检索我想要的私钥。下面是一些代码,我已经写信给目前我的问题:使用Java从MacOS X密钥存储区获取私钥
KeyStore myKeyStore;
myKeyStore = KeyStore.getInstance("KeychainStore", "Apple");
myKeyStore.load(null, null);
// Get all the aliases in a list (I thought that calling the KeyStore
// methods during the iteration was the reason why getKey wasn't responding properly!)
// ... it wasn't actually!
ArrayList<String> aliases = new ArrayList<String>();
Enumeration<String> e = myKeyStore.aliases();
while (e.hasMoreElements()) {
aliases.add(e.nextElement());
}
for (String alias : aliases) {
try {
// I read on the Internet that any ASCII password is required
// to get the getKey method working.
Key k = myKeyStore.getKey(alias, "TEST".toCharArray());
if (k == null) {
System.out.println(alias + ": <null> (cannot retrieve the key)");
} else {
System.out.println(alias + ":");
System.out.println(k);
}
} catch (Exception ex) {
System.out.println(alias + ": " + ex.getMessage());
}
}
执行这段代码后,我可以看到我的个人密钥库中的所有证书。不过,我只能检索一个私钥,即使密钥库中有一堆私钥。 (代码的输出只显示多个可信证书+仅一个私钥)
而且当我从密钥库中删除该私钥并再次执行该代码时,将返回另一个私钥,而其他所有其他私钥都不可访问。将私钥导回到密钥库中,并最后一次执行该代码,它将被Java返回,并且以前返回的最后一个私钥变得无法访问。
我偶然发现了一个关于该主题的邮件列表: http://lists.apple.com/archives/java-dev/2007/aug/msg00134.html。 不幸的是,它似乎并没有解决问题,我也无法联系相关人员(无电子邮件地址)。
有没有人试图从MacOS钥匙串存储中检索多个私钥,并成功了?
我的配置:
- 的MacOS X 10.6
- JVA JRE 1.6.0_15(32和64位)
- 的Safari 4.0.3
- 火狐3.6.3
由于提前,
答
不幸的是,这是一个已知的限制Apple KeyChain KeyStore实现的一部分。 OpenJDK MacOSX端口错误跟踪器(MACOSX_PORT-464)中有一个打开的票证,并且已经提交了一个修补程序。
然而,无论你需要从头重新编译OpenJDK(这需要一段时间,但它是一个简单的过程),或者你将不得不从OpenJDK源提取钥匙串加密服务提供程序,并建立一个新的jar包含独立的JCA提供程序但是由于它包含本地代码,它可能是一个更复杂的任务)。
您是否尝试过使用命令行中的keytool?如果它能工作 - 这可能是解决您的问题的方法。我过去使用过它,它对我很有用... – 2011-12-11 07:25:52