/// <summary>
        /// Encrypts a file stream with the given configuration.
        /// Not for networking.
        /// </summary>
        /// <param name="input">Input Stream</param>
        /// <param name="output">Output Stream</param>
        /// <param name="pass">Password</param>
        /// <param name="initial">Initial Key Size</param>
        /// <param name="rounds">Rounds of Key Generation</param>
        /// <param name="leftoff">Chunk of data left out</param>
        /// <param name="expansion">Multiplier for a key size. (Grows it).</param>
        /// <param name="additionalKey">Key to recycle</param>
        /// <param name="callback">Callback method</param>
        /// <param name="adaptor">Adaptor for PRNG</param>
        public static void Encrypt(Stream input, Stream output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, Callback callback, BasylPseudoAdaptor adaptor)
        {
            BinaryReader reader = new BinaryReader(input);


            //The SHA guarantees that no two files will have the same key for encryption and decryption.
            byte[] sha = SHA256.Create().ComputeHash(reader.BaseStream);
            reader.BaseStream.Position = 0;

            BasylWriter writer = new BasylWriter(output, new BasylKeyGenerator(pass, initial, rounds, leftoff, expansion, additionalKey, sha, adaptor), true);

            int speed = MAX_SPEED;

            while (speed > MIN_SPEED)
            {
                //Encrypt Entire File in Chunks
                while (reader.BaseStream.Position + speed < reader.BaseStream.Length)
                {
                    byte[] bytes = reader.ReadBytes(speed);
                    writer.Write(bytes);

                    if (callback != null)
                    {
                        callback((double)reader.BaseStream.Position / reader.BaseStream.Length);
                    }
                }
                speed >>= 1;
            }

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                writer.Write(reader.ReadByte());

                if (callback != null)
                {
                    callback((double)reader.BaseStream.Position / reader.BaseStream.Length);
                }
            }


            writer.Close();
            writer.Dispose();
            reader.Close();
            reader.Dispose();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a BasylReader from a Writer.
        /// Completely Synchronized and Attached.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="pass"></param>
        /// <param name="writer"></param>
        /// <returns></returns>
        public static BasylReader CreateFrom(Stream stream, BasylWriter writer)
        {
            BasylReader result = new BasylReader(stream, writer.GetKeyGenerator());

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a Writer from the Reader and shares its key.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static BasylWriter CreateFrom(Stream s, BasylReader reader)
        {
            BasylWriter writer = new BasylWriter(s, reader.GetKeyGenerator());

            return(writer);
        }