IText国密电子签章,基于《GB/T 38540-2020 安全电子签章规范》
准备条件:
1.签名验签服务器 2.SM2证书 3.电子印章数据(需SM2证书绑定在印章数据里,完成制章) 4.签名验签服务器SDK(调用服务器进行签名)
GetPdfHashParamVo paramVo = new GetPdfHashParamVo();
Path pdf = Paths.get("src/main/resources", "test.pdf");
paramVo.setPdf(Files.readAllBytes(pdf));
paramVo.setPageNo(1);
paramVo.setLlx(240);
paramVo.setLly(290);
paramVo.setUrx(paramVo.getLlx() + 120);
paramVo.setUry(paramVo.getLly() + 120);
Path seal = Paths.get("src/main/resources", "深圳测试科技有限公司.seal");
paramVo.setSeal(SESeal.getInstance(Files.readAllBytes(seal)));
paramVo.setLocation("深圳");
paramVo.setReason("国密电子签章测试");
// 创建签名域,获取pdf文件摘要
GetPdfHash getPdfHash = ITextGM.getPdfHash(paramVo);
TSAClient tsaClient = new GMTSAClient(new URL("http://1.12.67.126:8082/tsa/sign?type=SM2"), null, null,
new SM3.Digest());
PrivateKey prvKey = PkiUtil.getPrivateKey(Base64.decodeBase64(privateKey));
X509Certificate signCert = PkiUtil.readX509Certificate(Base64.decodeBase64(cert));
// 签署摘要》》》下面方法仅用于测试,按照合规方面,需要使用国家认可的签名验签服务器以及国家认可的CA机构的SM2证书
SESV4Container signature = new SESV4Container(prvKey, paramVo.getSeal(), signCert);
GMTimeStampHook timeStampHook = new GMTimeStampHook(tsaClient);
signature.setTimeStampHook(timeStampHook);
byte[] p7 = signature.sign(getPdfHash.getDigesHash(), "Signature.xml");
// 签署pdf
byte[] signSuccess = ITextGM.signDeferred(getPdfHash.getEmptySignaturePdf(), p7, getPdfHash.getFieldName());
FileUtils.writeByteArrayToFile(new File("src/main/resources/sign.pdf"), signSuccess);
GetPdfHashParamVo paramVo = new GetPdfHashParamVo();
Path pdf = Paths.get("src/main/resources", "test.pdf");
paramVo.setPdf(Files.readAllBytes(pdf));
paramVo.setPageNo(1);
paramVo.setLlx(240);
paramVo.setLly(290);
paramVo.setUrx(paramVo.getLlx() + 120);
paramVo.setUry(paramVo.getLly() + 120);
Path seal = Paths.get("src/main/resources", "深圳测试科技有限公司.seal");
paramVo.setSeal(SESeal.getInstance(Files.readAllBytes(seal)));
paramVo.setLocation("深圳");
paramVo.setReason("国密电子签章测试");
// 创建签名域,获取pdf文件摘要,组装待签名数据
GetPdfHash getPdfHash = ITextGM.getPdfHash(paramVo);
TSAClient tsaClient = new GMTSAClient(new URL("http://1.12.67.126:8082/tsa/sign?type=SM2"), null, null,
new SM3.Digest());
PrivateKey prvKey = PkiUtil.getPrivateKey(Base64.decodeBase64(privateKey));
X509Certificate signCert = PkiUtil.readX509Certificate(Base64.decodeBase64(cert));
GMTimeStampHook timeStampHook = new GMTimeStampHook(tsaClient);
// 以下为模拟外部签名测试,电子签章请使用符合国家规范具有国家型号证书的设备进行
SESV4ContainerV2 signV2 = new SESV4ContainerV2(paramVo.getSeal(), signCert, timeStampHook);
/**
* 模拟签名服务器进行签名,实际使用过程,只需要使用签名服务器对getPdfHash.getTBS_Sign().getEncoded()
* 进行一个p1签名即可
*/
byte[] p1 = PkiUtil.sign(prvKey, "SM3WithSM2", getPdfHash.getTBS_Sign().getEncoded());
// 组装电子签章数据
byte[] p7 = signV2.sign(getPdfHash.getTBS_Sign(), p1);
// 签署pdf
byte[] signSuccess = ITextGM.signDeferred(getPdfHash.getEmptySignaturePdf(), p7, getPdfHash.getFieldName());
FileUtils.writeByteArrayToFile(new File("src/main/resources/signPdf2.pdf"), signSuccess);
GetPdfHashParamVo paramVo = new GetPdfHashParamVo();
Path pdf = Paths.get("src/main/resources", "test.pdf");
paramVo.setPdf(Files.readAllBytes(pdf));
paramVo.setPageNo(1);
paramVo.setLlx(240);
paramVo.setLly(290);
paramVo.setUrx(paramVo.getLlx() + 120);
paramVo.setUry(paramVo.getLly() + 120);
paramVo.setLocation("深圳南山区");
paramVo.setReason("分离式签名测试");
paramVo.setHashAlgorithm("SHA256");
Path sealImage = Paths.get("src/main/resources", "深圳测试科技有限公司_公章.png");
paramVo.setSignImage(Files.readAllBytes(sealImage));
// 创建签名域,获取pdf文件摘要
GetPdfHash getPdfHash = ITextSignHashUtil.getPdfHash(paramVo);
TSAClient tsaClient = new TSAClientBouncyCastle("http://1.12.67.126:8082/tsa/sign?type=RSA", null, null, 4096,
"SHA256");
PrivateKey prvKey = PkiUtil.getPrivateKey(Base64.decodeBase64(pkStr));
X509Certificate signCert = PkiUtil.readX509Certificate(Base64.decodeBase64(certStr));
// 签署p1,以下为模拟外部签名测试
byte[] p1 = PkiUtil.sign(prvKey, "SHA256WithRSA", getPdfHash.getSignHash());
// 签署p7
byte[] p7 = ITextSignHashUtil.signHash(getPdfHash.getDigesHash(), p1,
PkiUtil.getCertificateChain(signCert.getEncoded()), "SHA256", tsaClient);
// 签署pdf
byte[] signSuccess = ITextGM.signDeferred(getPdfHash.getEmptySignaturePdf(), p7, getPdfHash.getFieldName());
FileUtils.writeByteArrayToFile(new File("src/main/resources/sign_hash.pdf"), signSuccess);
原网址: 访问
创建于: 2024-06-11 17:09:09
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论