大三了,快大四了,找工作了,先找个单位实践实践,不为钱,只想学点东西!
十六周计划,学习:
ASP.NET2.0自定义配置学习。
学习有关.NET2.0的加密解密类。
学习PetShop4.0。
C#异常复习。
本想在这个周末,重写完全支持ACCESS的成员资格与角色管理提供程序,在重写MembershipProvider的时候,遇到了有关密码加密的东东,以前也用到过,只是从网上下一个算法,现在想好好研究这个东西,今天花一整天的时间学习有关知识。
首选是有关加密解密的概要,什么是对称加密,什么是非对称加密,哈希是怎么回事,这些应该是要清楚的。还要了解什么是数字签名。
最重要的是会应用.NET2.0提供的基类,写一些加密解密函数。学习了SHA1,MD5和3DES的算法。具体的源代码如下:
/Files/JBoy/encrypt.rar
下面是3DES对称加密解密:
1

2

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
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
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

114


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

39

40


41

42

43

44

45

46

47

48



49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73


74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90



91

92



93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113


114


MD5加密算法:
















明天继续学习,要把它彻底征服了!