2011-07-16 6 views
0

.NET에서 게임 로그인/로비를 만들고 있습니다. SSL을 통해 TcpClient & TcpListener를 통해 로그인 부분을 처리하려고합니다. 이 두 클래스로 SSL을 사용하여 작업하는 방법은 무엇입니까? 클라이언트 컴퓨터에 설치해야하는 모든 종류의 인증서를 원하지 않습니다. 나는 프로그램에 공개 키를 하드 코딩 할 수 있으면 좋겠다. 그러나 대부분의 예에서는 cert 저장소를 다루기 시작한다.C# SSL TcpListener TcpClient

어떤 조언이 필요합니까?

답변

3

SSL (Secure Socket Layer)은 SSL을 사용하려는 경우 인증서 저장소에서만 작동하므로이 문제를 피할 수 없습니다.

그러나 암호문을 사용하여 간단하게 암호화 할 수 있습니다.

- 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가 아니라 HTTPs를 위해 작동합니다. 위의 코드에서 https 페이지를 받고 클라이언트의 웹 브라우저에 표시하려면 어떻게해야합니까? –