DES Decrypt password - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:Password

Description

DES Decrypt password

Demo Code


using System.Security.Cryptography;
using System.IO;/*from w  ww . j  ava 2 s . c o m*/
using System.Text;
using System.Collections.Generic;
using System;

public class Main{
   public static string Decrypt(string password, string ciphertext)
   {
      string password2 = "lzxinzhi168";
      
      string cipher = string.Empty;
      
      try
      {
         char[] key = new char[8];
         if (password.Length > 8)
         {
            password = password.Remove(8);
         }
         password.CopyTo(0, key, 0, password.Length);
         
         char[] iv = new char[8];
         if (password2.Length > 8)
         {
            password2 = password2.Remove(8);
         }
         password2.CopyTo(0, iv, 0, password2.Length);
         
         if (ciphertext == null)
         {
            return cipher;
         }
         
         SymmetricAlgorithm serviceProvider = new DESCryptoServiceProvider();
         serviceProvider.Key = Encoding.ASCII.GetBytes(key);
         serviceProvider.IV = Encoding.ASCII.GetBytes(iv);
         
         byte[] contentArray = Convert.FromBase64String(ciphertext);
         MemoryStream memoryStream = new MemoryStream(contentArray);
         CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider.CreateDecryptor(), CryptoStreamMode.Read);
         StreamReader streamReader = new StreamReader(cryptoStream);
         
         cipher = streamReader.ReadToEnd();
         
         streamReader.Dispose();
         cryptoStream.Dispose();
         memoryStream.Dispose();
         serviceProvider.Clear();
         
      }
      catch (Exception ex)
      {
         throw new SystemException("exception");
      }
      
      return cipher;
   }
}

Related Tutorials