网络流量加密

时间:2011-10-07 13:03:27

标签: c# network-programming

我有一个重新发送网络流量的代理程序。它侦听特定端口,并使用另一个端口寻址服务器。它有效,但我需要在我的代理的两个实例之间添加一些加密。 当加密未启用时,两个顺序代理工作正常,但加密不起作用。以下是代码:

public class CommunicationContext
{
   /// <summary>Define buffer max size. Influence on memory usage.</summary>
   public const int MaxBuffer = 16 * 1024;

   /// <summary>Unique counter...</summary>
   private static int _uniqueCounter = 0;

   public readonly Socket SocketIn;
   public readonly Socket SocketOut;
   public readonly PortMapping Mapping;
   public byte[] BufferIn;
   public byte[] BufferOut;
   public bool IsShutdown;
   public readonly object SyncObject = new object();
   public readonly int UniqueId;

   public CommunicationContext(Socket socketIn, Socket socketOut, PortMapping map){
       SocketIn = socketIn;
       SocketOut = socketOut;
       Mapping = map;

       UniqueId = Interlocked.Increment( ref _uniqueCounter );
   }

   public void InitializeBuffers(){
       this.BufferIn = new byte[MaxBuffer];
       this.BufferOut = new byte[MaxBuffer];
   }
}

private static void ReadInputSocket( IAsyncResult ar ){
  CommunicationContext context = (CommunicationContext)ar.AsyncState;

  try{
    int length = context.SocketIn.EndReceive( ar );

    if( length <= 0 )
      throw new NoDataSocketException();

    lock( context.SyncObject ){
        Switches.GeneralLog.Verbose( "==> Client data size: " + length );
        SocketFlags flags = ( context.SocketIn.Available == 0 ) ? SocketFlags.None : SocketFlags.Partial;

        if(!CryptoTools.CryptEnabled){
            //without encrypion works fine
        }
        else if(CryptoTools.CryptInner){
            context.BufferIn = CryptoTools.Crypt(context.BufferIn);
        }else{
            context.BufferIn = CryptoTools.Decrypt(context.BufferIn);
        }

        context.SocketOut.Send(context.BufferIn, 0, length, flags);  
    }

    Thread.Sleep( 0 );

    context.SocketIn.BeginReceive(context.BufferIn, 0, MaxBuffer, SocketFlags.None, ReadInputSocket, context);
  }
  catch( Exception ex ){
    Switches.GeneralLog.Verbose( ex );
    Switches.GeneralLog.Info( ex.Message );
    ShutdownCommunication( context );
  }
}

private static void ReadOutputSocket(IAsyncResult ar ){
  CommunicationContext context = (CommunicationContext)ar.AsyncState;

  try{
    int length = context.SocketOut.EndReceive( ar);

    if( length <= 0 )
      throw new NoDataSocketException();

    lock( context.SyncObject )
    {
      Switches.GeneralLog.Verbose( "<== Server data size: " + length );
      SocketFlags flags = ( context.SocketOut.Available == 0 ) ? SocketFlags.None : SocketFlags.Partial;


      if (!CryptoTools.CryptEnabled){
          //without encrypion works fine
      }
      else if (CryptoTools.CryptInner){
          context.BufferOut = CryptoTools.Decrypt(context.BufferOut);
      }
      else{
          context.BufferOut = CryptoTools.Crypt(context.BufferOut);
      }

      context.SocketIn.Send(context.BufferOut, 0, length, flags);
    }

    context.SocketOut.BeginReceive(context.BufferOut, 0, MaxBuffer, SocketFlags.None, ReadOutputSocket, context);
  }
  catch( Exception ex )
  {
    Switches.GeneralLog.Verbose( ex );
    Switches.GeneralLog.Info( ex.Message );

    ShutdownCommunication( context );
  }
}

从评论中编辑:

什么不行:数据不断被破坏。

我没有例外。只是格式错误的数据。我为Crypt / Decrypt使用了不同的方法。我让它们都是平等的 - 简单的XOR。加密算法没有意义,例如,使用了XOR。

我的配置与此Client <--> Proxy1 <--enc--> Proxy2 <---> Server类似。两个代理之间必须是加密流。

0 个答案:

没有答案