2011-07-16 106 views
0

我在.NET中製作遊戲登錄/遊說。我想通過TcpClient & TcpListener通過SSL處理登錄部分。如何使用這兩個類來處理SSL?我不希望需要在客戶機上安裝任何種類的證書。我寧願我能夠將公鑰直接硬編碼到程序中,但我看到的大多數例子都開始處理證書商店。C#SSL TcpListener TcpClient

有什麼建議嗎?

回答

3

安全套接字層(SSL)僅適用於證書存儲區,如果要使用SSL,則無法避免此問題。

但是,您可以簡單地使用cryptostream進行加密。

一個例子 - 代碼摘自http://www.xtremedotnettalk.com/showthread.php?t=80370

class Class1 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] args) 
    { 
     System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient("localhost",12345); 
     Send s = new Send(); 
     s.sendFile("c:\\boot.ini",tcp.GetStream()); 
    } 
} 

public class Send 
{ 
    TripleDESCryptoServiceProvider tripleDES; 

    public Send() 
    { 
     tripleDES = new TripleDESCryptoServiceProvider(); 
     tripleDES.Key = Encoding.Unicode.GetBytes("foobar12foob".ToCharArray()); 
     tripleDES.IV = Encoding.Unicode.GetBytes("foob".ToCharArray()); 
    } 

    public void sendFile(String fileName, Stream networkStream) 
    { 
     FileStream fin = new FileStream(fileName,FileMode.Open, FileAccess.Read); 

     //Create variables to help with read and write. 
     byte[] bin = new byte[100]; //This is intermediate storage for the encryption. 
     long rdlen = 0; //This is the total number of bytes written. 
     long totlen = fin.Length; //This is the total length of the input file. 
     int len; //This is the number of bytes to be written at a time. 

     CryptoStream encStream = new CryptoStream(networkStream, tripleDES.CreateEncryptor(), CryptoStreamMode.Write); 
     Console.WriteLine("Encrypting..."); 

     //Read from the input file, then encrypt and write to the output file. 
     while(rdlen < totlen) 
     { 
      len = fin.Read(bin, 0, 100); 
      encStream.Write(bin, 0, len); 
      rdlen = rdlen + len; 
      //Console.WriteLine("{0} bytes processed", rdlen); 
     } 

     encStream.Close(); 
    } 
} 

class Class2 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] args) 
    { 
     // 
     // TODO: Add code to start application here 
     // 
     Receive r = new Receive(); 
     System.IO.FileStream fs = System.IO.File.OpenWrite("c:\\test.txt"); 
     System.Net.Sockets.TcpListener tcp = new TcpListener(12345); 
     tcp.Start(); 

     r.receiveFile(fs,tcp.AcceptTcpClient().GetStream()); 
     System.Console.ReadLine(); 
    } 
} 

public class Receive 
{ 
    TripleDESCryptoServiceProvider tripleDES; 

    public Receive() 
    { 
     tripleDES = new TripleDESCryptoServiceProvider(); 
     tripleDES.Key = Encoding.Unicode.GetBytes("foobar12foob".ToCharArray()); 
     tripleDES.IV = Encoding.Unicode.GetBytes("foob".ToCharArray()); 
    } 

    public void receiveFile(FileStream fs, NetworkStream ns) 
    { 
     while(!ns.DataAvailable){} 
     byte[] bin = new byte[100]; 
     long rdlen = 0; 
     int len = 100; 

     CryptoStream decStream = new CryptoStream(fs,tripleDES.CreateDecryptor(), CryptoStreamMode.Write); 
     Console.WriteLine("Decrypting..."); 

     while(len > 0) 
     { 
      len = ns.Read(bin, 0, len); 
      rdlen = rdlen + len; 
      decStream.Write(bin,0,len); 
      Console.WriteLine("{0} bytes read, {1} total bytes", len, rdlen); 
     } 

     decStream.FlushFinalBlock(); 
     decStream.Close(); 

     ns.Close(); 
     fs.Close(); 
    } 
} 
+0

這被認爲是一樣安全的SSL?我正在閱讀的所有內容都表示使用SSL。 因此,在我們的一個項目中,我們實際上通過代碼加載公共的.cert文件並在https請求中使用它。我不會反對做這樣的事情,但我不知道如何創建這樣的證書文件以及如何爲此設置服務器。似乎使用SSL將是最安全的,因爲它是一種經過驗證的真正方法。我喜歡只加密數據,並能夠將它作爲加密文本傳輸,但不知道它有多安全。 – user441521

+0

SSL是完全一樣的東西,只有它是一個128位RSA加密。 SSL使用由可信授權機構進行數字簽名的公鑰,以允許服務器交換對稱公鑰。請參閱[如何使用rsa-to-encrypt-files-huge-data-in-c](http://stackoverflow.com/questions/1199058/how-to-use-rsa-to-encrypt-files -huge-data-in-c) –

+0

我可以用它來接收HTTPs網頁嗎?我創建代理服務器應用程序。它爲HTTP而不是HTTP工作。在上述代碼中做什麼更改以接收任何https頁面並在客戶端的Web瀏覽器上顯示? –