Decrypt input string by DES algorithm. - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

Decrypt input string by DES algorithm.

Demo Code

/***************************************************************************
  Copyright (C) 2010 Eunge (Email: eunge.liu@gmail.com, Legal Name: Jian Liu)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.//from  w ww .  j a  v a  2 s  .c  o  m

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System;

public class Main{
        /// <summary>
      /// Decrypt input string by DES algorithm.
      /// </summary>
      /// <param name="inputString">Input string is for decryption.</param>
      /// <returns>Decrypted string.</returns>
      public static string DESDecrypt(string inputString)
      {
         MemoryStream ms = null;
         CryptoStream cs = null;
         StreamReader sr = null;

         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         try
         {
            ms = new MemoryStream(Convert.FromBase64String(inputString));
            cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
            sr = new StreamReader(cs);
            return sr.ReadToEnd();
         }
         finally
         {
            if (sr != null)
            {
               sr.Close();
            }

            if (cs != null)
            {
               cs.Close();
            }

            if (ms != null)
            {
               ms.Close();
            }
         }
      }
}

Related Tutorials