2017-06-11 92 views
0

嗨,我希望你們都是okey 我使用的是opensaml-j v3,我很努力地用後者創建一個憑證,但我不知道如何創建它。如何使用opensaml-j創建證書?

這裏是我的源代碼

`private static Credential getCredential() throws IOException, 
      NoSuchAlgorithmException, InvalidKeySpecException, CertificateException{ 
     InputStream is=new FileInputStream("C:\\openssl keys\\key.pk8"); 
     CertificateFactory cf=CertificateFactory.getInstance("X.509"); 
     X509Certificate cer=(X509Certificate) cf.generateCertificate(is); 
     BasicX509Credential basicCredential=new BasicX509Credential(cer); 
     basicCredential.setUsageType(UsageType.SIGNING); 
     return basicCredential; 
}` 

這裏的堆棧跟蹤

Exception in thread "main" java.security.cert.CertificateParsingException: signed fields invalid 
 
\t at sun.security.x509.X509CertImpl.parse(Unknown Source) 
 
\t at sun.security.x509.X509CertImpl.<init>(Unknown Source) 
 
\t at sun.security.provider.X509Factory.engineGenerateCertificate(Unknown Source) 
 
\t at java.security.cert.CertificateFactory.generateCertificate(Unknown Source) 
 
\t at memory.CreateSamlAssertion.getCredential(CreateSamlAssertion.java:108) 
 
\t at memory.CreateSamlAssertion.createAssertion(CreateSamlAssertion.java:61) 
 
\t at memory.SamlTest.main(SamlTest.java:42)

的錯誤是在這一行:

X509Certificate cer=(X509Certificate) cf.generateCertificate(is);

所以我只是有一個簡單的問題:

有人可以告訴我一個源代碼如何創建一個憑證?

回答

0

的問題是在你的文件,你應該創建一個.csr文件文件 這裏有一個鏈接:create a .csr file 在創建之後跟隨此代碼段

private static String password="jkzhsmzku";//the password you set in the storepass 
private static String alias="business"//this is the alias; 

private static Credential getCredential(){ 
    char[] pass=password.toCharArray(); 
    KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType()); 
    FileInputStream fis=new FileInputStream("pathToFile/fileName.csr"); 
    ks.load(fis, pass); 
    KeyStore.PrivateKeyEntry pkEntry=null; 
    pkEntry=(PrivateKeyEntry) ks.getEntry(alias,new KeyStore.PasswordProtection(password.toCharArray())); 
    PrivateKey pk=pkEntry.getPrivateKey(); 
    X509Certificate certificate=(X509Certificate) pkEntry.getCertificate(); 
    BasicX509Credential basicCredential=new BasicX509Credential(certificate); 
    basicCredential.setPrivateKey(pk); 
    return basicCredential; 
}