DES Decrypt Data - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

DES Decrypt Data

Demo Code


using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.IO;/*from w  w w. ja  v  a2  s.  c om*/
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static void DecryptData(Stream fin, Stream fout,
            Byte[] desKey, Byte[] desIV)
        {
            //Create variables to help with read and write.
            Byte[] bin = new Byte[4096];
            long rdlen = 0;
            long totlen = fin.Length;
            int len;
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            CryptoStream decStream = new CryptoStream(fout,des.CreateDecryptor(desKey,desIV),CryptoStreamMode.Write);

            //Read from the input file, then encrypt and write to the output file
            while (rdlen < totlen)
            {
                len = fin.Read(bin,0,4096);
                decStream.Write(bin,0,len);
                rdlen = Convert.ToInt32(rdlen + len / des.BlockSize * des.BlockSize);
            }

            decStream.FlushFinalBlock();
        }
}

Related Tutorials