2015年4月18日 星期六

[C#] 3DES解密

開發上用過的Method,紀錄在此。

  /// <summary>  
  /// 解密  
  /// </summary>  
  /// <param name="encrypt">要解密的字串</param>  
  /// <param name="key">KEY</param>  
  /// <param name="IV">IV</param>  
  /// <returns>解密後的字串</returns>  
  private static string desDecryptBase64(string encrypt, string key, string IV)  
  {  
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
    byte[] key = Encoding.ASCII.GetBytes(key);  
    byte[] iv = Encoding.ASCII.GetBytes(IV);  
    des.Key = key;  
    des.IV = iv;  
    byte[] dataByteArray = Convert.FromBase64String(encrypt);  
    using (MemoryStream ms = new MemoryStream())  
    {  
      using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))  
      {  
        cs.Write(dataByteArray, 0, dataByteArray.Length);  
        cs.FlushFinalBlock();  
        return Encoding.UTF8.GetString(ms.ToArray());  
      }  
    }  
  }  

沒有留言:

張貼留言