none
Encriptar y desencriptar archivos ? RRS feed

  • Pregunta

  • hola...

    Nesecito desarrollar una Aplicacion que muestra contenido ( .pdf . .txt . doc y video) y nesecitos q estos archivos esten encriptados ...como puedo hacerlo...un tutorial o codigo de ejemplo seria excelente ???


    EFRAIN MEJIAS C VALENCIA - VENEZUELA

    viernes, 19 de mayo de 2017 21:03

Respuestas

  • Hola, puedes probar con lo siguiente:

    Para encriptar:

    private static void EncryptFile(string inputFile, string outputFile, string skey)
    {
        try
        {
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
     
                /* This is for demostrating purposes only. 
                 * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
     
                using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
                {
                    using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
                    {
                        using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
                            {
                                int data;
                                while ((data = fsIn.ReadByte()) != -1)
                                {
                                    cs.WriteByte((byte)data);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // failed to encrypt file
        }
    }

    Para desencriptar:

    private static void DecryptFile(string inputFile, string outputFile, string skey)
    {
        try
        {
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
     
                /* This is for demostrating purposes only. 
                 * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
     
                using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
                {
                    using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
                    {
                        using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
                        {
                            using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
                            {
                                int data;
                                while ((data = cs.ReadByte()) != -1)
                                {
                                    fsOut.WriteByte((byte)data);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // failed to decrypt file
        }
    }

    Me comentas si fue de tu ayuda.

    viernes, 19 de mayo de 2017 21:39

Todas las respuestas

  • Hola, puedes probar con lo siguiente:

    Para encriptar:

    private static void EncryptFile(string inputFile, string outputFile, string skey)
    {
        try
        {
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
     
                /* This is for demostrating purposes only. 
                 * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
     
                using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
                {
                    using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
                    {
                        using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
                            {
                                int data;
                                while ((data = fsIn.ReadByte()) != -1)
                                {
                                    cs.WriteByte((byte)data);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // failed to encrypt file
        }
    }

    Para desencriptar:

    private static void DecryptFile(string inputFile, string outputFile, string skey)
    {
        try
        {
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
     
                /* This is for demostrating purposes only. 
                 * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
     
                using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
                {
                    using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
                    {
                        using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
                        {
                            using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
                            {
                                int data;
                                while ((data = cs.ReadByte()) != -1)
                                {
                                    fsOut.WriteByte((byte)data);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // failed to decrypt file
        }
    }

    Me comentas si fue de tu ayuda.

    viernes, 19 de mayo de 2017 21:39
  • hola

    >>Nesecito desarrollar una Aplicacion que muestra contenido ( .pdf . .txt . doc y video)

    pero mostrarlos donde ?

    >>nesecitos q estos archivos esten encriptados

    pero para mostrarlos no van a poder seguir encriptados, puedes aplicar seguridad cuando los persistes, pero para poder verlos vas a tener que desencriptarlos

    la pregunta seria donde persistes estos documentos? es en una carpeta o en una db

    saludos


    Leandro Tuttini

    Blog
    MVP Profile
    Buenos Aires
    Argentina

    sábado, 20 de mayo de 2017 5:43
  • Hola Leandro ...

    Nesecito desarrollar una Aplicacion que muestra contenido ( .pdf . .txt . doc y video)

    Mostrarlo dentro de la misma aplicacion

    Nesecitos q estos archivos esten encriptados

    Los Archivos van a estar en una carpeta ...en un CD


    EFRAIN MEJIAS C VALENCIA - VENEZUELA

    sábado, 20 de mayo de 2017 14:42
  • Si manejas SQL Server 2016, yo te recomiendo la encriptacion SHA

    un ejemplo, puedes hacerlo asi

    Select HASHBYTES('SHA2_512','Prueba');

    Es una encriptacion mas fuerte que esta en estos momentos, pero OJO, no podras desencriptar la informacion, esto solo se hace para comprar encriptacion con encriptacion, pero aumentas al 100% la seguridad de los datos.

    sábado, 20 de mayo de 2017 17:09
  •   using System;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Security.Cryptography;

    namespace EnDes_Criptacion
    {
        public partial class EnDesencriptar : Form
        {
            string filePath = Application.StartupPath + "\\Contenido\\Secuencia.mp4";
            string input = string.Empty;
            string key = "endescri";

            public EnDesencriptar()
            {
                InitializeComponent();
            }

            private void Encriptar_Click(object sender, EventArgs e)
            {
                try
                {
                    using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider())
                    {
                        Byte[] plainContent = File.ReadAllBytes(filePath);
                        DES.IV = Encoding.UTF8.GetBytes(key);
                        DES.Key = Encoding.UTF8.GetBytes(key);
                        DES.Mode = CipherMode.CBC;
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            CryptoStream cryptoStream = new CryptoStream(memStream, DES.CreateEncryptor(), CryptoStreamMode.Write);
                            cryptoStream.Write(plainContent, 0, plainContent.Length);
                            cryptoStream.FlushFinalBlock();
                            File.WriteAllBytes(filePath, memStream.ToArray());
                        }
                    }
                     MessageBox.Show("Encriptacion Satisfactoria");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Encriptacion Fallida");
                }
            }
            private void Desencriptar_Click(object sender, EventArgs e)
            {
                try
                {
                    using (DESCryptoServiceProvider DES = new DESCryptoServiceProvider())
                    {
                        Byte[] plainContent = File.ReadAllBytes(filePath);
                        DES.IV = Encoding.UTF8.GetBytes(key);
                        DES.Key = Encoding.UTF8.GetBytes(key);
                        DES.Mode = CipherMode.CBC;
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            CryptoStream cryptoStream = new CryptoStream(memStream, DES.CreateDecryptor(), CryptoStreamMode.Write);
                            cryptoStream.Write(plainContent, 0, plainContent.Length);
                            cryptoStream.FlushFinalBlock();
                            File.WriteAllBytes(filePath, memStream.ToArray());
                        }
                    }
                    MessageBox.Show("Desencriptacion Satisfactoria");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Desencriptacion Fallida");
                }
            }
        }
    }



    EFRAIN MEJIAS C VALENCIA - VENEZUELA


    lunes, 22 de mayo de 2017 9:03