.Net 常用密碼編譯演算法類

來源:互聯網
上載者:User
加密|演算法  

.Net架構由於擁有CLR提供的豐富庫支援,只需很少的代碼即可實現先前使用C等舊式語言很難實現的密碼編譯演算法。本類實現一些常用機密演算法,供參考。其中MD5演算法返回Int的ToString字串。返回數字字母型結果的演算法參見之前Blog文章。

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

namespace com.Quickline.Encrypt
{
 /// <summary>
 /// 類名:HashEncrypt
 /// 作用:對傳入的字串進行Hash運算,返回通過Hash演算法加密過的字串。
 /// 屬性:[無]
 /// 建構函式額參數:
 /// IsReturnNum:是否返回為加密後字元的Byte代碼
 /// IsCaseSensitive:是否區分大小寫。
 /// 方法:此類提供MD5,SHA1,SHA256,SHA512等四種演算法,加密字串的長
度依次增大。
 /// </summary>
 public class HashEncrypt
 {
  //private string strIN;
  private bool isReturnNum;
  private bool isCaseSensitive;
  
  public HashEncrypt(bool IsCaseSensitive,bool IsReturnNum)
  {
   this.isReturnNum = IsReturnNum;
   this.isCaseSensitive = IsCaseSensitive;
  }
  
  
  private string getstrIN(string strIN)
  {
   //string strIN = strIN;
   if (strIN.Length == 0)
   {
    strIN = "~NULL~";
   }
   if (isCaseSensitive == false)
   {
    strIN = strIN.ToUpper();
   }
   return strIN;
  }
  public string MD5Encrypt(string strIN)
  {
   //string strIN = getstrIN(strIN);
   byte[] tmpByte;
   MD5 md5 = new MD5CryptoServiceProvider();
   tmpByte =
md5.ComputeHash(GetKeyByteArray(getstrIN(strIN)));
   md5.Clear();

   return GetStringValue(tmpByte);

  }
  
  public string SHA1Encrypt(string strIN)
  {
   //string strIN = getstrIN(strIN);
   byte[] tmpByte;
   SHA1 sha1 = new SHA1CryptoServiceProvider();

   tmpByte = sha1.ComputeHash(GetKeyByteArray(strIN));
   sha1.Clear();

   return GetStringValue(tmpByte);

  }

  public string SHA256Encrypt(string strIN)
  {
   //string strIN = getstrIN(strIN);
   byte[] tmpByte;
   SHA256 sha256 = new SHA256Managed();

   tmpByte =
sha256.ComputeHash(GetKeyByteArray(strIN));
   sha256.Clear();

   return GetStringValue(tmpByte);

  }

  public string SHA512Encrypt(string strIN)
  {
   //string strIN = getstrIN(strIN);
   byte[] tmpByte;
   SHA512 sha512 = new SHA512Managed();

   tmpByte =
sha512.ComputeHash(GetKeyByteArray(strIN));
   sha512.Clear();

   return GetStringValue(tmpByte);

  }
  
  /// <summary>
  /// 使用DES加密(Added by niehl 2005-4-6)
  /// </summary>
  /// <param name="originalValue">待加密的字串</param>
  /// <param name="key">密鑰(最大長度8)</param>
  /// <param name="IV">初始化向量(最大長度8)</param>
  /// <returns>加密後的字串</returns>
  public string DESEncrypt(string originalValue,string key,string IV)
  {
   //將key和IV處理成8個字元
   key += "12345678";
   IV += "12345678";
   key = key.Substring(0,8);
   IV = IV.Substring(0,8);

   SymmetricAlgorithm sa;
   ICryptoTransform ct;
   MemoryStream ms;
   CryptoStream cs;
   byte[] byt;

   sa = new DESCryptoServiceProvider();
   sa.Key = Encoding.UTF8.GetBytes(key);
   sa.IV = Encoding.UTF8.GetBytes(IV);
   ct = sa.CreateEncryptor();

   byt = Encoding.UTF8.GetBytes(originalValue);

   ms = new MemoryStream();
   cs = new CryptoStream(ms, ct,
CryptoStreamMode.Write);
   cs.Write(byt, 0, byt.Length);
   cs.FlushFinalBlock();

   cs.Close();

   return Convert.ToBase64String(ms.ToArray());

  }

  public string DESEncrypt(string originalValue,string key)
  {
   return DESEncrypt(originalValue,key,key);
  }

  /// <summary>
  /// 使用DES解密(Added by niehl 2005-4-6)
  /// </summary>
  /// <param name="encryptedValue">待解密的字串</param>
  /// <param name="key">密鑰(最大長度8)</param>
  /// <param name="IV">m初始化向量(最大長度8)</param>
  /// <returns>解密後的字串</returns>
  public string DESDecrypt(string encryptedValue,string key,string IV)
  {
   //將key和IV處理成8個字元
   key += "12345678";
   IV += "12345678";
   key = key.Substring(0,8);
   IV = IV.Substring(0,8);

   SymmetricAlgorithm sa;
   ICryptoTransform ct;
   MemoryStream ms;
   CryptoStream cs;
   byte[] byt;

   sa = new DESCryptoServiceProvider();
   sa.Key = Encoding.UTF8.GetBytes(key);
   sa.IV = Encoding.UTF8.GetBytes(IV);
   ct = sa.CreateDecryptor();

   byt = Convert.FromBase64String(encryptedValue);

   ms = new MemoryStream();
   cs = new CryptoStream(ms, ct,
CryptoStreamMode.Write);
   cs.Write(byt, 0, byt.Length);
   cs.FlushFinalBlock();

   cs.Close();

   return Encoding.UTF8.GetString(ms.ToArray());

  }

  public string DESDecrypt(string encryptedValue,string key)
  {
   return DESDecrypt(encryptedValue,key,key);
  }

  private string GetStringValue(byte[] Byte)
  {
   string tmpString = "";

   if (this.isReturnNum == false)
   {
    ASCIIEncoding Asc = new ASCIIEncoding();
    tmpString = Asc.GetString(Byte);
   }
   else
   {
    int iCounter;

    for
(iCounter=0;iCounter<Byte.Length;iCounter++)
    {
     tmpString = tmpString +
Byte[iCounter].ToString();
    }
    
   }
   
   return tmpString;
  }

  private byte[] GetKeyByteArray(string strKey)
  {

   ASCIIEncoding Asc = new ASCIIEncoding();
   
   int tmpStrLen = strKey.Length;
   byte[] tmpByte = new byte[tmpStrLen-1];

   tmpByte = Asc.GetBytes(strKey);

   return tmpByte;

  }

 }
}



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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