Example #1
0
        static Context <byte> EncryptDecrypt(Context <byte> context, byte[] password, LevelEncrypt level, MethodCesar method, StopProcess stopProcess = null)
        {
            int levelEncrypt = (int)level;

            if (Equals(stopProcess, default))
            {
                stopProcess = new StopProcess();
            }
            else
            {
                stopProcess.Continue = true;
            }

            unsafe
            {
                byte *ptrOutput;

                fixed(byte *ptOutput = context.Output)
                {
                    ptrOutput = ptOutput + context.OutputIndex;

                    for (; !context.Acabado && stopProcess.Continue; ptrOutput++, context.OutputIndex++)
                    {
                        *ptrOutput = method(context, password, levelEncrypt, ptrOutput);
                    }
                }
            }
            return(context);
        }
Example #2
0
        static Context <T> DecryptEncrypt <T>(Context <T> data, byte[] password, StopProcess stopProcess, LevelEncrypt level, IndexPerdutMethod <T> metodoObtenerIndexOutput)
        {
            T    aux;
            long index;
            int  levelEncrypt = (int)level;


            if (Equals(stopProcess, null))
            {
                stopProcess = new StopProcess();
            }
            else
            {
                stopProcess.Continue = true;
            }

            for (; !data.Acabado && stopProcess.Continue; data.OutputIndex++)
            {
                index = metodoObtenerIndexOutput(data, password, levelEncrypt);
                aux   = data.Output[index];
                data.Output[index]            = data.Output[data.OutputIndex];
                data.Output[data.OutputIndex] = aux;
            }

            return(data);
        }
Example #3
0
        internal static Context <byte> Encrypt(Context <byte> context, byte[] password, int min, int max, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            if (Equals(stopProcess, null))
            {
                stopProcess = new StopProcess();
            }
            else
            {
                stopProcess.Continue = true;
            }

            int randomTrush;
            int levelEncrypt = (int)level;


            for (; !context.Acabado && stopProcess.Continue; context.InputIndex++)
            {
                //pongo random
                randomTrush = CalculoTrash(context.InputIndex, password, levelEncrypt);
                for (int i = 0; i < randomTrush; i++)
                {
                    context.Output[context.OutputIndex] = (byte)MiRandom.Next(min, max);
                    context.OutputIndex++;
                }
                //pongo data
                context.Output[context.OutputIndex] = context.Input[context.InputIndex];
                context.OutputIndex++;
            }
            if (!context.Acabado && stopProcess.Continue)
            {
                //pongo random para tapar el Ășltimo byte
                randomTrush = CalculoTrash(context.InputIndex, password, levelEncrypt);
                for (int i = 0; i < randomTrush; i++)
                {
                    context.Output[context.OutputIndex] = (byte)MiRandom.Next(min, max);
                    context.OutputIndex++;
                }
            }

            return(context);
        }
        public static async Task <Context <byte> > Encrypt(byte[] data, byte[] password, EncryptMethod method, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            if (Equals(stopProcess, default))
            {
                stopProcess = new StopProcess();
            }
            else
            {
                stopProcess.Continue = true;
            }

            Context <byte> context = default;

            Action decryptData = () => {
                switch (method)
                {
                case EncryptMethod.Cesar:
                    context = CesarMethod.Encrypt(data, password, level, stopProcess);

                    break;

                case EncryptMethod.Perdut:
                    context = PerdutMethod.Encrypt(data, password, level, stopProcess);

                    break;

                case EncryptMethod.Disimulat:
                    context = DisimulatMethod.Encrypt(data, password, level, stopProcess);

                    break;

                case EncryptMethod.DisimulatRandom:
                    context = DisimulatRandomMethod.Encrypt(data, password, level, stopProcess);

                    break;
                }
            };
            await Task.Run(decryptData);

            return(context);
        }
        public static async Task <IList <Context <byte> > > Encrypt(this IList <Context <byte> > contexts, byte[] password, EncryptMethod method, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            if (Equals(stopProcess, default))
            {
                stopProcess = new StopProcess();
            }
            else
            {
                stopProcess.Continue = true;
            }

            Task[] tasks = new Task[contexts.Count];
            for (int i = 0; i < contexts.Count; i++)
            {
                tasks[i] = Encrypt(contexts[i], password, method, level, stopProcess);
            }

            await Task.WhenAll(tasks);

            return(contexts);
        }
Example #6
0
        internal static Context <byte> Decrypt(Context <byte> context, byte[] password, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            if (Equals(stopProcess, null))
            {
                stopProcess = new StopProcess();
            }
            else
            {
                stopProcess.Continue = true;
            }
            int  randomTrush;
            int  levelEncrypt = (int)level;
            long lengthOutput = context.Output.Length;

            for (; context.OutputIndex < lengthOutput && stopProcess.Continue; context.OutputIndex++)
            {
                //quito random
                randomTrush         = CalculoTrash(context.OutputIndex, password, levelEncrypt);
                context.InputIndex += randomTrush;
                //pongo data
                context.Output[context.OutputIndex] = context.Input[context.InputIndex];
            }
            return(context);
        }
Example #7
0
 public static Context <byte> Decrypt(Context <byte> context, byte[] password, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
 {
     return(CommonDisimulatMethod.Decrypt(context, password, level, stopProcess));
 }
Example #8
0
        public static Context <byte> Encrypt(Context <byte> context, byte[] password, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            TwoKeys <int, int> minMax = GetMinMax(context.Input);

            return(CommonDisimulatMethod.Encrypt(context, password, minMax.Key1, minMax.Key2, level, stopProcess));
        }
Example #9
0
 public static Context <byte> Encrypt(byte[] data, byte[] password, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
 {
     return(CommonDisimulatMethod.Encrypt(data, password, 0, MAX, level, stopProcess));
 }
Example #10
0
 public static Context <T> Decrypt <T>(Context <T> data, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
 {
     return(DecryptEncrypt(data, password, stopProcess, level, IndexPerdutDecrypt));
 }
        static async Task <Context <byte>[]> EncryptDecrypt(byte[] data, byte[] password, StopProcess stopProcess, EncryptMethod method, LevelEncrypt level, bool encryptOrDecrypt, int buffer = -1)
        {
            if (buffer <= 0)
            {
                buffer = data.Length;
            }

            Task <Context <byte> >[] contexts;
            byte[] aux;

            int total = data.Length / buffer;

            if (data.Length % buffer != 0)
            {
                total++;
            }
            contexts = new Task <Context <byte> > [total];

            for (int i = 0; i < contexts.Length; i++)
            {
                aux = data.SubArray(i * buffer, buffer);
                if (encryptOrDecrypt)
                {
                    contexts[i] = Encrypt(aux, password, method, level, stopProcess);
                }
                else
                {
                    contexts[i] = Decrypt(aux, password, method, level, stopProcess);
                }
            }
            await Task.WhenAll(contexts);



            return(contexts.Convert((c) => c.Result));
        }
Example #12
0
 public static Context <T> Decrypt <T>(T[] data, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
 {
     return(EncryptDecrypt(data, password, false, stopProcess, level));
 }
Example #13
0
        public static Context <byte> Encrypt(Context <byte> context, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
        {
            Context <byte> result;

            unsafe
            {
                result = EncryptDecrypt(context, password, level, EncryptCesar, stopProcess);
            }
            return(result);
        }
Example #14
0
 public static Context <byte> Encrypt(byte[] data, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
 {
     return(EncryptDecrypt(data, password, true, stopProcess, level));
 }
Example #15
0
        static Context <T> EncryptDecrypt <T>(T[] data, byte[] password, bool encryptOrDecrypt = false, StopProcess stopProcess = null, LevelEncrypt level = LevelEncrypt.Normal)
        {
            Context <T> context = new Context <T>
            {
                Target = nameof(PerdutMethod),
                Output = data
            };

            if (encryptOrDecrypt)
            {
                Encrypt(context, password, level, stopProcess);
            }
            else
            {
                Decrypt(context, password, level, stopProcess);
            }
            return(context);
        }
Example #16
0
        internal static Context <byte> Encrypt(byte[] data, byte[] password, int min, int max, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            Context <byte> context = new Context <byte>
            {
                Input  = data,
                Output = new byte[GetLengthEncrypted(data.Length, password, level)]
            };

            Encrypt(context, password, min, max, level, stopProcess);
            return(context);
        }
 public static async Task <Context <byte>[]> Decrypt(byte[] data, byte[] password, EncryptMethod method, int buffer, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
 {
     return(await EncryptDecrypt(data, password, stopProcess, method, level, false, buffer));
 }