DES Decrypt File - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

DES Decrypt File

Demo Code


using System.Security.Cryptography;
using System.IO;/*from   ww  w. j  a  v a2 s  . co  m*/
using System.Text;
using System;

public class Main{
        private static DESCryptoServiceProvider desCSP = new DESCryptoServiceProvider();
        public void Decrypt(string inputFile, string outputFile)
        {
            byte[] Key = Encoding.UTF8.GetBytes(strKey);
            byte[] IV = Encoding.UTF8.GetBytes(strIV);

            FileStream fin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);

            ICryptoTransform ct = desCSP.CreateDecryptor(Key, IV);
            CryptoStream cs = new CryptoStream(fout, ct, CryptoStreamMode.Write);

            byte[] bin = new byte[100];
            long rdlen = 0;
            long totlen = fin.Length;
            int len;
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 100);
                cs.Write(bin, 0, len);
                rdlen = rdlen + len;
            }
            cs.Close();
            fout.Close();
            fin.Close();   
        }
        public static string Decrypt(string input)
        {
            byte[] Key = Encoding.UTF8.GetBytes(strKey);
            byte[] IV = Encoding.UTF8.GetBytes(strIV);
            byte[] byt = Convert.FromBase64String(input); 
            ICryptoTransform ct = desCSP.CreateDecryptor(Key, IV);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Encoding.UTF8.GetString(ms.ToArray());
        }
}

Related Tutorials