1

我对加载 XML 的必需/可选要求有点困惑DSACryptoServiceProvider

这个网站,以下是架构:架构定义:

   <element name="DSAKeyValue" type="ds:DSAKeyValueType"/> 
   <complexType name="DSAKeyValueType"> 
     <sequence>
       <sequence minOccurs="0">
         <element name="P" type="ds:CryptoBinary"/> 
         <element name="Q" type="ds:CryptoBinary"/>
       </sequence>
       <element name="G" type="ds:CryptoBinary" minOccurs="0"/> 
       <element name="Y" type="ds:CryptoBinary"/> 
       <element name="J" type="ds:CryptoBinary" minOccurs="0"/>
       <sequence minOccurs="0">
         <element name="Seed" type="ds:CryptoBinary"/> 
         <element name="PgenCounter" type="ds:CryptoBinary"/> 
       </sequence>
     </sequence>
   </complexType>

但是,根据MSDN,DsaKeyValue XML 定义如下:

<DSAKeyValue>

  <!-- Child elements -->
  P,
  Q,
  G?,
  Y,
  J?,
  Seed,
  PgenCounter

</DSAKeyValue>

从 XML 导入时,我得到了错误的数据。以下是我的代码:

JsonDictionary privateKeyDictionary = (JsonDictionary) JsonReader.Parse(privateKey);
//FYI: this private signing key was generated using Keyczar.

var dsaParameterArray = privateKeyDictionary["publicKey"] as JsonDictionary;

    DSACryptoServiceProvider signer = new DSACryptoServiceProvider(1024);

    var pArray = Encoding.UTF8.GetBytes(dsaParameterArray["p"].ToString());
    string p = Convert.ToBase64String(pArray);

    var qArray = Encoding.UTF8.GetBytes(dsaParameterArray["q"].ToString());
    string q = Convert.ToBase64String(qArray);

    var gArray = Encoding.UTF8.GetBytes(dsaParameterArray["g"].ToString());
    string g = Convert.ToBase64String(gArray);

    var yArray = Encoding.UTF8.GetBytes(dsaParameterArray["y"].ToString());
    string y = Convert.ToBase64String(yArray);
    
    xml = String.Format("<DSAKeyValue><P>{0}</P><Q>{1}</Q><G>{2}</G><Y>{3}</Y><J/><Seed/><PgenCounter/></DSAKeyValue>", p, q, g, y);
    doc = new XmlDocument();
    doc.LoadXml(xml);
    signer.FromXmlString(doc.InnerXml);

综上所述,我的问题是我的密钥中没有“J”、“Seed”和“PgenCounter”,因此我没有提供它们。根据 W3C,这是可选的。但是,我的密钥中确实有一个“x”,但不确定那是什么。

根据 W3C,Seed 和 PgenCounter 标签是可选的。但是,MSDN 根据需要对其进行了定义。

我试过删除 J、Seed 和 PgenCounter 标签,但这似乎没有帮助。

谢谢!

4

1 回答 1

2

这是我为此示例生成的测试 Dsa 密钥的 Mono 示例输出:

<DSAKeyValue
    <P>jTBkDXQ38ZCYNzCK8gmweKsJ/mTqtDkf9TMNJnJycC4Hel2B/owSoOBGclAXhFLkvGqOC+zz8uMz7X2K31rvTpFxhTD7j6MDiVkjQEhFIacb6+TUn71x0KdN3nZo0CR8QgkkU10bkjHq2409S34pU6d8fZaMUFyOFZtSbkxFGV8=</P>
    <Q>6N/tEzeK0DS5nS/PBg944efSlPs=</Q>
    <G>OCsUHXdniP4HpLV537LsaetSI3DQRI8dyQUOdhpCFk00t+/Sadokn2rt0dYkL1KCGTc74Z9fXpCKA0ae3aqh63GvV6Uznjn8Mm75jeS9O03YyNmTMKTi1mcTFVipLanaWskqR9o3+DaTY6Puk7UkL9dDEsF+njAE412DCihej0Q=</G>
    <Y>gjzX/7ggD8ax1inJv+7GsDNcLWr7wvxLvBETO0BxrMYABP4cH4xF/w63dghNgXOSEFKZ8SFQZpdDhG6LLlysIzOW/KvEE/L2/xkT4tWZUAhxYkVbdjfphVkH4IVqrKbpUUsHcTj/7qc7FtEepWLlywIMNW3dTeeaP3DuKnMIn7w=</Y>
    <J>mzWoXBr+3jptO5/JOYh+bNckQgwuwHhCyqbGCJotoVpZzTts10rWEOdf0wQGtWTCXnpU/so1P8S6ZItvflYJxX77fZxv9iEOLcaNIUwBbgv2Jf5kRFx9xAz0yafHQTSyzZJW7bcc+mU2SaG6</J>
    <Seed>NfC0CEI+/bzAnuyGgUo2PHTLyw8=</Seed>
    <PgenCounter>OA==</PgenCounter>
    <X>pK0LlCD6PLynniOKtW/S+iyR3F8=</X>
</DSAKeyValue>

这包括私有参数。如果我只包含公共参数,则输出为:

<DSAKeyValue>
    <P>6run4SADV/kR/oEUmYXCTIqwWHG9yE+vMlnAbh4+I1YYTTCZ7Mf9BKZ6Z4WlSk+7WcnX+idT0HI5lOht+f5p32Z7aV86f89fLz4FTB+6r5B/cF8hhNe3mdGVpzzInwh2o16H9ejaB4chgOMASgeeBdNSzgVfj2RRWOZVX8wXWKU=</P>
    <Q>mfCvMD98ObtyBO1bJ6rIno2Wxws=</Q>
    <G>kgwCcpUWTyGaps8P/ePGj+CZUKSl2kxeJUSABYkgOa5I7OVIWrEgNl5fxCrs7qjXwJ+P+hQFe3HeHZWXONGgHpuakvDtp+G0wXuNUIF4vVwmZ0ff63TMFHGdUQXhsJPtK4ZK3LppSvWXACSzvZETH35A79nm4WRXlAIcHpxyeyg=</G>
    <Y>3fUEqW303wicxGJwsq1DoGKtvNUc3Eo33fpsdHMtZj+SyOH5ihvgKE7qd8yQxZH5qMI+5SFTokdwD8lh1jwmIq+Rrf2hi3RjZqNx+Lf3c6xRW3vDZSeTKwW/2xPiA+5z1rpO1/o2Bo5vV8i0ML3ik0VZasNVouo3C9KSX7Ya2cw=</Y>
    <J>AAAAAYZbcc5kA7nb5725pbY/qaTakYrxutdCZKEbtFu8KeXYFLCyw3FWlhmvMsStoAkleCnop0bBIoI3FLSEq5ZqUGcBfpNsV4qCs6tKcS7aZ8OFWpXncrYF1pwpaKpdzjDCaV69CEgkDCdQHw4gbA==</J>
    <Seed>n6ylLBasoS2Xqoc+QvBAXxQaFsc=</Seed>
    <PgenCounter>Dg==</PgenCounter>
</DSAKeyValue>

请注意缺少的“X”值,即私钥。

您不应该尝试手动解析这些。相反,请使用该DsaCryptoServiceProvider.FromXmlString方法。此外,除非我误读了输出,否则这W3C 标准兼容,尽管我在上面发表了评论。

编辑

这是我用来生成密钥的代码。请记住,这是在 Mac OS X 上使用 Mono,但我认为它在 Windows 上的 .NET 中也可以正常工作。

using System;
using System.Security.Cryptography;

namespace DsaParametersToy
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var dsa = new DSACryptoServiceProvider(1024);
            var dsaparams = dsa.ToXmlString(false);
            Console.WriteLine (dsaparams);
        }
    }
}
于 2012-12-30T23:52:07.783 回答