示例#1
0
文件: Utils.cs 项目: pcbing/brunet
        /**
         * <summary>Converts a MemBlock to a string using char sep inbetween each
         * byte.</summary>
         * <param name="input">MemBlock to convert to string</param>
         * <param name="sep"> the separation character (commonly '.' or ',')</param>
         * <returns>The converted string.</returns>
         */
        public static string MemBlockToString(Brunet.Util.MemBlock input, char sep)
        {
            string return_msg = "";

            for (int i = 0; i < input.Length - 1; i++)
            {
                return_msg += input[i].ToString() + sep.ToString();
            }
            return_msg += input[input.Length - 1];
            return(return_msg);
        }
示例#2
0
        /**
         * Injects the binary data into the system.
         * @param assembly_data pre-compiled data that will be injected into the
         * system.
         */
        protected void Inject(Brunet.Util.MemBlock assembly_data)
        {
            Assembly ass = Assembly.Load(assembly_data);

            Type[] types = ass.GetTypes();
            foreach (Type type in types)
            {
                ass.CreateInstance(type.ToString(), false, BindingFlags.CreateInstance,
                                   null, new object[1] {
                    _node
                }, null, null);
            }
        }
示例#3
0
 public void Push(Brunet.Util.MemBlock p)
 {
     if (0 == _is_closed)
     {
         if (Delay > 0)
         {
             var timer = new Brunet.Util.SimpleTimer(DelayedPush, p, Delay, 0);
             timer.Start();
         }
         else
         {
             ReceivedPacketEvent(p);
         }
     }
 }
        /// <summary>Takes in a byte array and returns the data in a unencrypted
        /// format.</summary>
        /// <param name="EncryptedData">The data to decrypt.</param>
        /// <returns>The unencrypted data.</param> */
        public byte[] DecryptData(byte[] EncryptedData)
        {
            Brunet.Util.MemBlock iv_ref = Brunet.Util.MemBlock.Reference(EncryptedData, 0, BlockSizeByte);
            ICryptoTransform     dec    = (ICryptoTransform)_decryptors[iv_ref];

            if (dec == null)
            {
                byte[] iv = new byte[BlockSizeByte];
                iv_ref.CopyTo(iv, 0);
                dec = _sa.CreateDecryptor(_sa.Key, iv);
            }
            else
            {
                _decryptors.Remove(iv_ref);
            }

            int count = EncryptedData.Length - BlockSizeByte;

            iv_ref = Brunet.Util.MemBlock.Reference(EncryptedData, count, BlockSizeByte);
            _decryptors[iv_ref] = dec;


            if ((count % BlockSizeByte) > 0 || count == 0)
            {
                throw new CryptographicException("Invalid input block size.");
            }

            byte[] output = new byte[count];
            dec.TransformBlock(EncryptedData, BlockSizeByte, count, output, 0);

            byte padding = output[count - 1];
            int  length  = count - padding;

            for (int i = length; i < count; i++)
            {
                if (output[i] != padding)
                {
                    throw new CryptographicException(String.Format("Bad padding at position {0}.", i));
                }
            }

            byte[] res = new byte[length];
            Buffer.BlockCopy(output, 0, res, 0, length);
            return(res);
        }
        public void TDES()
        {
            SymmetricEncryption enc = new SymmetricEncryption(new TripleDESCryptoServiceProvider());

            for (int i = 1000; i < 1025; i++)
            {
                byte[] data = new byte[1010];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetBytes(data);
                byte[] encd = enc.EncryptData(data);
                if (i % 12 == 0)
                {
                    continue;
                }
                byte[] decd = enc.DecryptData(encd);
                Brunet.Util.MemBlock mdecd = Brunet.Util.MemBlock.Reference(decd);
                Brunet.Util.MemBlock mdata = Brunet.Util.MemBlock.Reference(data);
                Assert.AreEqual(mdecd, mdata, "TDESEncryption: " + i);
            }
        }