如何为XML文档生成模数和指数?

时间:2017-10-29 07:37:10

标签: ruby xml ruby-on-rails-3 ruby-on-rails-4 xml-dsig

我想签署XML文档。我正在做以下生成签名(ruby)。

unsigned_xml = <<-xml
  <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
 </note>
 xml

sdoc = Xmldsig::SignedDocument.new(unsigned_xml)
signature_xml = File.read('signature.xml')
sdoc.document.children.children.last.add_next_sibling(signature_xml)
privkey = OpenSSL::PKey::RSA.new(File.read('bd-key.pem'))
sdoc.sign(privkey)

请参阅下面的signature.xml和输出

signature.xml中

<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/2001/04/xmlenc#sha256"/>
      <DigestValue/>
    </Reference>
  </SignedInfo>
  <SignatureValue/>
  <KeyInfo>
    <KeyValue>
      <RSAKeyValue>
        <Modulus></Modulus>
        <Exponent></Exponent>
      </RSAKeyValue>
    </KeyValue>
  </KeyInfo>
</Signature>

的Output.xml

<?xml version="1.0"?>
<note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
   <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/2001/04/xmlenc#sha256"/>
      <DigestValue>IssCQWd+dCUvTL9QuVgE/TzecC3wSbzQQ71CLrjpJGQ=</DigestValue>
    </Reference>
  </SignedInfo>
  <SignatureValue>COI61D+lQ1lLJ17wIBKr+O2kV4au97BMqM+EVPePw6g/itAq4UGBueGhANvYvElzyQcd12dTyh3QUhh/4rUorP6PXuO6eF6f9m13h3rRUupgeKaQbE65j1uvOGj1uXqMoNEuNHSUatATBkXJlfg3PCQfKyywHmW2GTtSKsvfj7WaQ7X9qnJMaCJXdOFS7eEFZ5C9KIutxIKRrH+YsaibwkVOfBYoVNVF08PjUfEpUMHCL6+z2WedRSwLxDPe0ByAN3eLsqGfVOLPSXvB7q3Y+sjE9cE5+vIxHlKhNzlYYayaY0B8Txa79b/g2Rl3fcajKHqVH+FD2lGFVdfktrksjg==</SignatureValue>
  <KeyInfo>
    <KeyValue>
      <RSAKeyValue>
        <Modulus/>
        <Exponent/>
      </RSAKeyValue>
    </KeyValue>
  </KeyInfo>
</Signature>
</note>

但是,当在XML有效负载上方发布时,第三方服务会返回 SIGNATURE MISMATCHING 。我认为问题是由于output.xml中缺少Modulus和Exponent。

我的问题是如何生成模数和指数?

0 个答案:

没有答案