文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>asp.net 加密

asp.net 加密

时间:2011-02-22  来源:双魂人生

 MD5:(不可逆的加密)

我们可以使用C#自带的类MD5CryptoServiceProvider去实现

 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

string 加密后的密码=BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(原密码))).Replace("-","");

或者

 public string Encrypt1(string password)

{ //获取byte数组

Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);

 //获取hash值

 Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);

//获取加密后的信息

return BitConverter.ToString(hashedBytes);

}

DES:(可逆的加密)

加密

string key="加密钥匙";

public string Encrypt(string encryptString)

{

byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

byte[] keyIV = keyBytes;

 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();

 CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor (keyBytes,keyIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

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

}

 解密:

 public string Decrypt(string decryptString)

 {

byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

byte[] keyIV = keyBytes;

byte[] inputByteArray = Convert.FromBase64String(decryptString);

DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

MemoryStream mStream = new MemoryStream();

 CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

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

 }

网上的加密方式很多,最好结合自己修改的,要不同种加密算法,别人同样也会知道...

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载