码迷,mamicode.com
首页 > 其他好文 > 详细

5.encryptHelper类

时间:2018-05-31 19:30:06      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:向量   rms   provider   nconf   字符   一个   tor   decrypt   解密   

//把一个字符串转换成对应的MD5
public static string toMD5(string str)
{
  return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToLower();
}

public static string _KEY = "wzr17021"; //密钥
public static string _IV = "wzr17022"; //向量

/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string DES加密(string data)
{
  byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY);
  byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV);

  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  int i = cryptoProvider.KeySize;
  MemoryStream ms = new MemoryStream();
  CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

  StreamWriter sw = new StreamWriter(cst);
  sw.Write(data);
  sw.Flush();
  cst.FlushFinalBlock();
  sw.Flush();

  string strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
  return strRet;
}

/// <summary>
/// 解密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string DES解密(string data)
{

  byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY);
  byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV);

  byte[] byEnc;

  try
  {
    data.Replace("_%_", "/");
    data.Replace("-%-", "#");
    byEnc = Convert.FromBase64String(data);

  }
  catch
  {
    return null;
  }

  DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  MemoryStream ms = new MemoryStream(byEnc);
  CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
  StreamReader sr = new StreamReader(cst);
  return sr.ReadToEnd();
}

5.encryptHelper类

标签:向量   rms   provider   nconf   字符   一个   tor   decrypt   解密   

原文地址:https://www.cnblogs.com/feihusurfer/p/9117744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!