示例#1
0
        /// <summary>
        /// This creates a Basyl Key Generator from the arguments..
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="sha"></param>
        /// <param name="Key1Random"></param>
        /// <param name="Key2Random"></param>
        /// <param name="encryptedKey1"></param>
        /// <param name="adaptor"></param>
        public BasylKeyGenerator(string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, byte[] sha, byte[] Key1Random, byte[] Key2Random, bool encryptedKey1, BasylPseudoAdaptor adaptor)
        {
            if (adaptor == null)
            {
                adaptor = new BasylPseudoAdaptor();
            }
            this.sha        = sha;
            this.Key2Random = Key2Random;

            PseudoRandomGenerator Key1 = new PseudoRandomGenerator(initial, pass, rounds, adaptor);

            Key2 = new PseudoRandomGenerator(1024 * 40, pass, 400, adaptor);

            //Set the left off
            Key1.SetLeftoff(leftoff);
            Key2.SetLeftoff(80);

            //Set String Recycle Key
            Key1.SetRecycleKey(additionalKey);
            Key2.SetRecycleKey(additionalKey);

            //Expand the Keys
            Key1.ExpandKey((uint)expansion);
            Key2.ExpandKey(5);

            //Set SHA
            Key1.SetSHA(sha);
            Key2.SetSHA(sha);


            //Add randomness.
            Key2.SetSeedKey(Key2Random);


            //Recycle Key 2
            Key2.Recycle();

            //Stop Recycling Key 2
            //Key2.StopRecycling();


            //Add Key 1 Randomness

            if (encryptedKey1)
            {
                for (int i = 0; i < Key1Random.Length; i++)
                {
                    Key1Random[i] ^= Key2.GetRandomByte();
                }
            }
            this.Key1Random = Key1Random;
            Key1.SetSeedKey(Key1Random);

            //Recycle Key 1
            Key1.Recycle();

            //this.Key1 = new FilePseudoRandomGenerator(File.Open("Key1", FileMode.Create), Key1, additionalKey, Key1Random, sha, leftoff);
            this.Key1 = Key1;
        }
 public PseudoRandomGenerator(int size, string key, int rounds, BasylPseudoAdaptor basylPseudoAdaptor)
 {
     position = 0;
     this.rounds = rounds;
     Generation = new List<ulong>();
     this.recycleKey = "";
     ResizeBoth(size);
     this.basylPseudoAdaptor = basylPseudoAdaptor;
     Generate(key);
 }
 public PseudoRandomGenerator(int size, string key, int rounds, BasylPseudoAdaptor basylPseudoAdaptor)
 {
     position        = 0;
     this.rounds     = rounds;
     Generation      = new List <ulong>();
     this.recycleKey = "";
     ResizeBoth(size);
     this.basylPseudoAdaptor = basylPseudoAdaptor;
     Generate(key);
 }
示例#4
0
        /// <summary>
        /// This is mainly used by the Basyl Writer.
        /// Creates a Basyl Key Generator from the arguments.
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="sha"></param>
        ///<param name="adaptor"></param>
        public BasylKeyGenerator(string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, byte[] sha, BasylPseudoAdaptor adaptor)
        {
            if (adaptor == null)
            {
                adaptor = new BasylPseudoAdaptor();
            }
            RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();

            PseudoRandomGenerator Key1 = new PseudoRandomGenerator(initial, pass, rounds, adaptor);

            Key2 = new PseudoRandomGenerator(1024 * 40, pass, 400, adaptor);

            Key1Random = new byte[4];
            Key2Random = new byte[4];

            //Set the left off
            Key1.SetLeftoff(leftoff);
            Key2.SetLeftoff(80);


            //Set String Recycle Key
            Key1.SetRecycleKey(additionalKey);
            Key2.SetRecycleKey(additionalKey);

            //Expand the Keys
            Key1.ExpandKey((uint)expansion);
            Key2.ExpandKey(5);


            //Generate Randomness
            random.GetBytes(Key1Random);
            random.GetBytes(Key2Random);

            //Add randomness.
            Key1.SetSeedKey(Key1Random);
            Key2.SetSeedKey(Key2Random);

            //if sha exists
            if (sha != null)
            {
                this.sha = sha;
                //Set SHA
                Key1.SetSHA(sha);
                Key2.SetSHA(sha);
            }

            //Recycle the Keys
            Key1.Recycle();
            Key2.Recycle();

            //this.Key1 = new FilePseudoRandomGenerator(File.Open("Key1", FileMode.Create), Key1, additionalKey, Key1Random, sha, leftoff);
            this.Key1 = Key1;
        }
        /// <summary>
        /// This creates a Basyl Key Generator from the arguments..
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="sha"></param>
        /// <param name="Key1Random"></param>
        /// <param name="Key2Random"></param>
        /// <param name="encryptedKey1"></param>
        /// <param name="adaptor"></param>
        public BasylKeyGenerator(string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, byte[] sha, byte[] Key1Random, byte[] Key2Random, bool encryptedKey1, BasylPseudoAdaptor adaptor)
        {
            if (adaptor == null) adaptor = new BasylPseudoAdaptor();
            this.sha = sha;
            this.Key2Random = Key2Random;

            PseudoRandomGenerator Key1 = new PseudoRandomGenerator(initial, pass, rounds, adaptor);
            Key2 = new PseudoRandomGenerator(1024 * 40, pass, 400, adaptor);

            //Set the left off
            Key1.SetLeftoff(leftoff);
            Key2.SetLeftoff(80);

            //Set String Recycle Key
            Key1.SetRecycleKey(additionalKey);
            Key2.SetRecycleKey(additionalKey);

            //Expand the Keys
            Key1.ExpandKey((uint)expansion);
            Key2.ExpandKey(5);

            //Set SHA
            Key1.SetSHA(sha);
            Key2.SetSHA(sha);

            //Add randomness.
            Key2.SetSeedKey(Key2Random);

            //Recycle Key 2
            Key2.Recycle();

            //Stop Recycling Key 2
            //Key2.StopRecycling();

            //Add Key 1 Randomness

            if(encryptedKey1)
            for (int i = 0; i < Key1Random.Length; i++)
            {
                Key1Random[i] ^= Key2.GetRandomByte();
            }
            this.Key1Random = Key1Random;
            Key1.SetSeedKey(Key1Random);

            //Recycle Key 1
            Key1.Recycle();

            //this.Key1 = new FilePseudoRandomGenerator(File.Open("Key1", FileMode.Create), Key1, additionalKey, Key1Random, sha, leftoff);
            this.Key1 = Key1;
        }
        /// <summary>
        /// This is mainly used by the Basyl Writer.
        /// Creates a Basyl Key Generator from the arguments.
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="sha"></param>
        ///<param name="adaptor"></param>
        public BasylKeyGenerator(string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, byte[] sha, BasylPseudoAdaptor adaptor)
        {
            if (adaptor == null) adaptor = new BasylPseudoAdaptor();
            RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();

            PseudoRandomGenerator Key1 = new PseudoRandomGenerator(initial, pass, rounds, adaptor);
            Key2 = new PseudoRandomGenerator(1024 * 40, pass, 400, adaptor);

            Key1Random = new byte[4];
            Key2Random = new byte[4];

            //Set the left off
            Key1.SetLeftoff(leftoff);
            Key2.SetLeftoff(80);

            //Set String Recycle Key
            Key1.SetRecycleKey(additionalKey);
            Key2.SetRecycleKey(additionalKey);

            //Expand the Keys
            Key1.ExpandKey((uint)expansion);
            Key2.ExpandKey(5);

            //Generate Randomness
            random.GetBytes(Key1Random);
            random.GetBytes(Key2Random);

            //Add randomness.
            Key1.SetSeedKey(Key1Random);
            Key2.SetSeedKey(Key2Random);

            //if sha exists
            if (sha != null)
            {
                this.sha = sha;
                //Set SHA
                Key1.SetSHA(sha);
                Key2.SetSHA(sha);
            }

            //Recycle the Keys
            Key1.Recycle();
            Key2.Recycle();

            //this.Key1 = new FilePseudoRandomGenerator(File.Open("Key1", FileMode.Create), Key1, additionalKey, Key1Random, sha, leftoff);
            this.Key1 = Key1;
        }
示例#7
0
 /// <summary>
 /// Encrypts a file with the parameters
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="outputFileName"></param>
 /// <param name="pass"></param>
 /// <param name="initial"></param>
 /// <param name="rounds"></param>
 /// <param name="leftoff"></param>
 /// <param name="expansion"></param>
 /// <param name="additionalKey"></param>
 /// <param name="callback"></param>
 /// <param name="adaptor"></param>
 public static void Encrypt(string fileName, string outputFileName, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, BasylFileEncryption.Callback callback, BasylPseudoAdaptor adaptor)
 {
     Encrypt(File.OpenRead(fileName), File.OpenWrite(outputFileName), pass, initial, rounds, leftoff, expansion, additionalKey, callback, adaptor);
 }
示例#8
0
        /// <summary>
        /// Decrypts a file with the parameters.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="callback"></param>
        public static void Decrypt(Stream input, Stream output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, BasylFileEncryption.Callback callback, BasylPseudoAdaptor adaptor)
        {
            //read in the necessary randomized info.

            byte[] sha = new byte[32];
            byte[] f   = new byte[4];
            byte[] f2  = new byte[4];

            input.Read(sha, 0, 32);
            input.Read(f2, 0, 4);
            input.Read(f, 0, 4);

            BasylKeyGenerator bkg    = new BasylKeyGenerator(pass, initial, rounds, leftoff, expansion, additionalKey, sha, f, f2, true, adaptor);
            BESCipher         cipher = new BESCipher(bkg);

            int speed = MAX_SPEED;

            while (speed > MIN_SPEED)
            {
                //Encrypt Entire File in Chunks
                byte[] buffer = new byte[speed];
                while (input.Position + speed <= input.Length)
                {
                    input.Read(buffer, 0, speed);

                    cipher.EncryptLeft(ref buffer);
                    output.Write(buffer, 0, speed);

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

            input.Close();
            output.Close();
        }
示例#9
0
        /// <summary>
        /// Encrypts a file from the parameters.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="callback"></param>
        /// <param name="adaptor"></param>
        public static void Encrypt(Stream input, Stream output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, BasylFileEncryption.Callback callback, BasylPseudoAdaptor adaptor)
        {
            //The SHA guarantees that no two files will have the same key for encryption and decryption.
            byte[] sha = SHA256.Create().ComputeHash(input);
            input.Position = 0;
            BasylKeyGenerator bkg = new BasylKeyGenerator(pass, initial, rounds, leftoff, expansion, additionalKey, sha, adaptor);

            //write out the necessary randomized info.
            output.Write(sha, 0, 32);
            output.Write(bkg.GetSecondRandomizer(), 0, 4);
            output.Write(bkg.GetEncryptedKey1Random(), 0, 4);


            BESCipher cipher = new BESCipher(bkg);

            int speed = MAX_SPEED;

            while (speed > MIN_SPEED)
            {
                //Encrypt Entire File in Chunks
                byte[] buffer = new byte[speed];
                while (input.Position + speed <= input.Length)
                {
                    input.Read(buffer, 0, speed);

                    cipher.EncryptRight(ref buffer);
                    output.Write(buffer, 0, speed);

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

            input.Close();
            output.Close();
        }
        /// <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();
        }
 /// <summary>
 /// Encrypts a file stream with the given configuration.
 /// Not for networking.
 /// </summary>
 /// <param name="input">Input File String</param>
 /// <param name="output">Output File String</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="adaptor"></param>
 public static void Encrypt(String input, String output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, Callback callback, BasylPseudoAdaptor adaptor)
 {
     Encrypt(File.OpenRead(input), File.OpenWrite(output), pass, initial, rounds, leftoff, expansion, additionalKey, callback, adaptor);
 }
        /// <summary>
        /// Decrypts a stream with the given configuration.
        /// Not for networking.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="callback"></param>
        /// <param name="adaptor">Adaptor for PRNG</param>
        public static void Decrypt(Stream input, Stream output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, Callback callback, BasylPseudoAdaptor adaptor)
        {
            BinaryWriter writer = new BinaryWriter(output);

            byte[] hash = new byte[32];
            byte[] o    = new byte[4];
            byte[] d    = new byte[4];
            input.Read(hash, 0, 32);
            input.Read(o, 0, 4);
            input.Read(d, 0, 4);
            BasylReader reader = new BasylReader(input, new BasylKeyGenerator(pass, initial, rounds, leftoff, expansion, additionalKey, hash, d, o, true, adaptor));

            //Speeds up decryption by doing the decryption in chunks.
            int speed = MAX_SPEED;

            while (speed > MIN_SPEED)
            {
                //Decrypt Entire File in Chunks
                while (reader.GetStream().Position + speed < reader.GetStream().Length)
                {
                    writer.Write(reader.ReadBytes(speed));
                    if (callback != null)
                    {
                        callback((double)reader.GetStream().Position / reader.GetStream().Length);
                    }
                }
                speed >>= 1;
            }


            //Decrypt Entire File
            while (reader.GetStream().Position < reader.GetStream().Length)
            {
                writer.Write(reader.ReadByte());
                if (callback != null)
                {
                    callback((double)reader.GetStream().Position / reader.GetStream().Length);
                }
            }


            writer.Close();
            reader.Close();
            reader.Dispose();
            writer.Dispose();
        }
 /// <summary>
 /// Decrypts a file with the parameters.
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="outputFileName"></param>
 /// <param name="pass"></param>
 /// <param name="initial"></param>
 /// <param name="rounds"></param>
 /// <param name="leftoff"></param>
 /// <param name="expansion"></param>
 /// <param name="additionalKey"></param>
 /// <param name="callback"></param>
 /// <param name="adaptor"></param>
 public static void Decrypt(string fileName, string outputFileName, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, BasylFileEncryption.Callback callback, BasylPseudoAdaptor adaptor)
 {
     Decrypt(File.OpenRead(fileName), File.OpenWrite(outputFileName), pass, initial, rounds, leftoff, expansion, additionalKey, callback, adaptor);
 }
        /// <summary>
        /// Encrypts a file from the parameters.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="callback"></param>
        /// <param name="adaptor"></param>
        public static void Encrypt(Stream input, Stream output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, BasylFileEncryption.Callback callback, BasylPseudoAdaptor adaptor)
        {
            //The SHA guarantees that no two files will have the same key for encryption and decryption.
            byte[] sha = SHA256.Create().ComputeHash(input);
            input.Position = 0;
            BasylKeyGenerator bkg = new BasylKeyGenerator(pass, initial, rounds, leftoff, expansion, additionalKey, sha, adaptor);

            //write out the necessary randomized info.
            output.Write(sha, 0, 32);
            output.Write(bkg.GetSecondRandomizer(), 0, 4);
            output.Write(bkg.GetEncryptedKey1Random(), 0, 4);

            BESCipher cipher = new BESCipher(bkg);

            int speed = MAX_SPEED;
            while (speed > MIN_SPEED)
            {
                //Encrypt Entire File in Chunks
                byte[] buffer = new byte[speed];
                while (input.Position + speed <= input.Length)
                {
                    input.Read(buffer, 0, speed);

                    cipher.EncryptRight(ref buffer);
                    output.Write(buffer, 0, speed);

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

                }
                speed >>= 1;
            }

            input.Close();
            output.Close();
        }
        /// <summary>
        /// Decrypts a file with the parameters.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="pass"></param>
        /// <param name="initial"></param>
        /// <param name="rounds"></param>
        /// <param name="leftoff"></param>
        /// <param name="expansion"></param>
        /// <param name="additionalKey"></param>
        /// <param name="callback"></param>
        public static void Decrypt(Stream input, Stream output, string pass, int initial, int rounds, int leftoff, int expansion, string additionalKey, BasylFileEncryption.Callback callback, BasylPseudoAdaptor adaptor)
        {
            //read in the necessary randomized info.

            byte[] sha = new byte[32];
            byte[] f = new byte[4];
            byte[] f2 = new byte[4];

            input.Read(sha, 0, 32);
            input.Read(f2, 0, 4);
            input.Read(f, 0, 4);

            BasylKeyGenerator bkg = new BasylKeyGenerator(pass, initial, rounds, leftoff, expansion, additionalKey, sha, f, f2, true, adaptor);
            BESCipher cipher = new BESCipher(bkg);

            int speed = MAX_SPEED;
            while (speed > MIN_SPEED)
            {
                //Encrypt Entire File in Chunks
                byte[] buffer = new byte[speed];
                while (input.Position + speed <= input.Length)
                {
                    input.Read(buffer, 0, speed);

                    cipher.EncryptLeft(ref buffer);
                    output.Write(buffer, 0, speed);

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

                }
                speed >>= 1;
            }

            input.Close();
            output.Close();
        }