добавляется при подписании документа с сертификатом - PullRequest
0 голосов
/ 18 апреля 2020

Я использую следующий код для подписи документа, но я получаю 
 Пожалуйста, дайте мне знать, как удалить это.

//Fetching the CbCR XML File and storing it in String
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware ( true );  
Document doc = dbFactory.newDocumentBuilder().parse(new FileInputStream(filePath));

String providerName = System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");

XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", (Provider) Class.forName(providerName).newInstance());

// Next, create a Reference to a same-document URI that is an Object element and specify the SHA256 digest algorithm
DigestMethod digestMethod = fac.newDigestMethod(DigestMethod.SHA256, null);
Reference reference = fac.newReference("#CBC",digestMethod);
SignatureMethod signatureMethod = fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null);
CanonicalizationMethod canonicalizationMethod = fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null);

// Create the SignedInfo
SignedInfo si = fac.newSignedInfo(canonicalizationMethod, signatureMethod, Collections.singletonList(reference));

// Create a KeyValue containing the RSA PublicKey that was generated
KeyInfoFactory kif = fac.getKeyInfoFactory();

// Set the x509Content from the Petronas Certificate to generate signature
FileInputStream fin = new FileInputStream(CERTIFICATE_PATH);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
List x509Content = new ArrayList();
X509Certificate cert = (X509Certificate)cf.generateCertificate(fin);
X509IssuerSerial issuer = kif.newX509IssuerSerial(cert.getIssuerDN().toString(), cert.getSerialNumber());
x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(issuer);
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);

// Create a RSA 2048 KeyPair
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.generateKeyPair();
KeyValue kv = kif.newKeyValue(kp.getPublic());

XMLStructure content = new DOMStructure(doc.getDocumentElement());
XMLObject obj = fac.newXMLObject(Collections.singletonList(content), "CBC", null, null);

// Create a KeyInfo and add the KeyValues to it
List keyInfoItems = new ArrayList();
keyInfoItems.add(xd);
keyInfoItems.add(kv);
KeyInfo ki = kif.newKeyInfo(keyInfoItems);
Document signedDocument = dbFactory.newDocumentBuilder().newDocument();
DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), signedDocument);

// Create the XMLSignature and sign it
XMLSignature signature = fac.newXMLSignature(si, ki,Collections.singletonList(obj), null, null);
signature.sign(dsc);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();

//Storing the Signed XML File in the File system
os = new FileOutputStream(filePath);
trans.transform(new DOMSource(signedDocument), new StreamResult(os));
...