这属于测试性质的电子印章。正式环境应是政府颁发的电子印章。
制作电子印章主要包含以下几个步骤:
//第一步 创建电子印章证书
GMX509Builder gmx509Builder= null;
try {
gmx509Builder = getCertificate();
} catch (Exception e) {
e.printStackTrace();
}
X509Certificate certificate= gmx509Builder.getCertificate();
System.out.println(certificate.toString());
BCECPrivateKey bcecPrivateKey=(BCECPrivateKey) gmx509Builder.getPrivateKey() ;
//第二步 电子印章的图片
File file = new File("image/123.png");
byte[] picdata= Util.getFileBytes(file);
DEROctetString data= new DEROctetString (picdata);
DEROctetString cert= new DEROctetString (certificate.getEncoded());
//第三步 补充签章位置等其他属性
SESeal seSeal= SealBuilder.getInstance("00001",5,"easyOFD电子印章",bcecPrivateKey,
"png",data,40,30,null,cert
);
//第四步 保存电子印章
String filePath1 = "XML/seal/seal-out.esl";
SaveSealFile(filePath1,seSeal);
//第五步 保存秘钥
String filePath2 = "XML/seal/seal-key.esl";
SaveSealKeyFile(filePath2,bcecPrivateKey);
//第一步 获取电子印章证书
String sealPath = "XML/seal/seal-out.esl";
SESeal seSeal=Util.getSealFromFile(sealPath);
//第二步 获取私钥文件
String keyPath = "XML/seal/seal-key.esl";
BCECPrivateKey bcecPrivateKey=(BCECPrivateKey) getPrivateKey(keyPath) ;
//第三分 对签名范围内文件进行电子签章
String filePath1 = "XML/image/image.ofd";
String filePath2 = "XML/seal/image-seal.ofd";
CTBoundary ctBoundary=new CTBoundary(10,10,40,30);
OFDSealFile ofdSealFile=new OFDSealFile( new File(filePath1),seSeal);
try {
ofdSealFile.seal(bcecPrivateKey,ctBoundary);
try {
OutputStream os = null;
os = new FileOutputStream(filePath2);
ofdSealFile.saveOFD(os);
if(os!=null)
{
os.flush();
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (JAXBException e) {
e.printStackTrace();
}
验签是很简单的过程,只要把需要验签的文件传递给OFDVerifySignature类,调用verifySignature方法即可,只有该方法返回是true的情况才属于验签通过。
目前该版本程序验签只负责核对签名文件是否被修改进行验证,不会对证书的有效性,证书链,是否被吊销,是否过期等进行检核。
// 对OFD文件签章验证
public void VerFapiaoSeal() throws IOException, CryptoException, CertificateEncodingException, NoSuchProviderException, InvalidKeySpecException {
boolean sucessed=false;
try {
String filePath = "XML/seal/fapiao.ofd"; // ASN.1文件路径
Security.addProvider(new BouncyCastleProvider());
OFDVerifySignature ofdVerifySignature =new OFDVerifySignature(new File(filePath));
ofdVerifySignature.setSealCheck(this::sealCheck);
boolean verifySignature = ofdVerifySignature.verifySignature();
System.out.println("aaaaaa:"+verifySignature);
}catch (Exception ex) {
Assert.fail(ex.getMessage());
}
sucessed=true;
assertEquals(true,sucessed ) ;
}
//你自己的签章验证程序
@Override
public boolean sealCheck(SES_Signature sesSignature) {
System.out.println("your seal check");
return true;
}
不过你可以实现ISealCheck接口来定义你自己的检核程序。SES_Signature,包含了签名的值详细内容见 电子签章技术规范 GB/T 38540