Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 3.93 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     /// <summary>
  2.     /// Diffie Hellmann's Key swap process.
  3.     ///  -ServerSide.
  4.     /// </summary>
  5.     public class ServerKeyExchange
  6.         : IDataPacket, IDisposable
  7.     {
  8.         #region Variables
  9.  
  10.         //Writers.
  11.         protected MemoryStream memStream = null;
  12.         protected BinaryWriter binWriter = null;
  13.  
  14.         //Key exchange packet args.
  15.         protected byte[] ServerIv1;
  16.         protected byte[] ServerIv2;
  17.         protected String P = null;
  18.         protected String G = null;
  19.         protected string serverPublicKey = null;
  20.  
  21.         //Const.
  22.         protected const int pad = 11;
  23.         protected const int junk = 12;
  24.         protected const string tqSeal = "TQServer";
  25.  
  26.         #endregion
  27.  
  28.         #region Constructor
  29.  
  30.         /// <summary>
  31.         /// Initializes construct.
  32.         /// </summary>
  33.         /// <param name="ServerIV1"></param>
  34.         /// <param name="ServerIV2"></param>
  35.         /// <param name="P"></param>
  36.         /// <param name="G"></param>
  37.         /// <param name="ServerPublicKey"></param>
  38.         public ServerKeyExchange(byte[] ServerIV1, byte[] ServerIV2, string P, string G, string ServerPublicKey)
  39.         {
  40.             //Initialize values for variables.
  41.             this.ServerIv1 = ServerIV1;
  42.             this.ServerIv2 = ServerIV2;
  43.             this.P = P;
  44.             this.G = G;
  45.             this.serverPublicKey = ServerPublicKey;
  46.  
  47.             //Initialize writers.
  48.             memStream = new MemoryStream();
  49.             binWriter = new BinaryWriter(memStream);
  50.             ComputeExchangePacket();
  51.         }
  52.  
  53.         #endregion
  54.  
  55.         #region Functions
  56.  
  57.         /// <summary>
  58.         /// Generates the Exchange Packet; -ServerSide.
  59.         /// </summary>
  60.         protected internal void ComputeExchangePacket()
  61.         {
  62.             //Assign random junk data to the buffers.
  63.             byte[] padBuff = new byte[pad];
  64.             EntryPoint.rnd.NextBytes(padBuff);
  65.             byte[] junkBuff = new byte[junk];
  66.             EntryPoint.rnd.NextBytes(junkBuff);
  67.             int pSize = 47 + this.P.Length + this.G.Length + this.serverPublicKey.Length + 12 + 8 + 8;
  68.  
  69.             //Write data.
  70.               //-Junk data.
  71.             binWriter.Write(padBuff);
  72.             binWriter.Write(pSize - pad);
  73.             binWriter.Write((UInt32)junk);
  74.             binWriter.Write(junkBuff);
  75.               //-Key data.
  76.             binWriter.Write((UInt32)ServerIv2.Length);
  77.             binWriter.Write(ServerIv2);
  78.             binWriter.Write((UInt32)ServerIv1.Length);
  79.             binWriter.Write(ServerIv1);
  80.             binWriter.Write(this.P.ToCharArray().Length);
  81.             foreach (char fP in this.P.ToCharArray())
  82.             {
  83.                 binWriter.BaseStream.WriteByte((byte)fP);
  84.             }
  85.             binWriter.Write(this.G.ToCharArray().Length);
  86.             foreach (char fG in this.G.ToCharArray())
  87.             {
  88.                 binWriter.BaseStream.WriteByte((byte)fG);
  89.             }
  90.             binWriter.Write((UInt32)this.serverPublicKey.ToCharArray().Length);
  91.             foreach (char SPK in this.serverPublicKey.ToCharArray())
  92.             {
  93.                 binWriter.BaseStream.WriteByte((byte)SPK);
  94.             }
  95.             foreach (char tq in tqSeal.ToCharArray())
  96.             {
  97.                 binWriter.BaseStream.WriteByte((byte)tq);
  98.             }
  99.         }
  100.         /// <summary>
  101.         /// Return byte buffer data.
  102.         /// </summary>
  103.         /// <returns></returns>
  104.         public byte[] ToArray()
  105.         {
  106.             byte[] copyBuffer = new byte[memStream.Length];
  107.             copyBuffer = memStream.ToArray();
  108.             memStream.Close();
  109.             return copyBuffer;
  110.         }
  111.  
  112.         /// <summary>
  113.         /// Disposes of all the garbage.
  114.         /// </summary>
  115.         public void Dispose()
  116.         {
  117.             memStream.Dispose();
  118.             binWriter.Close();
  119.         }
  120.  
  121.         #endregion
  122.     }