[C#] Aes File Encrypt/Decrypt

Iniciado por fudmario, Noviembre 10, 2016, 09:20:36 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

 En base a la pregunta de un usuario que tenia problemas al cifrar archivos con una esta funcion de cifrado, decidí implementarlo....
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login



Ejemplo de Uso:
Código: csharp

   // ENCRYPT FILE
    Aes_Encrypt("D:\\ISOs\\660mb.iso", "D:\\Encrypted.iso.aes", "testPassword", 256, CipherMode.CFB, Encoding.UTF8);
  // DECRYPT FILE
    Aes_Decrypt("D:\\Encrypted.iso.aes", "D:\\Decrypted.iso", "testPassword", 256, CipherMode.CFB, Encoding.UTF8);




Código: csharp

  public void Aes_Encrypt(string fileInput, string fileOutput, string password, int keySize,
            CipherMode mode = CipherMode.CFB, Encoding enc = null)
        {
            if (enc == null)
                enc = Encoding.Default;
            if (!((keySize == 128) | (keySize == 192) | (keySize == 256)))
                keySize = 256;
            var buffersize = GetBufferSize(new FileInfo(fileInput).Length);
            var salt = GenerateSalt();

            using (var fsOut = new FileStream(fileOutput, FileMode.Create))
            {
                var passBytes = enc.GetBytes(password);
                using (var aes = new RijndaelManaged())
                {
                    var pdb = new Rfc2898DeriveBytes(passBytes, salt, 50000);
                    aes.KeySize = keySize;
                    aes.Padding = PaddingMode.PKCS7;
                    aes.Key = pdb.GetBytes(keySize/8);
                    aes.IV = pdb.GetBytes(16);
                    aes.Mode = mode;

                    fsOut.Write(salt, 0, salt.Length);
                    using (var cs = new CryptoStream(fsOut, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        using (var fsInp = new FileStream(fileInput, FileMode.Open))
                        {
                            var tmpBuffer = new byte[buffersize];
                            var bytesRead = 0;
                            do
                            {
                                bytesRead = fsInp.Read(tmpBuffer, 0, tmpBuffer.Length);
                                cs.Write(tmpBuffer, 0, bytesRead);
                            } while (bytesRead > 0);
                        }
                    }
                }
            }
        }

        public void Aes_Decrypt(string fileinput, string fileoutput, string password, int keySize,
            CipherMode mode = CipherMode.CFB, Encoding enc = null)
        {
            if (enc == null)
                enc = Encoding.UTF8;
            if (!((keySize == 128) | (keySize == 192) | (keySize == 256)))
                keySize = 256;
            var salt = new byte[32];
            var buffersize = GetBufferSize(new FileInfo(fileinput).Length);
            using (var fsIn = new FileStream(fileinput, FileMode.Open))
            {
                fsIn.Read(salt, 0, salt.Length);
                var passBytes = enc.GetBytes(password);
                using (var crypto = new RijndaelManaged())
                {
                    var pdb = new Rfc2898DeriveBytes(passBytes, salt, 50000);

                    crypto.KeySize = keySize;
                    crypto.Padding = PaddingMode.PKCS7;
                    crypto.Key = pdb.GetBytes(keySize/8);
                    crypto.IV = pdb.GetBytes(16);
                    crypto.Mode = mode;


                    using (var cs = new CryptoStream(fsIn, crypto.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        using (var fsOut = new FileStream(fileoutput, FileMode.Create))
                        {
                            var tmpBuffer = new byte[buffersize];
                            var bytesRead = 0;
                            do
                            {
                                bytesRead = cs.Read(tmpBuffer, 0, tmpBuffer.Length);
                                fsOut.Write(tmpBuffer, 0, bytesRead);
                            } while (bytesRead > 0);
                        }
                    }
                }
            }
        }

        private byte[] GenerateSalt()
        {
            var data = new byte[32];
            using (var rng = new RNGCryptoServiceProvider())
            {
                rng.GetBytes(data);
            }
            return data;
        }


        public int GetBufferSize(long filesize)
        {
            return filesize >= 1073741824
                ? 1048576
                : (filesize >= 524288000
                    ? 524288
                    : (filesize >= 104857600
                        ? 131072
                        : (filesize >= 10485760 ? 32768 : (filesize >= 1048576 ? 8192 : 4096))));
        }


Cualquier error o sugerencia respecto al código comentar abajo....xD



Muy bueno tu codigo, lo voy a probar tambien andaba en algo parecido :v jajaja saludos