Esempio n. 1
0
        /// <summary>通过 XML 字符串中的密钥信息初始化 <see cref="T:System.Security.Cryptography.RSA" /> 对象。</summary>
        /// <param name="xmlString">包含 <see cref="T:System.Security.Cryptography.RSA" /> 密钥信息的 XML 字符串。</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="xmlString" /> parameter is null. </exception>
        /// <exception cref="T:System.Security.Cryptography.CryptographicException">The format of the <paramref name="xmlString" /> parameter is not valid. </exception>
        public override void FromXmlString(string xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            RSAParameters   parameters   = new RSAParameters();
            SecurityElement topElement   = new Parser(xmlString).GetTopElement();
            string          inputBuffer1 = topElement.SearchForTextOfLocalName("Modulus");

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

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

            if (inputBuffer3 != null)
            {
                parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer3));
            }
            string inputBuffer4 = topElement.SearchForTextOfLocalName("Q");

            if (inputBuffer4 != null)
            {
                parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer4));
            }
            string inputBuffer5 = topElement.SearchForTextOfLocalName("DP");

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

            if (inputBuffer6 != null)
            {
                parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer6));
            }
            string inputBuffer7 = topElement.SearchForTextOfLocalName("InverseQ");

            if (inputBuffer7 != null)
            {
                parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer7));
            }
            string inputBuffer8 = topElement.SearchForTextOfLocalName("D");

            if (inputBuffer8 != null)
            {
                parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer8));
            }
            this.ImportParameters(parameters);
        }
Esempio n. 2
0
 internal static string DiscardWhiteSpaces(string inputBuffer)
 {
     return(Utils.DiscardWhiteSpaces(inputBuffer, 0, inputBuffer.Length));
 }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        // Import/export functions

        // We can provide a default implementation of FromXmlString because we require
        // every RSA 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();
            RSAParameters   rsaParams  = new RSAParameters();
            Parser          p          = new Parser(xmlString);
            SecurityElement topElement = p.GetTopElement();

            // Modulus is always present
            String modulusString = topElement.SearchForTextOfLocalName("Modulus");

            if (modulusString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "RSA", "Modulus"));
            }
            rsaParams.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(modulusString));

            // Exponent is always present
            String exponentString = topElement.SearchForTextOfLocalName("Exponent");

            if (exponentString == null)
            {
                throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", "RSA", "Exponent"));
            }
            rsaParams.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(exponentString));

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

            if (pString != null)
            {
                rsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
            }

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

            if (qString != null)
            {
                rsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
            }

            // DP is optional
            String dpString = topElement.SearchForTextOfLocalName("DP");

            if (dpString != null)
            {
                rsaParams.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dpString));
            }

            // DQ is optional
            String dqString = topElement.SearchForTextOfLocalName("DQ");

            if (dqString != null)
            {
                rsaParams.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dqString));
            }

            // InverseQ is optional
            String inverseQString = topElement.SearchForTextOfLocalName("InverseQ");

            if (inverseQString != null)
            {
                rsaParams.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inverseQString));
            }

            // D is optional
            String dString = topElement.SearchForTextOfLocalName("D");

            if (dString != null)
            {
                rsaParams.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dString));
            }

            ImportParameters(rsaParams);
        }
Esempio n. 5
0
        public static void FromXmlString(this RSA rsa, string xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException(nameof(xmlString));
            }

            Contract.EndContractBlock();
            var rsaParams  = new RSAParameters();
            var parser     = new Parser(xmlString);
            var topElement = parser.GetTopElement();

            // Modulus is always present
            var modulusString = topElement.SearchForTextOfLocalName("Modulus");

            if (modulusString == null)
            {
                throw new CryptographicException("Cryptography_InvalidFromXmlString");
            }

            rsaParams.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(modulusString));

            // Exponent is always present
            var exponentString = topElement.SearchForTextOfLocalName("Exponent");

            if (exponentString == null)
            {
                throw new CryptographicException("Cryptography_InvalidFromXmlString");
            }

            rsaParams.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(exponentString));

            // P is optional
            var pString = topElement.SearchForTextOfLocalName("P");

            if (pString != null)
            {
                rsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
            }

            // Q is optional
            var qString = topElement.SearchForTextOfLocalName("Q");

            if (qString != null)
            {
                rsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
            }

            // DP is optional
            var dpString = topElement.SearchForTextOfLocalName("DP");

            if (dpString != null)
            {
                rsaParams.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dpString));
            }

            // DQ is optional
            var dqString = topElement.SearchForTextOfLocalName("DQ");

            if (dqString != null)
            {
                rsaParams.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dqString));
            }

            // InverseQ is optional
            var inverseQString = topElement.SearchForTextOfLocalName("InverseQ");

            if (inverseQString != null)
            {
                rsaParams.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inverseQString));
            }

            // D is optional
            var dString = topElement.SearchForTextOfLocalName("D");

            if (dString != null)
            {
                rsaParams.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(dString));
            }

            rsa.ImportParameters(rsaParams);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        /// <summary>Initializes an <see cref="T:System.Security.Cryptography.RSA" /> object from the key information from an XML string.</summary>
        /// <param name="xmlString">The XML string containing <see cref="T:System.Security.Cryptography.RSA" /> key information. </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: 0x06002293 RID: 8851 RVA: 0x0007C3A4 File Offset: 0x0007A5A4
        public override void FromXmlString(string xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            RSAParameters   parameters = default(RSAParameters);
            Parser          parser     = new Parser(xmlString);
            SecurityElement topElement = parser.GetTopElement();
            string          text       = topElement.SearchForTextOfLocalName("Modulus");

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

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

            if (text3 != null)
            {
                parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text3));
            }
            string text4 = topElement.SearchForTextOfLocalName("Q");

            if (text4 != null)
            {
                parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text4));
            }
            string text5 = topElement.SearchForTextOfLocalName("DP");

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

            if (text6 != null)
            {
                parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text6));
            }
            string text7 = topElement.SearchForTextOfLocalName("InverseQ");

            if (text7 != null)
            {
                parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text7));
            }
            string text8 = topElement.SearchForTextOfLocalName("D");

            if (text8 != null)
            {
                parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(text8));
            }
            this.ImportParameters(parameters);
        }