コード例 #1
0
        protected override async Task <int> CreateFreeChannelAsync()
        {
            var config = Settings.Local;

            var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            var aes    = Aes.Create();

            var d = new Rfc2898DeriveBytes(Settings.Password, Settings.SaltSize, Settings.Iterations);

            aes.Key = d.GetBytes(Settings.KeySize);

            var dataPacker   = new DataPacker(aes.CreateEncryptor());   // TODO:???
            var dataUnpacker = new DataUnpacker(aes.CreateDecryptor()); // TODO:???

            await socket.ConnectAsync(config.PeerAddress, config.PeerPort);

            using (var ns = new NetworkStream(socket, ownsSocket: false))
            {
                await ns.WriteAsync(d.Salt, 0, d.Salt.Length);

                await ns.WriteAsync(aes.IV, 0, aes.IV.Length);
            }

            return(AddChannel(socket, dataPacker, dataUnpacker));
        }
コード例 #2
0
        private void Negotiate(Socket channelSocket)
        {
            using (var ns = new NetworkStream(channelSocket, ownsSocket: false))
            {
                var salt = new byte[Settings.SaltSize];
                var iv   = new byte[16];//TODO:???

                FillBuffer(ns, salt);
                FillBuffer(ns, iv);

                var d   = new Rfc2898DeriveBytes(Settings.Password, salt, Settings.Iterations);
                var aes = Aes.Create();
                aes.Key = d.GetBytes(16);
                aes.IV  = iv;

                var dataPacker   = new DataPacker(aes.CreateEncryptor());   // TODO:????
                var dataUnpacker = new DataUnpacker(aes.CreateDecryptor()); // TODO:????

                var channelId = AddChannel(channelSocket, dataPacker, dataUnpacker);
            }
        }