DES Decrypt String with string key - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

DES Decrypt String with string key

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.IO;/*ww w  . j a va2  s .  c  o  m*/
using System.Collections.Generic;
using System;

public class Main{
      public static string Decrypt(string decryptString, string decryptKey)
      {
         try
         {
            var rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            var rgbIv = CryptKeys.DesKeys;
            var inputByteArray = Convert.FromBase64String(decryptString);
            var dcsp = new DESCryptoServiceProvider();
            var mStream = new MemoryStream();
            var cStream = new CryptoStream(mStream, dcsp.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
         }
         catch
         {
            return decryptString;
         }
      }
}

Related Tutorials