c# 对用户密码加密解密
一.使用16位、32位、64位MD5方法对用户名加密
1)16位的MD5加密
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/// <summary>/// 16位MD5加密/// </summary>/// <param name="password"></param>/// <returns></returns>public static string MD5Encrypt16(string password){ var md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8); t2 = t2.Replace("-", ""); return t2;} |
2)32位的MD5加密
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/// <summary>/// 32位MD5加密/// </summary>/// <param name="password"></param>/// <returns></returns>public static string MD5Encrypt32(string password){ string cl = password; string pwd = ""; MD5 md5 = MD5.Create(); //实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 for (int i = 0; i < s.Length; i++) { // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 pwd = pwd + s[i].ToString("X"); } return pwd;} |
3)64位的MD5加密
|
1
2
3
4
5
6
7
8
9
|
public static string MD5Encrypt64(string password){ string cl = password; //string pwd = ""; MD5 md5 = MD5.Create(); //实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); return Convert.ToBase64String(s);} |
4)使用MD5为用户密码加密
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/// <summary>/// 加密用户密码/// </summary>/// <param name="password">密码</param>/// <param name="codeLength">加密位数</param>/// <returns>加密密码</returns>public static string md5(string password, int codeLength){ if (!string.IsNullOrEmpty(password)) { // 16位MD5加密(取32位加密的9~25字符) if (codeLength == 16) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower().Substring(8, 16); } // 32位加密 if (codeLength == 32) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower(); } } return string.Empty;} |
由于MD5是不可逆的,所以加密之后就无法解密,取用户名和密码时候,需要再加密一边用户输入的数据与数据库中已加密的数据进行比对。如果比对结果一致,则可以判定登陆成功!代码如下所示:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/// <summary>/// 登陆/// </summary>public Model.UserInfo UserLogOn(string USERID, string pwd, out string statusCode){ //假设已经通过用户ID获取到UserInfo的Model对象 Model.UserInfo model = GetModel(USERID); if (model != null) { if (model.PASSWORD == MD5Encrypt64(pwd)) { statusCode = "登陆成功"; } else { statusCode = “密码错误”; } } else { statusCode = "用户不存在!"; model = null; } return model;} |
5)通过DESCryptoServiceProvider对象对字符串进行加密解密
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/// <summary>/// DES数据加密/// </summary>/// <param name="targetValue">目标值</param>/// <param name="key">密钥</param>/// <returns>加密值</returns>public static string Encrypt(string targetValue, string key){ if (string.IsNullOrEmpty(targetValue)) { return string.Empty; } var returnValue = new StringBuilder(); var des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(targetValue); // 通过两次哈希密码设置对称算法的初始化向量 des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5"). Substring(0, 8), "sha1").Substring(0, 8)); // 通过两次哈希密码设置算法的机密密钥 des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5") .Substring(0, 8), "md5").Substring(0, 8)); var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); foreach (byte b in ms.ToArray()) { returnValue.AppendFormat("{0:X2}", b); } return returnValue.ToString();} |
此种算法可以通过加密密钥进行解密,解密方法如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/// <summary>/// DES数据解密/// </summary>/// <param name="targetValue"></param>/// <param name="key"></param>/// <returns></returns>public static string Decrypt(string targetValue, string key){ if (string.IsNullOrEmpty(targetValue)) { return string.Empty; } // 定义DES加密对象 var des = new DESCryptoServiceProvider(); int len = targetValue.Length / 2; var inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(targetValue.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } // 通过两次哈希密码设置对称算法的初始化向量 des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5"). Substring(0, 8), "sha1").Substring(0, 8)); // 通过两次哈希密码设置算法的机密密钥 des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5") .Substring(0, 8), "md5").Substring(0, 8)); // 定义内存流 var ms = new MemoryStream(); // 定义加密流 var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray());} |
以上内容是基于C#对用户密码使用MD5加密与解密的全部叙述,希望大家喜欢。
c# 对用户密码加密解密的更多相关文章
- Openfire用户密码加密解密
需求要求审核过程中都用匿名进行用户注册登录,注册用户审核通过后才使用openfire内置表 如何做到用户密码统一 Openfire是通过org.jivesoftware.util.Blowfish.j ...
- openfire 用户密码加密解密
1.openfire采用的加密方法 Blowfish.java /** * $RCSfile$ * $Revision: 3657 $ * $Date: 2002-09-09 08:31:31 -07 ...
- C#:使用MD5对用户密码加密与解密
C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...
- 转 C#:使用MD5对用户密码加密与解密
C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...
- Maven-009-Nexus 用户密码加密(安全必须)
信息数据大爆发的时代,我们关心什么?没错,数据安全!数据安全!数据安全!(重要事情说三遍,哈哈哈...) 之前我们存放在 maven settings.xml 文件中的 Nexus 私服用户密码都是明 ...
- Cognos权限认证CJP方式之用户密码加密
在项目开发过程中,用户往往对系统的安全都有明确的要求,下面针对cognos门户认证用户密码如何加密来提供一个简单的wf 1Cognos权限认证方式:CJP 2Cognos用户数据库类型:Oracle ...
- 使用bcrypt进行用户密码加密的简单实现
Bcrypt百度百科: bcrypt,是一个跨平台的文件加密工具.由它加密的文件可在所有支持的操作系统和处理器上进行转移.它的口令必须是8至56个字符,并将在内部被转化为448位的密钥. 除了对您的数 ...
- Druid 数据库用户密码加密 代码实现
druid-1.0.16.jar 阿里巴巴的开源数据连接池 jar包 明文密码+私钥(privateKey)加密=加密密码 加密密码+公钥(publicKey)解密=明文密码 程序代码如下: pack ...
- php提供的用户密码加密函数
在实际项目中,对用户的密码加密基本上采用的 md5加盐的方式, php5.5后提供了一个加密函数,不需要手动加盐,不需要去维护盐值, $str = "123456"; $pwd ...
随机推荐
- 省赛i题/求1~n内全部数对(x,y),满足最大公约数是质数的对数
求1~n内全部数对(x,y),gcd(x,y)=质数,的对数. 思路:用f[n]求出,含n的对数.最后用sum[n]求和. 对于gcd(x,y)=a(设x<=y,a是质数),则必有gcd(x/a ...
- ICMP 隧道——将流量封装进 IMCP 的 ping 数据包中,旨在利用 ping 穿透防火墙的检测
利用 ICMP 隧道穿透防火墙 转自:http://xiaix.me/li-yong-icmp-sui-dao-chuan-tou-fang-huo-qiang/ 以前穿透防火墙总是使用 SSH 隧道 ...
- 访问Storm ui界面,出现org.apache.storm.utils.NimbusLeaderNotFoundException: Could not find leader nimbus from seed hosts ["master"]. Did you specify a valid list of nimbus hosts for confi的问题解决(图文详解)
不多说,直接上干货! 前期博客 apache-storm-0.9.6.tar.gz的集群搭建(3节点)(图文详解) apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)( ...
- Win7 利用批处理文件结束进程
@echo offtitle 结束进程正在进行... ::结束进程TeamViewer.exewmic process where name="TeamViewer.exe" ca ...
- 编程语言与Python学习(二)
1.1 流程控制之for循环 1 迭代式循环:for,语法如下 for i in range(10): 缩进的代码块 2 break与continue(同上) 3 循环嵌套 for i in rang ...
- 你不知道的JavaScript(十)with关键字
with关键字在JavaScript中不太常用,用来定义一个和对象相关的作用域,在该作用域中可以访问对象的属性或方法而前面无需加上对象名,以达到简化代码的目的. <script type=&qu ...
- Enable .Net 4.5 in IIS on Windows 8.1
Setting up a new development box for myself I had forgotten all about the necessity to use theaspnet ...
- 【原创】Unable to read TLD "META-INF/c.tld" from JAR file 解决方法
type Exception report message description The server encountered an internal error () that prevented ...
- HDU 1087 Super Jumping! Jumping! Jumping!【DP】
解题思路:题目的大意是给出一列数,求这列数里面最长递增数列的和 dp[i]表示到达地点i的最大值,那么是如何达到i的呢,则我们可以考虑没有限制条件时候的跳跃,即可以从第1,2,3,---,i-1个地点 ...
- css文字超出变省略号...
<style>.text1 { width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-over ...