2013-04-30 9 views
5

मैं लिफाफा हस्ताक्षर और javax.xml.crypto.dsig। * कक्षाओं का उपयोग कर एक एक्सएमएल फ़ाइल पर हस्ताक्षर करने की कोशिश कर रहा हूं। नतीजतन मुझे सही हस्ताक्षर सामग्री के साथ फ़ाइल मिलती है लेकिन बिना नामस्थान परिभाषित किया गया है। मैं xmlns कैसे जोड़ सकता हूं: डीएस = "http://www.w3.org/2000/09/xmldsig#" नेमस्पेस और संबंधित डीएस उपसर्ग? मुझे कोई भी जगह नहीं दिखाई दे रही है जहां मैं इसे परिभाषित कर सकता हूं।javax.xml.crypto.dsig का उपयोग कर XML फ़ाइल पर हस्ताक्षर करते समय नामस्थान कैसे जोड़ें। *?

उदाहरण कोड:

XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory.getInstance("DOM"); 

    (...) 

    XMLSignature signature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo); 

    // Marshal, generate, and sign the enveloped signature. 
    signature.sign(domSignContext); 

उदाहरण एक्सएमएल देता है:

<?xml version="1.0" encoding="UTF-8"?> 
<test xmlns="http://different.namespace.com"> 
    <someBody/> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
     <SignedInfo> 
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>  
      <Reference URI=""> 
       <Transforms> 
        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
       </Transforms> 
       <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
       <DigestValue>base64_digest</DigestValue> 
      </Reference> 
     </SignedInfo> 
     <SignatureValue>some_base64</SignatureValue> 
     <KeyInfo> 
      <X509Data> 
       <X509SubjectName>subject_data</X509SubjectName> 
       <X509Certificate>some_more_base64</X509Certificate> 
      </X509Data> 
      <KeyValue> 
       <RSAKeyValue> 
        <Modulus>another_base64</Modulus> 
        <Exponent>base64_as_well</Exponent> 
       </RSAKeyValue> 
      </KeyValue> 
     </KeyInfo> 
    </Signature> 
</test> 

लेकिन मैं चाहता हूँ:

<?xml version="1.0" encoding="UTF-8"?> 
<test xmlns="http://different.namespace.com" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
    <someBody/> 
    <ds:Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
     <ds:SignedInfo> 
      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> 
      <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>  
      <ds:Reference URI=""> 
       <ds:Transforms> 
        <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> 
       </ds:Transforms> 
       <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> 
       <ds:DigestValue>base64_digest</ds:DigestValue> 
      </ds:Reference> 
     </ds:SignedInfo> 
     <ds:SignatureValue>some_base64</ds:SignatureValue> 
     <ds:KeyInfo> 
      <ds:X509Data> 
       <ds:X509SubjectName>subject_data</ds:X509SubjectName> 
       <ds:X509Certificate>some_more_base64</ds:X509Certificate> 
      </ds:X509Data> 
      <ds:KeyValue> 
       <ds:RSAKeyValue> 
        <ds:Modulus>another_base64</ds:Modulus> 
        <ds:Exponent>base64_as_well</ds:Exponent> 
       </ds:RSAKeyValue> 
      </ds:KeyValue> 
     </ds:KeyInfo> 
    </ds:Signature> 
</test> 
+0

परिणामस्वरूप एक्सएमएल सही है। आप पर उपसर्ग क्यों चाहते हैं? हां, इसे "जाने-माने" उपसर्ग माना जाता है, लेकिन इससे परिणामी एक्सएमएल की शुद्धता प्रभावित नहीं होनी चाहिए। – user568826

उत्तर

3

नीचे छा हस्ताक्षर पैदा करने के लिए ओरेकल से नमूना कोड है। और मुझे लगता है कि आप जो खोज रहे हैं वह dsc.setDefaultNamespacePrefix ("dsig"); जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है।

XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); 

    Reference ref = fac.newReference 
    ("", fac.newDigestMethod(DigestMethod.SHA1, null), 
      Collections.singletonList 
      (fac.newTransform 
        (Transform.ENVELOPED, (TransformParameterSpec) null)), 
        null, null); 

    // Create the SignedInfo 
    SignedInfo si = fac.newSignedInfo 
    (fac.newCanonicalizationMethod 
      (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, 
        (C14NMethodParameterSpec) null), 
        fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), 
        Collections.singletonList(ref)); 

    // Create a DSA KeyPair 
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); 
    kpg.initialize(512); 
    KeyPair kp = kpg.generateKeyPair(); 

    // Create a KeyValue containing the DSA PublicKey that was generated 
    KeyInfoFactory kif = fac.getKeyInfoFactory(); 
    KeyValue kv = kif.newKeyValue(kp.getPublic()); 

    // Create a KeyInfo and add the KeyValue to it 
    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv)); 

    // Instantiate the document to be signed 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    dbf.setNamespaceAware(true); 
    Document doc = dbf.newDocumentBuilder().parse(new FileInputStream(sourceFile)); 

    // Create a DOMSignContext and specify the DSA PrivateKey and 
    // location of the resulting XMLSignature's parent element 
    DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement()); 
    dsc.setDefaultNamespacePrefix("dsig"); 

    // Create the XMLSignature (but don't sign it yet) 
    XMLSignature signature = fac.newXMLSignature(si, ki); 

    // Marshal, generate (and sign) the enveloped signature 
    signature.sign(dsc); 

    // output the resulting document 
    OutputStream os; 
    os = new FileOutputStream(DestinationFile); 

    TransformerFactory tf = TransformerFactory.newInstance(); 
    Transformer trans = tf.newTransformer(); 
    trans.transform(new DOMSource(doc), new StreamResult(os)); 
-1

स्ट्रिंग एल्गोरिटोमो = XMLSignature.ALGO_ID_SIGNATURE_RSA; XMLSignature sig = new XMLSignature(doc, algoritmo);

-1

आप अपने हस्ताक्षरित XML में प्रारूप

<ds:Signature ...> ... </ds:Signature> 

नीचे तो करना चाहते हैं कृपया जावा 6 संस्करण 31 का उपयोग करें और आप करेंगे आवश्यक रूप से हस्ताक्षरित XML।

संबंधित मुद्दे