title: Preparing SOAP request with WS-Security, RSA Encryption in node.js
body: <p>I am trying to create an http client from wsdl and send an encrypted request.
  However, I have tried most of encryption-Security packages in npm(soap, soap-x509-http, xml-crypto etc.) to achieve my goal but I couldn't send a correct request.</p>
  
  <p>You can see the requirements and a sample request below.</p>
  
  <p>Thanks in advance.</p>
  
  <ol>
  <li>Encryption should be performed using the following algorithms:<br/>
  a.  Key Encryption Algorithm: <strong>rsa-1_5</strong><br/>
  b.  Symmetric Encryption Algorithm: <strong>#aes128-cb</strong>c<br/>
  c.  Encription Canonicalization: <strong>xml-exc-c14n</strong><br/>
  d.  Token reference type: <strong>keyIdentifier (SKI)</strong><br/></li>
  <li>Signature should be performed using the following algorithms:<br/>
  a.  Signature Algorithm: <strong>rsa-sha1</strong><br/>
  b.  CanonicalizationMethod Algorithm: <strong>xml-exc-c14n</strong><br/></li>
  <li>SOAP message could contain Timestamp</li>
  </ol>
  
  <p>Sample Request(cipher values are shortened)
  </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;env:Header&gt;
      &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" env:mustUnderstand="1"&gt;
        &lt;wsu:Timestamp wsu:Id="timestamp"&gt;
          &lt;wsu:Created&gt;2017-01-25T10:53:55.572Z&lt;/wsu:Created&gt;
          &lt;wsu:Expires&gt;2017-01-25T10:58:55.572Z&lt;/wsu:Expires&gt;
        &lt;/wsu:Timestamp&gt;
        &lt;wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="token-47-1485341635575-1612376233"&gt;MIICXjCCAcegAwIBQ==&lt;/wsse:BinarySecurityToken&gt;
        &lt;xenc:EncryptedKey xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;
          &lt;xenc:EncryptionMethod xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/&gt;
          &lt;ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
            &lt;wsse:SecurityTokenReference wsu:Id="reference-50-1485341635576-76405248"&gt;
              &lt;wsse:KeyIdentifier EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary7f9" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier"&gt;MZBUluEDgIrFJrntxdBEI9Cr2A0=&lt;/wsse:KeyIdentifier&gt;
            &lt;/wsse:SecurityTokenReference&gt;
          &lt;/ds:KeyInfo&gt;
          &lt;xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;
            &lt;xenc:CipherValue xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;m5xNzosqhCIll4rMOH97zwKNprCC15w0LDta+NL81yIcPuLftJNZeI/RiPmJY7d3y2bdMV5Y=&lt;/xenc:CipherValue&gt;
          &lt;/xenc:CipherData&gt;
          &lt;xenc:ReferenceList xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;
            &lt;xenc:DataReference xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" URI="#encrypted-49-1485341635575-737511276"/&gt;
          &lt;/xenc:ReferenceList&gt;
        &lt;/xenc:EncryptedKey&gt;
        &lt;ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
          &lt;ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
            &lt;ds:CanonicalizationMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
            &lt;ds:SignatureMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
            &lt;ds:Reference xmlns:ds="http://www.w3.org/2000/09/xmldsig#" URI="#element-46-1485341635572-1313338421"&gt;
              &lt;ds:Transforms xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
                &lt;ds:Transform xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
              &lt;/ds:Transforms&gt;
              &lt;ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
              &lt;ds:DigestValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;koftV3puKxIgrkVHHxwSJYxsUhA=&lt;/ds:DigestValue&gt;
            &lt;/ds:Reference&gt;
            &lt;ds:Reference xmlns:ds="http://www.w3.org/2000/09/xmldsig#" URI="#timestamp"&gt;
              &lt;ds:Transforms xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
                &lt;ds:Transform xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
              &lt;/ds:Transforms&gt;
              &lt;ds:DigestMethod xmlns:ds="http://www.4dbw3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
              &lt;ds:DigestValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;Q0nZM95JuHF2nveehBU6wjUsoY4=&lt;/ds:DigestValue&gt;
            &lt;/ds:Reference&gt;
          &lt;/ds:SignedInfo&gt;
          &lt;ds:SignatureValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;BvlJMHJNrfI=&lt;/ds:SignatureValue&gt;
          &lt;ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
            &lt;wsse:SecurityTokenReference wsu:Id="reference-48-1485341635575-111206538"&gt;
              &lt;wsse:Reference URI="#token-47-1485341635575-1612376233" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/&gt;
            &lt;/wsse:SecurityTokenReference&gt;
          &lt;/ds:KeyInfo&gt;
        &lt;/ds:Signature&gt;
      &lt;/wsse:Security&gt;
    &lt;/env:Header&gt;
    &lt;env:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="element-46-1485341635572-1313338421"&gt;
      &lt;xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Id="encrypted-49-1485341635575-737511276" Type="http://www.w3.org/2001/04/xmlenc#Content"&gt;
        &lt;xenc:EncryptionMethod xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/&gt;
        &lt;xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;
          &lt;xenc:CipherValue xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;XoH4ROAG+6v0A28z1YeGeUfl4Im1q/jEJO+uDtAejU=13&lt;/xenc:CipherValue&gt;
        &lt;/xenc:CipherData&gt;
      &lt;/xenc:EncryptedData&gt;
    &lt;/env:Body&gt;
  &lt;/env:Envelope&gt;
  </code></pre>