コード例 #1
0
        public void TDES()
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            SymmetricAlgorithm        sa   = new TripleDESCryptoServiceProvider();
            SecurityHandler           sh   = new SecurityHandler(sa, sa, sha1, sha1, 0);

            byte[] data = new byte[1024];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            rng.GetBytes(data);
            SecurityDataMessage sdm = new SecurityDataMessage();

            sdm.SPI  = 5;
            sdm.Data = Brunet.Util.MemBlock.Reference(data);
            sh.SignAndEncrypt(sdm);
            SecurityDataMessage sdm_d = new SecurityDataMessage(sdm.Packet);

            sh.DecryptAndVerify(sdm_d);
            Assert.AreEqual(sdm.Data, sdm.Data, "SecurityHandler");
        }
コード例 #2
0
        ///<summary>All incoming data filters through here.</summary>
        override protected bool HandleIncoming(MemBlock data, out MemBlock app_data)
        {
            if (!_active)
            {
                if (_closed == 0)
                {
                    UpdateSH(null, null);
                }
                app_data = null;
                return(false);
            }

            SecurityDataMessage sdm = new SecurityDataMessage(data);

            if (sdm.SPI != _spi)
            {
                throw new Exception("Invalid SPI!");
            }

            try {
                // try to decrypt the data
                lock (_sync) {
                    _current_sh.DecryptAndVerify(sdm);
                }
            } catch {
                // Maybe this is just a late arriving packet, if it is, we'll just ignore it
                if (sdm.Epoch == _last_epoch)
                {
                    app_data = null;
                    return(false);
                    // bad packet, let's throw it away!
                }
                else
                {
                    throw;
                }
            }

            app_data = sdm.Data;
            return(true);
        }