2015-07-20 139 views
0

我有在CreateVisableSignature例如符號()方法的一個問題拋出一個空指針異常的org.apache.pdfbox.examples.signature.CreateVisableSignature拋出空異常

List<Certificate> certList = Arrays.asList(cert); 

我想我需要創建證書。有人能告訴我該怎麼做嗎?

這裏是有問題的代碼:

public byte[] sign(InputStream content) throws SignatureException, 
     IOException { 
    CMSProcessableInputStream input = new CMSProcessableInputStream(content); 
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); 
    // CertificateChain 
    List<Certificate> certList = Arrays.asList(cert); 
    CertStore certStore = null; 
    try { 
     certStore = CertStore.getInstance("Collection", 
       new CollectionCertStoreParameters(certList), provider); 
     gen.addSigner(privKey, (X509Certificate) certList.get(0), 
       CMSSignedGenerator.DIGEST_SHA256); 
     gen.addCertificatesAndCRLs(certStore); 
     CMSSignedData signedData = gen.generate(input, false, provider); 
     return signedData.getEncoded(); 
    } catch (Exception e) { 
     // should be handled 
     System.err.println("Error while creating pkcs7 signature."); 
     e.printStackTrace(); 
    } 
    throw new RuntimeException("Problem while preparing signature"); 
} 

這是設置它的代碼:

​​

這是我如何創建密鑰庫:

keytool -genkeypair -storepass password1 -storetype pkcs12 -alias hhscms -validity 365 -keyalg RSA -keystore keystore.p12 

爲什麼以下代碼無法獲得證書?

 privKey = (PrivateKey) keystore.getKey(alias, pin); 
     cert = keystore.getCertificateChain(alias); 

這是我收到的例外:

Exception in thread "main" java.lang.NullPointerException 
at java.util.Objects.requireNonNull(Unknown Source) 
at java.util.Arrays$ArrayList.<init>(Unknown Source) 
at java.util.Arrays.asList(Unknown Source) 
at org.apache.pdfbox.examples.signature.CreateVisibleSignature.sign(CreateVisibleSignature.java:191) 
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteSignature(COSWriter.java:767) 
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1162) 
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:578) 
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1517) 
at org.apache.pdfbox.pdmodel.PDDocument.saveIncremental(PDDocument.java:1398) 
at org.apache.pdfbox.examples.signature.CreateVisibleSignature.signPDF(CreateVisibleSignature.java:164) 
at org.apache.pdfbox.examples.signature.CreateVisibleSignature.main(CreateVisibleSignature.java:238) 

這是我的pom.xml的條目:

 <dependency> 
     <groupId>org.apache.pdfbox</groupId> 
     <artifactId>pdfbox</artifactId> 
     <version>1.8.9</version> 
    </dependency> 

    <dependency> 
     <groupId>org.bouncycastle</groupId> 
     <artifactId>bcprov-jdk15</artifactId> 
     <version>1.44</version> 
    </dependency> 
    <dependency> 
     <groupId>org.bouncycastle</groupId> 
     <artifactId>bcmail-jdk15</artifactId> 
     <version>1.44</version> 
    </dependency> 
    <dependency> 
     <groupId>org.bouncycastle</groupId> 
     <artifactId>bcprov-ext-jdk15on</artifactId> 
     <version>1.52</version> 
    </dependency> 
    <dependency> 
     <groupId>org.bouncycastle</groupId> 
     <artifactId>bcpkix-jdk15on</artifactId> 
     <version>1.52</version> 
    </dependency> 
+0

最後一個參數應該是-keystore keystore.p12。 – DenisMP

+0

我知道。我只是建議你編輯你的問題,它看起來更好:-) –

+0

這是怎麼回事? ;-) – DenisMP

回答

1

正如由蒂爾曼指出的那樣,你必須確保你正在運行正確的keytool。在我的情況下,keytool指向的是jre1.8而不是jdk1.8