Digitale XML-Signatur Java
brauche ich, um digital signieren meiner XML-Nachrichten in JAVA:
Die resultierende XML-Signatur muss das folgende format haben:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
<DigestValue>DsP5NLca+plhp9tZvGwykfb2whQYt3CQ5sbsVd9Q9aE=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
LrfE0po3YPvVxB/m77iBWWiR07Ghiuhuj7tO2C2LKqZK2cLrAiidt+3tjbJ3m16quCFxfh7bmjRtJsGi7a3HKtK
qY4auqrjNB62AtYrxvm+7Qd/cRacom4e3M9uF9JD1zTfoGun9w4WDfDrDaoZ+ZwUgNtf6sTYO5Ctcj5sYcD0=
</SignatureValue>
<KeyInfo>
<KeyName>7D665C81ABBE1A7D0E525BFC171F04D276F07BF2</KeyName>
</KeyInfo>
</Signature>
Kann jemand etwas Hilfe mit code ??
Edit:
kam ich mit diesem code :
private static Document sign(Document doc) throws InstantiationException, IllegalAccessException, ClassNotFoundException,
NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException,
FileNotFoundException, TransformerException {
String providerName = System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", (Provider) Class.forName(providerName).newInstance());
Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA256, null));
//Create the SignedInfo
SignedInfo si = fac.newSignedInfo(
fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null),
fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref));
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
KeyInfoFactory kif = fac.getKeyInfoFactory();
KeyValue kv = kif.newKeyValue(kp.getPublic());
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement());
XMLSignature signature = fac.newXMLSignature(si, ki);
signature.sign(dsc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(new FileOutputStream("mySignedFile")));
return doc;
}
aber das Problem ist, ich bin immer :
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
und nicht :
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hier ist die Lösung:
ich finde es auf diesem link
http://mail-archives.apache.org/mod_mbox/santuario-dev/200907.mbox/%[email protected]%3E
das problem war, RSA-SHA256-Algorithmus :
hier ist der resultierende code :
Du hast
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
, weil das, was Sie wollte:fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null)
.Als Sie herausgefunden und https://blogs.oracle.com/mullan/entry/using_stronger_xml_signature_algorithms Staaten
fac.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", (SignatureMethodParameterSpec) null)
erhalten Sie<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
.Diese sind standard-XML-Signaturen. Finden Sie die W3-Dokumentation auf "XML-Signature Syntax and Processing" für die Spezifikation, wie dies getan werden kann. Eine schnelle Suche auf google führen zu diesem howto, geschrieben von Oracle an der integration von XML-Signaturen in Java: XML-Digital-Signature-API. Wenn Sie weitere Probleme haben, bitte zeigt Sie uns, was Sie versucht haben.