-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDialogPSW.cs
151 lines (127 loc) · 4.94 KB
/
DialogPSW.cs
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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WeCode1._0
{
public partial class DialogPSW : Form
{
public string ReturnVal { get; protected set; }
public string strOpenType = "";
public DialogPSW(string OpenType)
{
InitializeComponent();
strOpenType = OpenType;
if (OpenType == "0")
{
//第一次设置密码
this.Text = "第一次使用本地加密,请设置密码!";
}
else if (OpenType == "1")
{
//加密
this.Text = "加密";
labelConfirm.Visible = false;
textBoxConfirm.Visible = false;
}
else if (OpenType == "2")
{
//取消加密
this.Text = "解密";
labelConfirm.Visible = false;
textBoxConfirm.Visible = false;
}
else if(OpenType=="3")
{
//查看时输入密码
this.Text = "请输入密码";
labelConfirm.Visible = false;
textBoxConfirm.Visible = false;
}
}
private void buttonOK_Click(object sender, EventArgs e)
{
string pswInput = textBoxInput.Text;
string pswConfirm = textBoxConfirm.Text;
if (strOpenType == "0")
{
//校验密码长度以及两次输入是否一样
if (pswInput != pswConfirm)
{
MessageBox.Show("两次密码不一致,请重新输入!");
return;
}
if (pswInput.Length > 16 || pswInput.Length < 6)
{
MessageBox.Show("密码长度需在6-16位之间!");
return;
}
//密码转换,写入到内存以及数据库中
string keyD = EncryptDecrptt.KeyA2KeyD(pswInput);
string keyDmd5 = EncryptDecrptt.str2MD5(keyD);
string keyE = EncryptDecrptt.EncrptyByKey(keyD, pswInput);
//第一次设置密码,写入到数据库
OleDbParameter p1 = new OleDbParameter("@KeyE", OleDbType.VarChar);
p1.Value = keyE;
OleDbParameter p2 = new OleDbParameter("@KeyD5", OleDbType.VarChar);
p2.Value = keyDmd5;
OleDbParameter[] ArrPara = new OleDbParameter[2];
ArrPara[0] = p1;
ArrPara[1] = p2;
string SQL = "insert into MyKeys(KeyE,KeyD5) values(@KeyE,@KeyD5)";
AccessAdo.ExecuteNonQuery(SQL, ArrPara);
//临时存储keyD到内存,避免每次加密文章输入密码
Attachment.KeyD = keyD;
//返回keyD
ReturnVal = keyD;
DialogResult = DialogResult.OK;
this.Close();
}
else if (strOpenType == "1" || strOpenType == "2"||strOpenType=="3")
{
//加密,验证密码是否与数据库MD5匹配
string d5 = AccessAdo.ExecuteScalar("select top 1 KeyD5 from MyKeys").ToString();
//string KeyD = EncryptDecrptt.KeyA2KeyD(pswInput);
//keyD需用keyE和keyA解密
string KeyE=AccessAdo.ExecuteScalar("select top 1 KeyE from MyKeys").ToString();
string d51 = EncryptDecrptt.str2MD5(EncryptDecrptt.KeyA2KeyD(pswInput));
if(d5!=d51)
{
MessageBox.Show("密码错误!");
return;
}
//keyD需从keyE和明文密码解密出来,修改了密码也不会改变
//存储在数据库的keyD5会随着密码变化变化,目的是为了对比密码是否正确
string KeyD = EncryptDecrptt.DecrptyByKey(KeyE, pswInput);
//写入到内存
Attachment.KeyD = KeyD;
//返回keyD
ReturnVal = KeyD;
DialogResult = DialogResult.OK;
this.Close();
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
this.Close();
}
private void textBoxInput_DoubleClick(object sender, EventArgs e)
{
}
private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void textBoxInput_TextChanged(object sender, EventArgs e)
{
this.textBoxInput.PasswordChar = '●';
}
private void DialogPSW_KeyUp(object sender, KeyEventArgs e)
{
}
}
}