我嘗試從KeyStore獲取密鑰。無法從KeyStore獲取密鑰
密鑰工具-genkeypair -dname 「CN =馬克·瓊斯,OU = JavaSoft的,O =太陽,C = US」 -alias業務2 -keypass abcdtest -keystore C:\工作區\ XMLSample \ 我通過使用Keytool創建一個密鑰密鑰庫\ mykeystore.jks -storepass 123456
而下面是GenerateXML.java
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import javax.xml.crypto.dsig.XMLSignContext;
import javax.xml.crypto.dsig.XMLSignatureFactory;
import javax.xml.crypto.dsig.dom.DOMSignContext;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class GenerateXML {
public static void main(String[] args) throws Exception {
try {
char[] passwd = "123456".toCharArray();
//Load the KeyStore and get the signing key and certificate
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("C:\\workspace\\XMLSample\\keystore\\mykeystore.jks"), passwd);
KeyStore.PrivateKeyEntry keyEnt = (KeyStore.PrivateKeyEntry)ks.getEntry("business2", new KeyStore.PasswordProtection(passwd)); // -> ERROR IN THIS ROW
X509Certificate cert = (X509Certificate)keyEnt.getCertificate();
//Create a DOMSignContext
XMLSignContext context = new DOMSignContext(keyEnt.getPrivateKey(), doc.getDocumentElement()) ;
//Create a DOM XMLSignatureFactory
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
} catch(Exception e) {
e.printStackTrace();
throw new Exception(e.toString());
}
}
}
我對Java 1.6的
運行,但有錯誤:
java.security.UnrecoverableKeyException: Cannot recover key
at sun.security.provider.KeyProtector.recover(KeyProtector.java:311)
at sun.security.provider.JavaKeyStore.engineGetKey(JavaKeyStore.java:121)
at sun.security.provider.JavaKeyStore$JKS.engineGetKey(JavaKeyStore.java:38)
at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:456)
at java.security.KeyStore.getEntry(KeyStore.java:1261)
at xml.generate.GenerateXML.main(GenerateXML.java:31)
我遇到同樣的問題。我正在給密鑰庫密碼,但它說:「可信的證書條目不受密碼保護」。如果我沒有給密碼說:「java.security.KeyStore $ TrustedCertificateEntry與java.security.KeyStore不兼容$ PrivateKeyEntry」 – 2011-05-17 09:57:37
嗨Coder, 我有一個問題在下面提到的鏈接。請給我一個解決方案。 http://stackoverflow.com/questions/33369965/trusted-certificate-entries-are-not-password-protected-java – praneeth 2015-10-28 12:55:16