Beispiel #1
0
        // We can provide a default implementation of FromXmlString because we require
        // every DSA implementation to implement ImportParameters
        // All we have to do here is parse the XML.

        public override void FromXmlString(String xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            Contract.EndContractBlock();
            DSAParameters   dsaParams  = new DSAParameters();
            Parser          p          = new Parser(xmlString);
            SecurityElement topElement = p.GetTopElement();

            // P is always present
            String pString = topElement.SearchForTextOfLocalName("P");

            if (pString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "P"));
            }
            dsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));

            // Q is always present
            String qString = topElement.SearchForTextOfLocalName("Q");

            if (qString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "Q"));
            }
            dsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));

            // G is always present
            String gString = topElement.SearchForTextOfLocalName("G");

            if (gString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "G"));
            }
            dsaParams.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(gString));

            // Y is always present
            String yString = topElement.SearchForTextOfLocalName("Y");

            if (yString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "Y"));
            }
            dsaParams.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(yString));

            // J is optional
            String jString = topElement.SearchForTextOfLocalName("J");

            if (jString != null)
            {
                dsaParams.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(jString));
            }

            // X is optional -- private key if present
            String xString = topElement.SearchForTextOfLocalName("X");

            if (xString != null)
            {
                dsaParams.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(xString));
            }

            // Seed and PgenCounter are optional as a unit -- both present or both absent
            String seedString        = topElement.SearchForTextOfLocalName("Seed");
            String pgenCounterString = topElement.SearchForTextOfLocalName("PgenCounter");

            if ((seedString != null) && (pgenCounterString != null))
            {
                dsaParams.Seed    = Convert.FromBase64String(Utils.DiscardWhiteSpaces(seedString));
                dsaParams.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(pgenCounterString)));
            }
            else if ((seedString != null) || (pgenCounterString != null))
            {
                if (seedString == null)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "Seed"));
                }
                else
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "DSA", "PgenCounter"));
                }
            }

            ImportParameters(dsaParams);
        }
        /// <summary>Reconstructs a <see cref="T:System.Security.Cryptography.DSA" /> object from an XML string.</summary>
        /// <param name="xmlString">The XML string to use to reconstruct the <see cref="T:System.Security.Cryptography.DSA" /> object. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="xmlString" /> parameter is <see langword="null" />. </exception>
        /// <exception cref="T:System.Security.Cryptography.CryptographicException">The format of the <paramref name="xmlString" /> parameter is not valid. </exception>
        // Token: 0x06002178 RID: 8568 RVA: 0x0007658C File Offset: 0x0007478C
        public override void FromXmlString(string xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            DSAParameters   parameters = default(DSAParameters);
            Parser          parser     = new Parser(xmlString);
            SecurityElement topElement = parser.GetTopElement();
            string          text       = topElement.SearchForTextOfLocalName("P");

            if (text == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
                {
                    "DSA",
                    "P"
                }));
            }
            parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text));
            string text2 = topElement.SearchForTextOfLocalName("Q");

            if (text2 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
                {
                    "DSA",
                    "Q"
                }));
            }
            parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text2));
            string text3 = topElement.SearchForTextOfLocalName("G");

            if (text3 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
                {
                    "DSA",
                    "G"
                }));
            }
            parameters.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text3));
            string text4 = topElement.SearchForTextOfLocalName("Y");

            if (text4 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
                {
                    "DSA",
                    "Y"
                }));
            }
            parameters.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text4));
            string text5 = topElement.SearchForTextOfLocalName("J");

            if (text5 != null)
            {
                parameters.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text5));
            }
            string text6 = topElement.SearchForTextOfLocalName("X");

            if (text6 != null)
            {
                parameters.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text6));
            }
            string text7 = topElement.SearchForTextOfLocalName("Seed");
            string text8 = topElement.SearchForTextOfLocalName("PgenCounter");

            if (text7 != null && text8 != null)
            {
                parameters.Seed    = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text7));
                parameters.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(text8)));
            }
            else if (text7 != null || text8 != null)
            {
                if (text7 == null)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
                    {
                        "DSA",
                        "Seed"
                    }));
                }
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", new object[]
                {
                    "DSA",
                    "PgenCounter"
                }));
            }
            this.ImportParameters(parameters);
        }
Beispiel #3
0
        /// <summary>通过 XML 字符串重新构造 <see cref="T:System.Security.Cryptography.DSA" /> 对象。</summary>
        /// <param name="xmlString">用于重新构造 <see cref="T:System.Security.Cryptography.DSA" /> 对象的 XML 字符串。</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="xmlString" /> 参数为 null。</exception>
        /// <exception cref="T:System.Security.Cryptography.CryptographicException">
        /// <paramref name="xmlString" /> 参数的格式无效。</exception>
        public override void FromXmlString(string xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            DSAParameters   parameters   = new DSAParameters();
            SecurityElement topElement   = new Parser(xmlString).GetTopElement();
            string          inputBuffer1 = topElement.SearchForTextOfLocalName("P");

            if (inputBuffer1 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object)"DSA", (object)"P"));
            }
            parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer1));
            string inputBuffer2 = topElement.SearchForTextOfLocalName("Q");

            if (inputBuffer2 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object)"DSA", (object)"Q"));
            }
            parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer2));
            string inputBuffer3 = topElement.SearchForTextOfLocalName("G");

            if (inputBuffer3 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object)"DSA", (object)"G"));
            }
            parameters.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer3));
            string inputBuffer4 = topElement.SearchForTextOfLocalName("Y");

            if (inputBuffer4 == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object)"DSA", (object)"Y"));
            }
            parameters.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer4));
            string inputBuffer5 = topElement.SearchForTextOfLocalName("J");

            if (inputBuffer5 != null)
            {
                parameters.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer5));
            }
            string inputBuffer6 = topElement.SearchForTextOfLocalName("X");

            if (inputBuffer6 != null)
            {
                parameters.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer6));
            }
            string inputBuffer7 = topElement.SearchForTextOfLocalName("Seed");
            string inputBuffer8 = topElement.SearchForTextOfLocalName("PgenCounter");

            if (inputBuffer7 != null && inputBuffer8 != null)
            {
                parameters.Seed    = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer7));
                parameters.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer8)));
            }
            else if (inputBuffer7 != null || inputBuffer8 != null)
            {
                if (inputBuffer7 == null)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object)"DSA", (object)"Seed"));
                }
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object)"DSA", (object)"PgenCounter"));
            }
            this.ImportParameters(parameters);
        }