我遇到电子签名问题。 windowsta有效
请帮助我
import org.apache.log4j.Logger;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.security.pkcs11.wrapper.PKCS11Exception;
import tr.gov.tubitak.uekae.esya.api.asn.x509.ECertificate;
import tr.gov.tubitak.uekae.esya.api.common.ESYAException;
import tr.gov.tubitak.uekae.esya.api.common.crypto.Algorithms;
import tr.gov.tubitak.uekae.esya.api.common.crypto.BaseSigner;
import tr.gov.tubitak.uekae.esya.api.common.util.LicenseUtil;
import tr.gov.tubitak.uekae.esya.api.common.util.bag.Pair;
import tr.gov.tubitak.uekae.esya.api.crypto.alg.SignatureAlg;
import tr.gov.tubitak.uekae.esya.api.smartcard.apdu.APDUSmartCard;
import tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.*;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.Context;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.SignatureMethod;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.XMLSignature;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.XMLSignatureException;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.config.Config;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.document.Document;
import tr.gov.tubitak.uekae.esya.api.xmlsignature.document.StreamingDocument;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@RestController
@EnableScheduling
@RequestMapping("/rest")
public class EImzaController {
private static final Logger logger = Logger.getLogger(EImzaController.class);
@RequestMapping(value = "/")
public String systemTest() {
StringBuilder sb = new StringBuilder();
sb.append("<h1>the system is running</h1>");
return sb.toString();
}
@RequestMapping(value = "/setImzaTest")
public String setImzaTest(String ping) throws ESYAException, PKCS11Exception, IOException, XMLSignatureException {
// file:///home/pardus/eimza/
String rootDir = System.getProperty("user.home");
logger.info("user.home : " + rootDir);
String seperator = File.separator;
rootDir += seperator + "eimza";
logger.info("app.dir : " + rootDir);
String lisansXml = rootDir + seperator + "lisans" + seperator + "lisans.xml";
logger.info("License Xml : " + lisansXml);
String configDir = rootDir + seperator + "config" + seperator;
String configFile = configDir + "xmlsignature-config.xml";
logger.info("Config File " + configFile);
String policyFile = configDir + "certval-policy-test.xml";
logger.info("Policy File " + policyFile);
String policyFileCrl = configDir + "certval-policy-test-crl.xml";
logger.info("Policy File Crl " + policyFileCrl);
String pfxPass = "313729";
// String pfxFile = rootDir + seperator + "sertifikadeposu" + seperator + "testuc@test.com.tr_313729.pfx";
// logger.info("Pfx File " + pfxFile);
// PfxSigner pfxSigner = new PfxSigner(SignatureAlg.RSA_SHA256, pfxFile, pfxPass.toCharArray());
Document xmlDoc = null;
String xmlData = "<envelope><data id=\"data1\"><item>ali</item></data></envelope>";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
xmlDoc = new StreamingDocument(new ByteArrayInputStream(xmlData.getBytes()), null, "text/xml", "sha256");
} catch (Exception e) {
logger.error("XML File Error! " + e.getMessage());
return e.getMessage();
}
try {
LicenseUtil.setLicenseXml(new FileInputStream(lisansXml));
} catch (Exception e) {
logger.error("Lisans XML bulunamadı! " + lisansXml);
return e.getMessage();
}
Date expirationDate = null;
try {
expirationDate = LicenseUtil.getExpirationDate();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
logger.info("License expiration date :" + dateFormat.format(expirationDate));
} catch (Exception e) {
logger.error("Lisans Expiration Date Error! " + e.getMessage());
return e.getMessage();
}
String base64Str = "";
String[] terminals = SmartOp.getCardTerminals();
try {
if (terminals == null || terminals.length == 0) {
logger.error("USB Cart not found! ");
return "USB Cart not found! ";
}
String terminal = terminals[0];
logger.info(" ********* terminal = " + terminal + " ********* ");
String pinKodu = "12345";
boolean apduSupport = APDUSmartCard.isSupported(terminal);
Pair<Long, CardType> slotAndCardType = SmartOp.getSlotAndCardType(terminal);
BaseSmartCard bsc = new P11SmartCard(slotAndCardType.getObject2());
bsc.openSession(slotAndCardType.getObject1());
// mSerialNumber = StringUtil.toString(bsc.getSerial());
// mSlotCount = terminals.length;
bsc.login(pinKodu);
List<byte[]> allCerts = bsc.getSignatureCertificates();
ECertificate cert = new ECertificate(allCerts.get(0));
Context context = new Context(rootDir);
context.setConfig(new Config(configFile));
XMLSignature signature = new XMLSignature(context);
signature.setSigningTime(Calendar.getInstance());
String id = signature.addDocument(xmlDoc);
signature.setId(id);
signature.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256);
signature.addKeyInfo(cert);
BaseSigner mSigner = bsc.getSigner(cert.asX509Certificate(), Algorithms.SIGNATURE_RSA_SHA256);
signature.sign(mSigner);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
signature.write(baos);
byte[] signedXml = baos.toByteArray();
base64Str = new String(Base64.encodeBase64(signedXml));
logger.info("signedXml : " + base64Str);
mSigner = null;
bsc.logout();
return base64Str;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}
// todo:e-imza库下载:https://yazilim.kamusm.gov.tr/?q=tr/node/14/surum-> https://yazilim.kamusm.gov.tr/?q=tr/system/files/public/MA3-JAVA-API-2.2.1.zip
我已经为那些遇到电子签名问题的人准备了该软件。
1-在此地址下载deb包,并使用gdebi或dpkg -i进行安装。 2-编写一个新的控制台(打开终端),然后键入“ sudo e-signature-setup olmadan不带引号”。 3-对Java的许可协议说“是”(没有其他要求您坐下来) 4-当再见安装位时,该过程完成。
将安装Java Oracle 8,akis和必需的驱动程序包。 图片和视频将在以后添加。