Exemplo n.º 1
0
        public string CifrarArchivo(string path, int key)
        {
            GenerateKeys(key);

            #region Crear_Archivo
            string NuevoArchivo = Path.GetFileName(path);
            string rutaCifrado  = Directories.directorioArchivos + NuevoArchivo;
            Archivo.crearArchivo(rutaCifrado);
            #endregion

            using (var file = new FileStream(path, FileMode.Open))
            {
                using (var reader = new BinaryReader(file))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        var         buffer          = reader.ReadBytes(bufferLength);
                        List <byte> CompresionBytes = new List <byte>();

                        foreach (var item in buffer)
                        {
                            //Comprimir

                            string _byte = ConvertByteToString(item);
                            string InitialPermutation = Permutate(_byte, this.PI);

                            string result1 = Swap(AlgorithmSDES(InitialPermutation, this.K1));
                            string result2 = Permutate(AlgorithmSDES(result1, K2), this.PIn);

                            CompresionBytes.Add(ConvertStringToByte(result2));
                        }

                        WriteToFile(rutaCifrado, CompresionBytes.ToArray());
                    }
                }
            }

            return(rutaCifrado);
        }
Exemplo n.º 2
0
        public string descomprimirArchivo()
        {
            int maxDictionaryLenght = LZW.dictionaryLenght;

            string bits = "";

            using (var file = new FileStream(path, FileMode.Open))
            {
                using (var reader = new BinaryReader(file))
                {
                    //Definir diccionario
                    Dictionary <int, string> diccionario = obtenerDiccionarioDescompresion();

                    //Operacion inicial
                    int    byteLenght   = initialByteLenght;
                    int    key          = 0;
                    string c            = diccionario[(int)reader.ReadByte()];
                    string descomprimir = c;

                    //Buffer para descomprimir
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        var buffer = reader.ReadBytes(count: bufferLength);

                        foreach (var t in buffer)
                        {
                            if (diccionario.Count + 1 >= maxDictionaryLenght)
                            {
                                byteLenght++;
                                maxDictionaryLenght = (int)Math.Pow(2, byteLenght);
                            }

                            string ByteString = Convert.ToString(t, 2).PadLeft(8, '0'); // produce cadena "00111111";
                            bits += ByteString;

                            if (bits.Length >= byteLenght)
                            {
                                key  = Convert.ToInt32(bits.Substring(0, byteLenght), 2);
                                bits = bits.Remove(0, byteLenght);

                                string entry = null;
                                if (diccionario.ContainsKey(key))
                                {
                                    entry = diccionario[key];
                                }
                                else if (key == diccionario.Count)
                                {
                                    entry = c + c[0];
                                }

                                descomprimir += entry;

                                //  Agregar nueva frase al diccionario

                                if (entry != null)
                                {
                                    diccionario.Add(diccionario.Count, c + entry[0]);
                                }

                                c = entry;
                            }
                        }

                        //Lectura.Escritura(descomprimir, rutaArchivo);
                        Archivo.escribirEnArchivo(rutaComprimido, descomprimir);
                        descomprimir = "";
                    }
                }
            }

            //Descargar
            return(rutaComprimido);
        }
Exemplo n.º 3
0
        public static string comprimirArchivo(string pathOrigen, string pathDestino)
        {
            //File
            string nombreNuevoArchivo = Path.GetFileName(pathOrigen);
            string rutaComprimido     = Path.Combine(pathDestino, nombreNuevoArchivo);

            Archivo.crearArchivo(rutaComprimido);

            //Crear dicionario
            var diccionario = obtenerDiccionarioCompresion();

            //Analizar texto, creando diccionario y escribiendo en archivo
            #region Algoritmo

            //Empezar concatenar
            string     c         = string.Empty;
            List <int> comprimir = new List <int>();

            int    byteLenght          = initialByteLenght;
            int    maxDictionaryLenght = dictionaryLenght;
            string bits = "";

            //Buffer para comprimir
            using (var file = new FileStream(pathOrigen, FileMode.Open))
            {
                using (var reader = new BinaryReader(file))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        var buffer = reader.ReadBytes(count: bufferLength);

                        foreach (var t in buffer)
                        {
                            string ct = c + ((char)t);
                            if (diccionario.ContainsKey(ct))
                            {
                                c = ct;
                            }
                            else
                            {
                                //sacarlo
                                string ByteString = Convert.ToString(diccionario[c], 2).PadLeft(byteLenght, '0'); // produce cadena "00111111";
                                bits += ByteString;

                                while (bits.Length >= 8) //Escribir bytes en archivo
                                {
                                    string Byte = bits.Substring(0, 8);
                                    bits = bits.Remove(0, 8);

                                    ByteArrayToFile(rutaComprimido, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) });
                                }

                                comprimir.Add(diccionario[c]);
                                //Aqui ya lo concatena y lo agrega
                                diccionario.Add(ct, diccionario.Count);
                                c = ((char)t).ToString();
                                //Verificar tamaño de los bits
                                if (diccionario.Count >= maxDictionaryLenght)
                                {
                                    byteLenght++;
                                    maxDictionaryLenght = (int)Math.Pow(2, byteLenght);
                                }
                            }
                        }
                    }
                }
            }

            //Ultima cadena del archivo
            if (!string.IsNullOrEmpty(c))
            {
                string ByteString = Convert.ToString(diccionario[c], 2).PadLeft(byteLenght, '0'); // produce cadena "00111111";
                bits += ByteString;

                comprimir.Add(diccionario[c]);
            }

            if (bits != "") //Bits restantes
            {
                while (bits.Length % 8 != 0)
                {
                    bits += "0";
                }
                while (bits.Length >= 8) //Escribir bytes en archivo
                {
                    string Byte = bits.Substring(0, 8);
                    bits = bits.Remove(0, 8);

                    ByteArrayToFile(rutaComprimido, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) });
                }
            }

            #endregion

            return(rutaComprimido);
        }