示例#1
0
        public static string Decrypt(string data, LevelEncrypt level)
        {
            GenericEncryption GE = new GenericEncryption();

            try
            {
                switch (level)
                {
                case LevelEncrypt.low:
                {
                    return(GE.DecryptAlgorithm_1(data));
                }

                case LevelEncrypt.medium:
                {
                    string buf = GE.DecryptAlgorithm_2(data);
                    return(GE.DecryptAlgorithm_1(buf));
                }

                default:
                {
                    string buf  = GE.DecryptAlgorithm_1(data);
                    string buf2 = GE.DecryptAlgorithm_2(buf);
                    return(GE.DecryptAlgorithm_1(buf2));
                }
                }
            }
            catch (Exception ex)
            {
                return("Error while decrypting data value: \n" + ex.Message);
            }
        }
示例#2
0
        internal static Context <byte> Decrypt(byte[] data, byte[] password, LevelEncrypt level = LevelEncrypt.Normal, StopProcess stopProcess = null)
        {
            Context <byte> context = new Context <byte>
            {
                Input  = data,
                Output = new byte[GetLengthDecrypted(data.Length, password, level)]
            };

            Decrypt(context, password, level, stopProcess);
            return(context);
        }
示例#3
0
        internal static int GetLengthEncrypted(int lengthOriginal, byte[] password, LevelEncrypt level)
        {
            int levelEncrypt  = (int)level;
            int longitudArray = lengthOriginal;

            //le añado uno porque así el ultimo byte no queda expuesto
            for (int i = 0, f = lengthOriginal + 1; i <= f; i++)
            {
                longitudArray += CalculoTrash(i, password, levelEncrypt);
            }
            return(longitudArray);
        }
示例#4
0
        internal static int GetLengthDecrypted(int lengthOriginal, byte[] password, LevelEncrypt level)
        {
            int longitudAux  = lengthOriginal;
            int levelEncrypt = (int)level;
            int longitud     = 0;
            int pos          = 0;

            //calculo la longitud original
            while (longitudAux > 0)
            {
                //le resto los caracteres random
                longitudAux -= CalculoTrash(pos, password, levelEncrypt);
                //quito el caracter original
                longitudAux--;
                //lo cuento
                longitud++;
                pos++;
            }
            longitud--;//como el ultimo byte esta protegido por basura después de él pues se cuenta otro byte de más
            return(longitud);
        }
示例#5
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));
 }
示例#6
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);
        }
示例#7
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));
        }
示例#8
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));
 }
示例#9
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);
        }
        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);
        }
示例#11
0
 public static Context <T> Decrypt <T>(T[] data, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
 {
     return(EncryptDecrypt(data, password, false, stopProcess, level));
 }
示例#12
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);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
0
 public static Context <byte> Encrypt(byte[] data, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
 {
     return(EncryptDecrypt(data, password, true, stopProcess, level));
 }
示例#16
0
 public static int GetLengthDecrypted(int lengthOriginal, byte[] password, LevelEncrypt level)
 {
     return(CommonDisimulatMethod.GetLengthDecrypted(lengthOriginal, password, level));
 }
        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));
        }
示例#18
0
 public static Context <T> Decrypt <T>(Context <T> data, byte[] password, LevelEncrypt level, StopProcess stopProcess = null)
 {
     return(DecryptEncrypt(data, password, stopProcess, level, IndexPerdutDecrypt));
 }
        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);
        }
示例#20
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);
        }
 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));
 }