Example #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.

        /// <include file='doc\dsa.uex' path='docs/doc[@for="DSA.FromXmlString"]/*' />
        public override void FromXmlString(String xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException("xmlString");
            }
            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(String.Format(Environment.GetResourceString("Cryptography_InvalidFromXmlString"), "DSA", "P"));
            }
            dsaParams.P = Convert.FromBase64String(AsymmetricAlgorithm.DiscardWhiteSpaces(pString));

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

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

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

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

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

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

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

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

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

            if (xString != null)
            {
                dsaParams.X = Convert.FromBase64String(AsymmetricAlgorithm.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(AsymmetricAlgorithm.DiscardWhiteSpaces(seedString));
                dsaParams.Counter = ConvertByteArrayToInt(Convert.FromBase64String(AsymmetricAlgorithm.DiscardWhiteSpaces(pgenCounterString)));
            }
            else if ((seedString != null) || (pgenCounterString != null))
            {
                if (seedString == null)
                {
                    throw new CryptographicException(String.Format(Environment.GetResourceString("Cryptography_InvalidFromXmlString"), "DSA", "Seed"));
                }
                else
                {
                    throw new CryptographicException(String.Format(Environment.GetResourceString("Cryptography_InvalidFromXmlString"), "DSA", "PgenCounter"));
                }
            }

            ImportParameters(dsaParams);
        }
        /* 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");
            }
            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(String.Format(Environment.GetResourceString("Cryptography_InvalidFromXmlString"), "RSA", "Modulus"));
            }
            rsaParams.Modulus = Convert.FromBase64String(AsymmetricAlgorithm.DiscardWhiteSpaces(modulusString));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            ImportParameters(rsaParams);
        }