简体   繁体   English

C# SSL TcpListener TcpClient

[英]C# SSL TcpListener TcpClient

I'm making a game login/lobby in .NET.我正在 .NET 中进行游戏登录/大厅。 I want to handle the login portion via SSL over TcpClient & TcpListener.我想通过 TcpClient 和 TcpListener 通过 SSL 处理登录部分。 How do I go about working with SSL with these 2 classes?我如何 go 关于与这两个类一起使用 SSL ? I don't want any kind of cert that needs to be installed on the client machine.我不想在客户端机器上安装任何类型的证书。 I'd prefer that I would be able to just hardcode the public key right into the program, yet most example I see start dealing with cert stores.我希望我能够将公钥直接硬编码到程序中,但我看到的大多数示例都开始处理证书存储。

Any advice?有什么建议吗?

Secure Socket Layer (SSL) only works with cert stores, if you want to use SSL then you can't avoid this.安全套接字层 (SSL) 仅适用于证书存储,如果您想使用 SSL,那么您将无法避免这种情况。

However you can simply do encryption using an cryptostream.但是,您可以简单地使用加密流进行加密。

An example - Code Excerpt from http://www.xtremedotnettalk.com/showthread.php?t=80370一个例子- 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM