码迷,mamicode.com
首页 > 编程语言 > 详细

c#进行MD5加密方式和解密算法

时间:2017-11-06 16:19:21      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:byte   int   toe   字符   bsp   creat   inpu   substring   ted   

 

 

 

--------------- 因为加密个解密都需要用到key所有在加密的后需要把key和加密码都存到数据库中

 

/// <summary>
/// 唯一加密方式
/// </summary>
/// <param name="texts"></param>
/// <returns></returns>
public static string WeiJiaMiGuid(string texts)
{
string Keys = GenerateKey();
return MD5Encrypt(texts, Keys) + "=" + Keys;      //这里我把要加密的字符串和生成的key给拼接起来,这样我在调用 WeiJiaMiGuid方法是只需要传文本框text值就可以了;
}

 

------------------------取出加密时存在数据库的加密码和key  


/// <summary>
/// 唯一解密方式
/// </summary>
/// <param name="texts"></param>
/// <returns></returns>
public static string WeiYiJieMiGuid(string texts)
{
string[] pwa = texts.Split(new char[] { ‘=‘ });   //分割一下    然后调解密
return GXC.Commonality.CommGUID.MD5Decrypt(pwa[0], pwa[1]);

}

 

/// <summary>
/// 创建Key
/// </summary>
/// <returns></returns>
public static string GenerateKey()
{
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}

 

 

/// <summary>
/// MD5加密
/// </summary>
/// <param name="pToEncrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string MD5Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}

/// <summary>
/// MD5解密
/// </summary>
/// <param name="pToDecrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string MD5Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}

des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

StringBuilder ret = new StringBuilder();

return System.Text.Encoding.Default.GetString(ms.ToArray());
}

 

复制就可以用  无需改动 16位加密方式

c#进行MD5加密方式和解密算法

标签:byte   int   toe   字符   bsp   creat   inpu   substring   ted   

原文地址:http://www.cnblogs.com/pang572936554/p/7793665.html

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