Esempio n. 1
0
        /// <summary>
        ///     Reconstructs a <see cref="DiffieHellman" /> object from an XML string.
        /// </summary>
        /// <param name="xmlString">The XML string to use to reconstruct the DiffieHellman object.</param>
        /// <exception cref="CryptographicException">One of the values in the XML string is invalid.</exception>
        public override void FromXmlString(string xmlString)
        {
            if (xmlString == null)
                throw new ArgumentNullException();

            var dhParams = new DHParameters();
            try
            {
                var sp = new SecurityParser();
                sp.LoadXml(xmlString);
                SecurityElement se = sp.ToXml();
                if (se.Tag != "DHKeyValue")
                    throw new CryptographicException();
                dhParams.P = GetNamedParam(se, "P");
                dhParams.G = GetNamedParam(se, "G");
                dhParams.X = GetNamedParam(se, "X");
                ImportParameters(dhParams);
            }
            finally
            {
                if (dhParams.P != null)
                    Array.Clear(dhParams.P, 0, dhParams.P.Length);
                if (dhParams.G != null)
                    Array.Clear(dhParams.G, 0, dhParams.G.Length);
                if (dhParams.X != null)
                    Array.Clear(dhParams.X, 0, dhParams.X.Length);
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Imports the specified <see cref="DHParameters" />.
        /// </summary>
        /// <param name="parameters">The parameters for <see cref="DiffieHellman" />.</param>
        /// <exception cref="CryptographicException">
        ///     <paramref name="P" /> or <paramref name="G" /> is a null reference (
        ///     <b>Nothing</b> in Visual Basic) -or- <paramref name="P" /> is not a prime number.
        /// </exception>
        public override void ImportParameters(DHParameters parameters)
        {
            if (parameters.P == null)
            {
                throw new CryptographicException("Missing P value.");
            }
            if (parameters.G == null)
            {
                throw new CryptographicException("Missing G value.");
            }

            BigInteger p = new BigInteger(parameters.P), g = new BigInteger(parameters.G), x = null;

            if (parameters.X != null)
            {
                x = new BigInteger(parameters.X);
            }
            Initialize(p, g, x, 0, true);
        }
Esempio n. 3
0
        /// <summary>
        ///     Reconstructs a <see cref="DiffieHellman" /> object from an XML string.
        /// </summary>
        /// <param name="xmlString">The XML string to use to reconstruct the DiffieHellman object.</param>
        /// <exception cref="CryptographicException">One of the values in the XML string is invalid.</exception>
        public override void FromXmlString(string xmlString)
        {
            if (xmlString == null)
            {
                throw new ArgumentNullException();
            }

            var dhParams = new DHParameters();

            try
            {
                var sp = new SecurityParser();
                sp.LoadXml(xmlString);
                SecurityElement se = sp.ToXml();
                if (se.Tag != "DHKeyValue")
                {
                    throw new CryptographicException();
                }
                dhParams.P = GetNamedParam(se, "P");
                dhParams.G = GetNamedParam(se, "G");
                dhParams.X = GetNamedParam(se, "X");
                ImportParameters(dhParams);
            }
            finally
            {
                if (dhParams.P != null)
                {
                    Array.Clear(dhParams.P, 0, dhParams.P.Length);
                }
                if (dhParams.G != null)
                {
                    Array.Clear(dhParams.G, 0, dhParams.G.Length);
                }
                if (dhParams.X != null)
                {
                    Array.Clear(dhParams.X, 0, dhParams.X.Length);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     Creates and returns an XML string representation of the current <see cref="DiffieHellman" /> object.
        /// </summary>
        /// <param name="includePrivateParameters"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
        /// <returns>An XML string encoding of the current DiffieHellman object.</returns>
        public override string ToXmlString(bool includePrivateParameters)
        {
            var          sb       = new StringBuilder();
            DHParameters dhParams = ExportParameters(includePrivateParameters);

            try
            {
                sb.Append("<DHKeyValue>");

                sb.Append("<P>");
                sb.Append(Convert.ToBase64String(dhParams.P));
                sb.Append("</P>");

                sb.Append("<G>");
                sb.Append(Convert.ToBase64String(dhParams.G));
                sb.Append("</G>");

                if (includePrivateParameters)
                {
                    sb.Append("<X>");
                    sb.Append(Convert.ToBase64String(dhParams.X));
                    sb.Append("</X>");
                }

                sb.Append("</DHKeyValue>");
            }
            finally
            {
                Array.Clear(dhParams.P, 0, dhParams.P.Length);
                Array.Clear(dhParams.G, 0, dhParams.G.Length);
                if (dhParams.X != null)
                {
                    Array.Clear(dhParams.X, 0, dhParams.X.Length);
                }
            }
            return(sb.ToString());
        }
Esempio n. 5
0
 /// <summary>
 ///     When overridden in a derived class, imports the specified <see cref="DHParameters" />.
 /// </summary>
 /// <param name="parameters">The parameters for Diffie-Hellman.</param>
 public abstract void ImportParameters(DHParameters parameters);
Esempio n. 6
0
 /// <summary>
 ///     When overridden in a derived class, imports the specified <see cref="DHParameters" />.
 /// </summary>
 /// <param name="parameters">The parameters for Diffie-Hellman.</param>
 public abstract void ImportParameters(DHParameters parameters);
Esempio n. 7
0
        /// <summary>
        ///     Imports the specified <see cref="DHParameters" />.
        /// </summary>
        /// <param name="parameters">The parameters for <see cref="DiffieHellman" />.</param>
        /// <exception cref="CryptographicException">
        ///     <paramref name="P" /> or <paramref name="G" /> is a null reference (
        ///     <b>Nothing</b> in Visual Basic) -or- <paramref name="P" /> is not a prime number.
        /// </exception>
        public override void ImportParameters(DHParameters parameters)
        {
            if (parameters.P == null)
                throw new CryptographicException("Missing P value.");
            if (parameters.G == null)
                throw new CryptographicException("Missing G value.");

            BigInteger p = new BigInteger(parameters.P), g = new BigInteger(parameters.G), x = null;
            if (parameters.X != null)
            {
                x = new BigInteger(parameters.X);
            }
            Initialize(p, g, x, 0, true);
        }
Esempio n. 8
0
 /// <summary>
 ///     Exports the <see cref="DHParameters" />.
 /// </summary>
 /// <param name="includePrivateParameters"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
 /// <returns>The parameters for <see cref="DiffieHellman" />.</returns>
 public override DHParameters ExportParameters(bool includePrivateParameters)
 {
     var ret = new DHParameters();
     ret.P = m_P.GetBytes();
     ret.G = m_G.GetBytes();
     if (includePrivateParameters)
     {
         ret.X = m_X.GetBytes();
     }
     return ret;
 }