// // DiffieHellman.cs: Defines a base class from which all Diffie-Hellman implementations inherit // // Author: // Pieter Philippaerts (Pieter@mentalis.org) // // (C) 2003 The Mentalis.org Team (http://www.mentalis.org/) // using System; using System.Text; using System.Security; using System.Security.Cryptography; using Mono.Xml; using Mono.Math; namespace Org.Mentalis.Security.Cryptography { /// /// Defines a base class from which all Diffie-Hellman implementations inherit. /// public abstract class DiffieHellman : AsymmetricAlgorithm { /// /// Creates an instance of the default implementation of the algorithm. /// /// A new instance of the default implementation of DiffieHellman. public static new DiffieHellman Create () { return Create ("Mono.Security.Cryptography.DiffieHellman"); } /// /// Creates an instance of the specified implementation of . /// /// The name of the implementation of DiffieHellman to use. /// A new instance of the specified implementation of DiffieHellman. public static new DiffieHellman Create (string algName) { return (DiffieHellman) CryptoConfig.CreateFromName (algName); } /// /// Initializes a new instance. /// public DiffieHellman() {} /// /// When overridden in a derived class, creates the key exchange data. /// /// The key exchange data to be sent to the intended recipient. public abstract byte[] CreateKeyExchange(); /// /// When overridden in a derived class, extracts secret information from the key exchange data. /// /// The key exchange data within which the secret information is hidden. /// The secret information derived from the key exchange data. public abstract byte[] DecryptKeyExchange(byte[] keyEx); /// /// When overridden in a derived class, exports the . /// /// true to include private parameters; otherwise, false. /// The parameters for Diffie-Hellman. public abstract DHParameters ExportParameters (bool includePrivate); /// /// When overridden in a derived class, imports the specified . /// /// The parameters for Diffie-Hellman. public abstract void ImportParameters (DHParameters parameters); private byte[] GetNamedParam(SecurityElement se, string param) { SecurityElement sep = se.SearchForChildByTag(param); if (sep == null) return null; return Convert.FromBase64String(sep.Text); } /// /// Reconstructs a object from an XML string. /// /// The XML string to use to reconstruct the DiffieHellman object. /// One of the values in the XML string is invalid. public override void FromXmlString (string xmlString) { if (xmlString == null) throw new ArgumentNullException (); DHParameters dhParams = new DHParameters(); try { SecurityParser 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); } } /// /// Creates and returns an XML string representation of the current object. /// /// true to include private parameters; otherwise, false. /// An XML string encoding of the current DiffieHellman object. public override string ToXmlString (bool includePrivateParameters) { StringBuilder sb = new StringBuilder (); DHParameters dhParams = ExportParameters(includePrivateParameters); try { sb.Append (""); sb.Append ("

"); sb.Append (Convert.ToBase64String (dhParams.P)); sb.Append ("

"); sb.Append (""); sb.Append (Convert.ToBase64String (dhParams.G)); sb.Append (""); if (includePrivateParameters) { sb.Append (""); sb.Append (Convert.ToBase64String (dhParams.X)); sb.Append (""); } sb.Append ("
"); } 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 (); } } }