Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
Arquivo: RSA.cs Projeto: ForNeVeR/pnet
        // Reconstruct an asymmetric algorithm object from an XML string.
        public override void FromXmlString(String xmlString)
        {
            SecurityElement elem;
            RSAParameters   rsaParams = new RSAParameters();
            String          tag;

            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            try
            {
                elem = SecurityElement.Parse(xmlString);
                if (elem == null || elem.Tag != "RSAKeyValue")
                {
                    throw new CryptographicException
                              (_("Crypto_InvalidRSAParams"));
                }
                foreach (SecurityElement child in elem.Children)
                {
                    tag = child.Tag;
                    if (tag == "Modulus")
                    {
                        rsaParams.Modulus = Convert.FromBase64String
                                                (child.Text);
                    }
                    else if (tag == "Exponent")
                    {
                        rsaParams.Exponent = Convert.FromBase64String
                                                 (child.Text);
                    }
                    else if (tag == "D")
                    {
                        rsaParams.D = Convert.FromBase64String
                                          (child.Text);
                    }
                    else if (tag == "DP")
                    {
                        rsaParams.DP = Convert.FromBase64String
                                           (child.Text);
                    }
                    else if (tag == "DQ")
                    {
                        rsaParams.DQ = Convert.FromBase64String
                                           (child.Text);
                    }
                    else if (tag == "InverseQ")
                    {
                        rsaParams.InverseQ = Convert.FromBase64String
                                                 (child.Text);
                    }
                    else if (tag == "P")
                    {
                        rsaParams.P = Convert.FromBase64String
                                          (child.Text);
                    }
                    else if (tag == "Q")
                    {
                        rsaParams.Q = Convert.FromBase64String
                                          (child.Text);
                    }
                }
            }
            catch (FormatException)
            {
                throw new CryptographicException
                          (_("Crypto_InvalidRSAParams"));
            }
            catch (ArgumentNullException)
            {
                throw new CryptographicException
                          (_("Crypto_InvalidRSAParams"));
            }
            ImportParameters(rsaParams);
        }