Null error when using pdfsign function

A null error occurred when executing the pdfsign function with the pkcs#12 certificate. However, it succeeds when run with a different pkcs#12 certificate.
I have a certificate that works and one that doesn’t. What’s the difference?

Error log and sample logic are attached. Please confirm.
aspose pdfsign function sample code.docx (20.2 KB)

pdf error log.docx (27.7 KB)

@emrods

Would you kindly also share your sample PFX files with us (both working and non-working) along with sample PDF document so that we can test the scenario in our environment and address it accordingly. Also, please try to share the code snippet in text format here in the post editor because we were not able to check and view it in the DOCX file (may be the file is corrupted or encoding is different).

TEST-1_signed.pdf (1.5 MB)

It seems difficult to share PFX files. To explain the situation in more detail, there is a site that converts the certificate format. If you convert PFX to DER format and then change DER back to PFX, it will be successful.
So, an error occurs with the PFX certificate, and success occurs when the PFX is converted to a converted PFX through the conversion process, so I suspect that the new format according to the conversion site is not implemented in the aspose.pdf code.

-------- CODE --------
import com.aspose.pdf.Document;

import com.aspose.pdf.PKCS7;

import com.aspose.pdf.facades.PdfFileSignature;

import com.google.common.io.ByteStreams;

import org.apache.commons.io.IOUtils;

import org.junit.Test;

import smartsuite.exception.CommonException;

import java.awt.*;

import java.io.*;

public class AsposeTestAPI {

@Test

public void signPdf() throws Exception{

    String imageFilePath = "D:\\watermark_emro_s.png";

    File image = new File(imageFilePath);



    String certFilePath = "D:\\tradesign_samsung.pfx";

    String certPw = "s2cdlswmdtj2!";

    File certFile = new File(certFilePath);

    FileInputStream fis = new FileInputStream(certFile);

    byte[] certByte = IOUtils.toByteArray(fis);

    PKCS7 pkcs7 = new PKCS7(new ByteArrayInputStream(certByte), certPw);

    pkcs7.setShowProperties(false);



    String filePath = "C:\\upload\\9209b23a-346e-43d8-a792-092396ab92e1.PDF";

    Document doc = new Document(filePath);

    double height = doc.getPageInfo().getHeight() - 10;

    double width  = doc.getPageInfo().getWidth() - 10;

    OutputStream out = new ByteArrayOutputStream();

    doc.save(out);



    PdfFileSignature pdfSign = new PdfFileSignature();

    pdfSign.bindPdf(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));



    pdfSign.sign(1, true, new Rectangle((int)(width-70), (int)(height-70), 65, 65), pkcs7); // 수직시 사용

    try {

        pdfSign.setSignatureAppearanceStream(new FileInputStream(image));

    } catch (Exception e) {

        throw new CommonException(this.getClass().getName()+".setCertificate : "+ e.getMessage() + e.toString() );

    } finally {

        try {

            out.close();

        } catch (IOException e) {

            System.out.println("error:" + e.toString());

        }

    }



    String signFilePath = "C:\\upload\\sign.pdf";



    try{

        pdfSign.save(signFilePath);

    }catch(Exception e){

        System.out.println("error:" + e.toString());

        System.out.println("error:" + e.getMessage());

    }





}

}

@emrods

The PFX files are needed to investigate the issue accordingly. You can please share them in .zip format OR please let us know the steps on how to create some so that we can further proceed to assist you accordingly. Also, have you tried with 25.1 version of the API?

aspose pdf version : 19.2

I am attaching a PFX certificate for other tests. Two certificates are attached. This is a pfx certificate that fails and a pfx certificate that succeeds. The sample source was also rewritten.
certificate.zip (13.0 KB)

------- code -------
package test;

import com.aspose.pdf.Document;
import com.aspose.pdf.PKCS7;
import com.aspose.pdf.facades.PdfFileSignature;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import smartsuite.exception.CommonException;

import java.awt.;
import java.io.
;

public class AsposeTestAPI {

@Test
public void signPdf() throws Exception{

	String imageFilePath = "D:\\watermark_emro_s.png";

	File image = new File(imageFilePath);

	String certFilePath = "D:\\cert.pfx"; // success pfx
	//String certFilePath = "D:\\tradesign.pfx"; // fail pfx

	String certPw = "signgate1!";

	File certFile = new File(certFilePath);

	FileInputStream fis = new FileInputStream(certFile);

	byte[] certByte = IOUtils.toByteArray(fis);

	PKCS7 pkcs7 = new PKCS7(new ByteArrayInputStream(certByte), certPw);

	pkcs7.setShowProperties(false);

	String filePath = "D:\\test.pdf";

	Document doc = new Document(filePath);

	double height = doc.getPageInfo().getHeight() - 10;

	double width  = doc.getPageInfo().getWidth() - 10;

	OutputStream out = new ByteArrayOutputStream();

	doc.save(out);

	PdfFileSignature pdfSign = new PdfFileSignature();
	pdfSign.bindPdf(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));
	pdfSign.sign(1, true, new Rectangle((int)(width-70), (int)(height-70), 65, 65), pkcs7); // 수직시 사용
	try {
		pdfSign.setSignatureAppearanceStream(new FileInputStream(image));
	} catch (Exception e) {
		throw new CommonException(this.getClass().getName()+".setCertificate : "+ e.getMessage() + e.toString() );
	} finally {
		try {
			out.close();
		} catch (IOException e) {
			System.out.println("error:" + e.toString());
		}
	}
	String signFilePath = "D:\\sign.pdf";
	try{
		pdfSign.save(signFilePath);
	}catch(Exception e){
		System.out.println("error:" + e.toString());
		System.out.println("error:" + e.getMessage());
	}
}

}

@emrods

We tested using the latest version of the API and could not notice any issues. Please try using the latest version and if you still notice any issues, please let us know.

sign_tradesign.pdf (287.1 KB)

I attached two certificates, and did they both succeed? (cert.pfx, tradesign.pfx) I proceeded to the latest version 25.1, and the error occurs as shown below. Integrity check failed: java.security.NoSuchAlgorithmException: Algorithm HmacPBESHA256 not vailableerror:java.

@emrods

Yes, both were success and we also shared an output with you generated with tradesign.pfx certificate. Can you please try creating a sample console application and test only sign functionality in it with the minimal code to see if issue is happening?

@asad.ali
Please can you provide sample code that you have tested? What additional java versions have you tested?
I tested with oracle open jdk 11 version.

@emrods

We tested with JDK1.8 and 25.1 version of the API. Below is the code that we used and it is same as you shared with us.

String imageFilePath = dataDir + "aspose.png";
File image = new File(imageFilePath);

String certFilePath = dataDir + "tradesign.pfx"; // fail pfx
String certPw = "signgate1!";

File certFile = new File(certFilePath);
FileInputStream fis = new FileInputStream(certFile);
byte[] certByte = org.apache.commons.io.IOUtils.toByteArray(fis);

PKCS7 pkcs7 = new PKCS7(new ByteArrayInputStream(certByte), certPw);
pkcs7.setShowProperties(false);

String filePath = dataDir + "test.pdf";
Document doc = new Document(filePath);

double height = doc.getPageInfo().getHeight() - 10;
double width = doc.getPageInfo().getWidth() - 10;

OutputStream out = new ByteArrayOutputStream();
doc.save(out);

com.aspose.pdf.facades.PdfFileSignature pdfSign = new com.aspose.pdf.facades.PdfFileSignature();
pdfSign.bindPdf(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));
pdfSign.sign(1, true, new java.awt.Rectangle((int) (width - 70), (int) (height - 70), 65, 65), pkcs7);

try {
    pdfSign.setSignatureAppearanceStream(new FileInputStream(image));
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        out.close();
    } catch (IOException e) {
        System.out.println("error: " + e.toString());
    }
}

String signFilePath = dataDir + "sign.pdf";
try {
    pdfSign.save(signFilePath);
} catch (Exception e) {
    System.out.println("error: " + e.toString());
    System.out.println("error: " + e.getMessage());
}

@asad.ali
The error below occurred in jdk 1.8.0_332 version, but succeeded in jdk 11.0.17 version.

jdk 1.8.0_332 error
Integrity check failed: java.security.NoSuchAlgorithmException: Algorithm HmacPBESHA256 not availableerror: java.lang.RuntimeException: java.lang.NullPointerException
error: java.lang.NullPointerException

Thank you for your help.

@emrods

We found that:

HmacPBESHA256 is not included in JDK 8 (1.8.0_332)

  • Some cryptographic algorithms, like HmacPBESHA256, were introduced or better supported in later versions of Java (JDK 9+).
  • JDK 8 does not include HmacPBESHA256 by default, leading to NoSuchAlgorithmException.