DES Decrypt string - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

DES Decrypt string

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.IO;/*from   w  w  w  .j a  va  2 s.  c  om*/
using System;

public class Main{
        public static string Decrypt(string input)
        {
            var des = new DESCryptoServiceProvider();

            var inputByteArray = new byte[input.Length / 2];
            for (int x = 0; x < input.Length / 2; x++)
            {
                int i = (Convert.ToInt32(input.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = Encoding.ASCII.GetBytes(SKey); 
            des.IV = Encoding.ASCII.GetBytes(SKey);
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }
}

Related Tutorials