5.encryptHelper类

Posted feihusurfer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.encryptHelper类相关的知识,希望对你有一定的参考价值。

//把一个字符串转换成对应的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类的主要内容,如果未能解决你的问题,请参考以下文章

CodeIgniter 加密类到 java

php加密类

java AES 加密解密工具(Advanced Encryption Standard)

PHP版DES算法加密数据(3DES)另附openssl_encrypt版本

mcrypt_encrypt 到 openssl_encrypt 和 OPENSSL_ZERO_PADDING 问题

在 PHP 中将 mcrypt_encrypt 转换为 openssl_encrypt