首页

PDF数字签名(ITEXT+keytool)

java

2020-6-27

用keytool生成数字证书,利用itext生成签名pdf,完整工程代码。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;

import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSignatureAppearance;
import com.itextpdf.text.pdf.PdfStamper;

public class Sign{
	
	// 证书密码
	private static final String cert_pwd = "ctid@888";
	//利用keytool生成数字证书
	//keytool -genkey -alias ctidcert -keystore c:/证书名称.keystore -storepass "888" -keypass "888" -keyalg "RSA" -dname "CN=www.123.cn,OU=简称,O=公司名称,L=北京,ST=北京,C=中国"
	private static final String cert_path = "c:/sign.keystore";
	//"D:\\dev\\eclipse-jee-juno-SR2-win32\\eclipse\\workspace\\sign\\src\\machunlin_1.pfx";
	
	public static void main(String[] args) {
		try {
			sign();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}  
		System.out.println("done!!!");
	}
	
	
	
	public static void sign() throws Exception{

		KeyStore ks = KeyStore.getInstance("jks");
		ks.load(new FileInputStream(cert_path), cert_pwd.toCharArray());
		String alias = (String)ks.aliases().nextElement();
		PrivateKey key = (PrivateKey)ks.getKey(alias, cert_pwd.toCharArray());
		Certificate[] chain = ks.getCertificateChain(alias);
		PdfReader reader = new PdfReader("C:\\foot.pdf");
		FileOutputStream fout = new FileOutputStream("c:\\signed.pdf");
		PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
		
		
		PdfSignatureAppearance sap = stp.getSignatureAppearance();
		sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
		sap.setReason("I'm the author");
		sap.setLocation("Ma");
		sap.setImage(Image.getInstance("c:/logo.png"));
		sap.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
		Rectangle r = new Rectangle(400, 650, 500,750);
		sap.setVisibleSignature(r, 1, null);
		stp.close();
	}
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
sign/.classpath , 484
sign/.project , 380
sign/.settings/org.eclipse.core.resources.prefs , 119
sign/lib/bcpkix-jdk15on-1.47.jar , 515071
sign/lib/bcprov-jdk15on-1.47.jar , 1997327
sign/lib/itextpdf-5.1.3.jar , 3817487
sign/src/Sign.java , 2036
没有账号? 忘记密码?

社交账号快速登录