示例#1
0
        /// <summary>
        /// Read data from the stream and encrypt it.
        /// </summary>
        /// <param name="buffer">The buffer to read into.</param>
        /// <param name="offset">The offset in the buffer to begin storing data.</param>
        /// <param name="count">The number of bytes to read.</param>
        /// <returns></returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = stream.Read(buffer, offset, count);

            if (target == Target.Normal)
            {
                bf.Encipher(buffer, bytesRead);
            }
            else
            {
                bf.Decipher(buffer, bytesRead);
            }
            return(bytesRead);
        }
示例#2
0
 internal static byte[] Cipher(byte[] buffer, ref byte[] key, bool decrypt)
 {
     var cipher = new Blowfish(key);
     var b = new byte[8];
     using (var m = new MemoryStream(buffer, true))
     {
         while (m.Position < m.Length)
         {
             //Endian swap 2 sets of 4 bytes
             for (int i = 3; i >= 0; i--)
             {
                 b[i] = (byte)m.ReadByte();
             }
             for (int i = 7; i >= 4; i--)
             {
                 b[i] = (byte)m.ReadByte();
             }
             //cipher the 8 bytes
             if (decrypt) cipher.Decipher(b, 8);
             else cipher.Encipher(b, 8);
             //Reset stream position to prepare for writing.
             m.Position -= 8;
             //Endian swap 4 bytes twice
             for (int i = 3; i >= 0; i--)
             {
                 m.WriteByte(b[i]);
             }
             for (int i = 7; i >= 4; i--)
             {
                 m.WriteByte(b[i]);
             }
         }
     }
     return buffer;
 }
示例#3
0
        public void TestArray()
        {
            string       key  = "TestKey1";
            UTF8Encoding utf8 = new UTF8Encoding();
            Blowfish     bf   = new Blowfish(utf8.GetBytes(key));
            string       data = "If you can read this it worked  ";

            byte[] en = utf8.GetBytes(data);
            bf.Encipher(en, en.Length);
            bf.Decipher(en, en.Length);
            Console.WriteLine(utf8.GetString(en, 0, en.Length));
        }