用C#進行MD5、SHA、DES加密解密

來源:互聯網
上載者:User

以下是用C#進行md5、sha加密以及des加密解密的類,最後有應用舉例。

 

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace LU.Security
{
    public class Util
    {
        // String To Bytes
        public static byte[] StringToBytes(string s)
        {
            return System.Text.Encoding.ASCII.GetBytes(s);
        }

        // Bytes To String
        public static string BytesToString(byte[] b)
        {
            return System.Text.Encoding.ASCII.GetString(b);
        }

        // Bytes To Hex String
        public static string BytesToHexString(byte[] b)
        {
            if (b == null)
                return null;

            StringBuilder sb = new StringBuilder(b.Length * 2);

            for (int i = 0; i < b.Length; i++)
                sb.AppendFormat("{0:x2}", b[i]);

            return sb.ToString();
        }

        // Hex String To Bytes
        public static byte[] HexStringToBytes(string s)
        {
            if (s == null) return null;

            int nLen = s.Length;
            if ((nLen % 2) != 0) // 如果長度為奇數,則忽略最後一個十六位字元
                nLen--;

            byte[] buffer = new byte[nLen / 2];

            for (int i = 0; i < nLen; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);
            }

            return buffer;
        }
    }

 public class Hash
 {
  // MD5(32bit)
  public static string MD5(byte[] b)
  {
   MD5 md5 = new MD5CryptoServiceProvider();
   return Util.BytesToHexString( md5.ComputeHash(b) );
  }
  
  public static string MD5(string szInput)
  {
   return MD5( Util.StringToBytes(szInput) );
  }
  
  // SHA1(160bit)
  public static string SHA1(byte[] b)
  {
   SHA1 sha1 = new SHA1Managed();
   return Util.BytesToHexString( sha1.ComputeHash(b) );
  }
  
  public static string SHA1(string szInput)
  {
   return SHA1( Util.StringToBytes(szInput) );
  }
  
  // SHA256(256bit)
  public static string SHA256(byte[] b)
  {
   SHA256 sha256 = new SHA256Managed();
   return Util.BytesToHexString( sha256.ComputeHash(b) );
  }
  
  public static string SHA256(string szInput)
  {
   return SHA256( Util.StringToBytes(szInput) );
  }
  
  // SHA384(384bit)
  public static string SHA384(byte[] b)
  {
   SHA384 sha384 = new SHA384Managed();
   return Util.BytesToHexString( sha384.ComputeHash(b) );
  }
  
  public static string SHA384(string szInput)
  {
   return SHA384( Util.StringToBytes(szInput) );
  }
  
  // SHA512(512bit)
  public static string SHA512(byte[] b)
  {
   SHA512 sha512 = new SHA512Managed();
   return Util.BytesToHexString( sha512.ComputeHash(b) );
  }
  
  public static string SHA512(string szInput)
  {
   return SHA512( Util.StringToBytes(szInput) );
  }
 }

    public class DES
    {
        private DESCryptoServiceProvider _des;

        public DES()
        {
            _des = new DESCryptoServiceProvider();
        }

        public DES(byte[] key, byte[] iv)
        {
            _des = new DESCryptoServiceProvider();
            _des.Key = key;
            _des.IV = iv;
        }

        public DES(string SerialNumber)
        {
            SHA1 sha1 = new SHA1Managed();
            byte[] buffer = sha1.ComputeHash(Util.StringToBytes(SerialNumber));
            byte[] key = new byte[8];
            byte[] iv = new byte[8];

            Array.Copy(buffer, 0, key, 0, 8);
            Array.Copy(buffer, 8, iv, 0, 8);

            _des = new DESCryptoServiceProvider();
            _des.Key = key;
            _des.IV = iv;
        }

        public byte[] Key
        {
            get { return _des.Key; }
            set { _des.Key = value; }
        }

        public byte[] IV
        {
            get { return _des.IV; }
            set { _des.IV = value; }
        }

        public byte[] Encrypt(byte[] plainText)
        {
            MemoryStream ms = new MemoryStream();

            CryptoStream encStream = new CryptoStream(ms, _des.CreateEncryptor(), CryptoStreamMode.Write);
            encStream.Write(plainText, 0, plainText.Length);
            encStream.Close();

            byte[] buffer = ms.ToArray();
            ms.Close();

            return buffer;
        }

        public string Encrypt(string plainText)
        {
            byte[] cypherText = this.Encrypt(Util.StringToBytes(plainText));
            return Util.BytesToHexString(cypherText);
        }

        public byte[] Decrypt(byte[] cypherText)
        {
            MemoryStream ms = new MemoryStream(cypherText);
            CryptoStream encStream = new CryptoStream(ms, _des.CreateDecryptor(), CryptoStreamMode.Read);

            int b;
            MemoryStream tmpStream = new MemoryStream();
            while ((b = encStream.ReadByte()) != -1)
            {
                tmpStream.WriteByte((byte)b);
            }

            encStream.Close();
            ms.Close();

            byte[] buffer = tmpStream.ToArray();
            tmpStream.Close();

            return buffer;
        }

        public string Decrypt(string cypherText)
        {
            byte[] plainText = this.Decrypt(Util.HexStringToBytes(cypherText));
            return Util.BytesToString(plainText);
        }
    }
}

 

 

應用舉例:

 

// using LU.Security;  // 不要忘記引用命名空間

……

// md5
string pwd1 = Hash.MD5("12345678");

// sha1
string pwd2 = Hash.SHA1("12345678");

// des
/* 下面引號中的字串 WeR24^_weFangO!7X2*99 是密鑰,可自己設定,但應妥善儲存,否則所有密碼會失效,且無法解密 */
DES des = new DES("WeR24^_weFangO!7X2*99");
string pwd3 = des.Encrpt("12345678");
string plainText = des.Decrypt(pwd3); // 應該是12345678

……

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.