Encrypt File - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:Encrypt Decrypt

Description

Encrypt File

Demo Code


using System.Data.SqlClient;
using System.Data;
using System.Security.Cryptography;
using System.Configuration;
using System.Xml;
using System.Reflection;
using System.IO;//from   w w w.  jav  a  2 s .co m
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        public static void EncryptFile(Stream inputData, string sOutputFilename)
        {
            SetKeys();
            
            FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);

            ICryptoTransform desencrypt = des.CreateEncryptor(key, IV);
            CryptoStream cryptostreamEncrypter = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[inputData.Length];

            inputData.Read(bytearrayinput, 0, bytearrayinput.Length);

            cryptostreamEncrypter.Write(bytearrayinput, 0, bytearrayinput.Length);
            
            cryptostreamEncrypter.Close();
            inputData.Close();
            fsEncrypted.Close();
        }
        public static void EncryptFile(string sInputFilename, string sOutputFilename)
        {
            SetKeys();

            FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
            FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);

            ICryptoTransform desencrypt = des.CreateEncryptor(key, IV);
            CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length];

            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);

            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();           
        } // static void EncryptFile(...)
        /// <summary>
        /// Aun presenta bugs: No utilizar
        /// </summary>
        /// <param name="sInputFilename"></param>
        /// <returns></returns>
        public static MemoryStream EncryptFile(string sInputFilename)
        {
            SetKeys();

            FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
            MemoryStream temp = new MemoryStream();
            MemoryStream output = new MemoryStream();

            ICryptoTransform desencrypt = des.CreateEncryptor(key, IV);
            CryptoStream cryptostream = new CryptoStream(temp, desencrypt, CryptoStreamMode.Write);
           
            byte[] bytearrayinput = new byte[fsInput.Length];

            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);

            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

            //Imprimir el contenido del archivo descifrado.             
            temp.WriteTo(output);

            //Cerrar los streams
            cryptostream.Close();
            fsInput.Close();

            return output;
        } // static MemoryStream EncryptFile(...)
}

Related Tutorials