十七周开始考试,不急,到时候再看要考试的内容。
大三了,快大四了,找工作了,先找个单位实践实践,不为钱,只想学点东西!
十六周计划,学习:
ASP.NET2.0自定义配置学习。
学习有关.NET2.0的加密解密类。
学习PetShop4.0。
C#异常复习。
本想在这个周末,重写完全支持ACCESS的成员资格与角色管理提供程序,在重写MembershipProvider的时候,遇到了有关密码加密的东东,以前也用到过,只是从网上下一个算法,现在想好好研究这个东西,今天花一整天的时间学习有关知识。
首选是有关加密解密的概要,什么是对称加密,什么是非对称加密,哈希是怎么回事,这些应该是要清楚的。还要了解什么是数字签名。
最重要的是会应用.NET2.0提供的基类,写一些加密解密函数。学习了SHA1,MD5和3DES的算法。具体的源代码如下:
/Files/JBoy/encrypt.rar
下面是3DES对称加密解密:
MD5加密算法:
byte[] bt_str = System.Text.Encoding.Unicode.GetBytes(str); //将一组字符编码为字节序列
//MD5 md5Hasher = MD5.Create(); //创建一个 MD5CryptoServiceProvider 实例
//MD5 md5Hasher = (MD5CryptoServiceProvider)HashAlgorithm.Create("MD5");
HashAlgorithm md5Hasher = HashAlgorithm.Create("MD5");
byte[] hashed_str = md5Hasher.ComputeHash(bt_str); //计算指定字节数组的哈希
StringBuilder result = new StringBuilder();
foreach (byte b in hashed_str)
{
result.Append(b.ToString("x2")); //十六进制格式
//lblResult.Text += b.ToString();
}
明天继续学习,要把它彻底征服了!
大三了,快大四了,找工作了,先找个单位实践实践,不为钱,只想学点东西!
十六周计划,学习:
ASP.NET2.0自定义配置学习。
学习有关.NET2.0的加密解密类。
学习PetShop4.0。
C#异常复习。
本想在这个周末,重写完全支持ACCESS的成员资格与角色管理提供程序,在重写MembershipProvider的时候,遇到了有关密码加密的东东,以前也用到过,只是从网上下一个算法,现在想好好研究这个东西,今天花一整天的时间学习有关知识。
首选是有关加密解密的概要,什么是对称加密,什么是非对称加密,哈希是怎么回事,这些应该是要清楚的。还要了解什么是数字签名。
最重要的是会应用.NET2.0提供的基类,写一些加密解密函数。学习了SHA1,MD5和3DES的算法。具体的源代码如下:
/Files/JBoy/encrypt.rar
下面是3DES对称加密解密:
1
![](https://www.cnblogs.com/Images/dot.gif)
2
![](https://www.cnblogs.com/Images/dot.gif)
3
//用户自己设置
4
private static string key_string = "1234567890123456"; //加密密钥
5
private static string iv_string = "12345678"; //初始化向量
6
//系统根据字符串自动生成
7
private static byte[] key;
8
private static byte[] iv;
9
10
//初始化iv与key
11
private static void InitKeyAndIV()
12
{
13
/*比较复杂的方式生成key与iv
14
//3DES使用124位--192位加密,密钥的长度是16--24个字节
15
key = new byte[16]; //124位密钥
16
iv = new byte[8]; //64位
17
18
SHA1 hasher = (SHA1)HashAlgorithm.Create("sha1");
19
//向字符编码为字节数组
20
byte[] key_arr = ASCIIEncoding.ASCII.GetBytes(key_string);
21
byte[] iv_arr = ASCIIEncoding.ASCII.GetBytes(iv_string);
22
23
//哈希相应的字符数组
24
//SHA1哈希后得到20*8位
25
//MD5哈希后得到16*8位
26
byte[] hashed_key_arr = hasher.ComputeHash(key_arr);
27
byte[] hashed_iv_arr = hasher.ComputeHash(iv_arr);
28
29
//取前16个字符为密钥
30
//取前8个字符为初始化向量
31
for (int i = 0; i < 16; i++)
32
{
33
key[i] = hashed_key_arr[i];
34
}
35
for (int i = 0; i < 8; i++)
36
{
37
iv[i] = hashed_iv_arr[i];
38
}
39
*/
40
/*简单的方式生成key与iv*/
41
key = System.Text.Encoding.ASCII.GetBytes(key_string);
42
iv = System.Text.Encoding.ASCII.GetBytes(iv_string);
43
return;
44
}
45
46
//加密算法
47
private static string Encrypt(string data)
48
{
49
//先初始化 KEY 和 IV
50
if (key == null || iv == null)
51
InitKeyAndIV();
52
53
//将要加密的数据编码为字节数组
54
byte[] _data = System.Text.Encoding.UTF8.GetBytes(data); //如果使用Unicode编码,支持中文加密
55
56
//创建流,保存加密后的数据
57
MemoryStream ms = new MemoryStream();
58
59
//创建TripleDESCryptoServiceProvider类
60
TripleDESCryptoServiceProvider _3des = new TripleDESCryptoServiceProvider();
61
62
//创建加密流
63
CryptoStream cs = new CryptoStream(ms, _3des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
64
65
//调用Write方法将加密后的结果写到 流 中
66
cs.Write(_data, 0, _data.Length);
67
//将数据从流写入内存(这句不能少,否则在从内存导出到字符串的时候会出现字符丢失)
68
cs.FlushFinalBlock();
69
70
//ms.ToString(); ==>将流写入字节数组中
71
string result = Convert.ToBase64String(ms.ToArray());
72
//string result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
73
/*
74
StringBuilder result = new StringBuilder();
75
foreach (byte b in ms.ToArray())
76
{
77
result.AppendFormat("{0:X2}", b);
78
}
79
*/
80
81
ms.Close();
82
cs.Close();
83
84
return result;//.ToString();
85
86
}
87
88
//解密算法
89
private static string Decrypt(string data)
90
{
91
if (key == null || iv == null)
92
{
93
InitKeyAndIV();
94
}
95
96
byte[] encrypted_data = Convert.FromBase64String(data);
97![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
MemoryStream ms = new MemoryStream();
99
100
TripleDESCryptoServiceProvider _3des = new TripleDESCryptoServiceProvider();
101
102
CryptoStream cs = new CryptoStream(ms, _3des.CreateDecryptor(key, iv), CryptoStreamMode.Write);
103![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
cs.Write(encrypted_data, 0, encrypted_data.Length);
105
cs.FlushFinalBlock();
106
string result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
107
108
cs.Close();
109
ms.Close();
110
111
return result;
112
}
113
![](https://www.cnblogs.com/Images/dot.gif)
114
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
MD5加密算法:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
明天继续学习,要把它彻底征服了!