2 // DiffieHellman.cs: Defines a base class from which all Diffie-Hellman implementations inherit
5 // Pieter Philippaerts (Pieter@mentalis.org)
7 // (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
12 using System.Security;
13 using System.Security.Cryptography;
17 namespace Mono.Security.Cryptography {
19 /// Defines a base class from which all Diffie-Hellman implementations inherit.
21 public abstract class DiffieHellman : AsymmetricAlgorithm {
23 /// Creates an instance of the default implementation of the <see cref="DiffieHellman"/> algorithm.
25 /// <returns>A new instance of the default implementation of DiffieHellman.</returns>
26 public static new DiffieHellman Create () {
27 return Create ("Mono.Security.Cryptography.DiffieHellman");
30 /// Creates an instance of the specified implementation of <see cref="DiffieHellman"/>.
32 /// <param name="algName">The name of the implementation of DiffieHellman to use.</param>
33 /// <returns>A new instance of the specified implementation of DiffieHellman.</returns>
34 public static new DiffieHellman Create (string algName) {
35 return (DiffieHellman) CryptoConfig.CreateFromName (algName);
39 /// Initializes a new <see cref="DiffieHellman"/> instance.
41 public DiffieHellman() {}
44 /// When overridden in a derived class, creates the key exchange data.
46 /// <returns>The key exchange data to be sent to the intended recipient.</returns>
47 public abstract byte[] CreateKeyExchange();
49 /// When overridden in a derived class, extracts secret information from the key exchange data.
51 /// <param name="keyEx">The key exchange data within which the secret information is hidden.</param>
52 /// <returns>The secret information derived from the key exchange data.</returns>
53 public abstract byte[] DecryptKeyExchange(byte[] keyEx);
56 /// When overridden in a derived class, exports the <see cref="DHParameters"/>.
58 /// <param name="includePrivate"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
59 /// <returns>The parameters for Diffie-Hellman.</returns>
60 public abstract DHParameters ExportParameters (bool includePrivate);
62 /// When overridden in a derived class, imports the specified <see cref="DHParameters"/>.
64 /// <param name="parameters">The parameters for Diffie-Hellman.</param>
65 public abstract void ImportParameters (DHParameters parameters);
67 private byte[] GetNamedParam(SecurityElement se, string param) {
68 SecurityElement sep = se.SearchForChildByTag(param);
71 return Convert.FromBase64String(sep.Text);
74 /// Reconstructs a <see cref="DiffieHellman"/> object from an XML string.
76 /// <param name="xmlString">The XML string to use to reconstruct the DiffieHellman object.</param>
77 /// <exception cref="CryptographicException">One of the values in the XML string is invalid.</exception>
78 public override void FromXmlString (string xmlString) {
79 if (xmlString == null)
80 throw new ArgumentNullException ("xmlString");
82 DHParameters dhParams = new DHParameters();
84 SecurityParser sp = new SecurityParser();
85 sp.LoadXml(xmlString);
86 SecurityElement se = sp.ToXml();
87 if (se.Tag != "DHKeyValue")
88 throw new CryptographicException();
89 dhParams.P = GetNamedParam(se, "P");
90 dhParams.G = GetNamedParam(se, "G");
91 dhParams.X = GetNamedParam(se, "X");
92 ImportParameters(dhParams);
94 if (dhParams.P != null)
95 Array.Clear(dhParams.P, 0, dhParams.P.Length);
96 if (dhParams.G != null)
97 Array.Clear(dhParams.G, 0, dhParams.G.Length);
98 if (dhParams.X != null)
99 Array.Clear(dhParams.X, 0, dhParams.X.Length);
103 /// Creates and returns an XML string representation of the current <see cref="DiffieHellman"/> object.
105 /// <param name="includePrivateParameters"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
106 /// <returns>An XML string encoding of the current DiffieHellman object.</returns>
107 public override string ToXmlString (bool includePrivateParameters) {
108 StringBuilder sb = new StringBuilder ();
109 DHParameters dhParams = ExportParameters(includePrivateParameters);
111 sb.Append ("<DHKeyValue>");
114 sb.Append (Convert.ToBase64String (dhParams.P));
118 sb.Append (Convert.ToBase64String (dhParams.G));
121 if (includePrivateParameters) {
123 sb.Append (Convert.ToBase64String (dhParams.X));
127 sb.Append ("</DHKeyValue>");
129 Array.Clear(dhParams.P, 0, dhParams.P.Length);
130 Array.Clear(dhParams.G, 0, dhParams.G.Length);
131 if (dhParams.X != null)
132 Array.Clear(dhParams.X, 0, dhParams.X.Length);
134 return sb.ToString ();