Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
Ftt_Common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Ftt_Common
Commits
ae2cc16a
Commit
ae2cc16a
authored
Feb 22, 2021
by
tr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、登录密码-加密解密优化
parent
be173998
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
51 deletions
+68
-51
Utils.cs
Tools/MyUtils/Utils.cs
+26
-39
wLoginPwd.Designer.cs
Tools/UI/wLoginPwd.Designer.cs
+22
-9
wLoginPwd.cs
Tools/UI/wLoginPwd.cs
+20
-3
No files found.
Tools/MyUtils/Utils.cs
View file @
ae2cc16a
...
...
@@ -87,68 +87,55 @@ namespace Tools.MyUtils
#
endregion
#
region
加密解密
/// <summary>
/// 秘钥
/// </summary>
private
static
string
KEY_ENCRYPT
=
"c_dc"
;
/// <summary>
///
解密字符串
///
DES解密
/// </summary>
/// <param name="
pDecryptStr">
</param>
/// <param name="
src">源字符串
</param>
/// <returns></returns>
public
static
string
Decrypt
String
(
string
pDecryptStr
)
public
static
string
Decrypt
or
(
string
src
)
{
DESCryptoServiceProvider
desc
=
new
DESCryptoServiceProvider
();
//产生key
PasswordDeriveBytes
db
=
new
PasswordDeriveBytes
(
KEY_ENCRYPT
,
null
);
byte
[]
key
=
db
.
GetBytes
(
8
);
//存储解密后的数据
MemoryStream
ms
=
new
MemoryStream
();
CryptoStream
cs
=
new
CryptoStream
(
ms
,
desc
.
CreateDecryptor
(
key
,
key
),
CryptoStreamMode
.
Write
);
//取到加密后的数据的字节流,如果是保存到文件
try
{
byte
[]
databytes
=
Convert
.
FromBase64String
(
pDecryptStr
);
//解密数据
cs
.
Write
(
databytes
,
0
,
databytes
.
Length
);
var
key
=
new
PasswordDeriveBytes
(
KEY_ENCRYPT
,
null
).
GetBytes
(
8
);
var
transform
=
new
DESCryptoServiceProvider
().
CreateDecryptor
(
key
,
key
);
var
ms
=
new
MemoryStream
();
var
cs
=
new
CryptoStream
(
ms
,
transform
,
CryptoStreamMode
.
Write
);
var
data
=
Convert
.
FromBase64String
(
src
);
cs
.
Write
(
data
,
0
,
data
.
Length
);
cs
.
FlushFinalBlock
();
byte
[]
res
=
ms
.
ToArray
();
//返回解密后的数据,这里返回的数据应该和参数pwd的值相同。
return
System
.
Text
.
Encoding
.
UTF8
.
GetString
(
res
);
return
Encoding
.
UTF8
.
GetString
(
ms
.
ToArray
());
}
catch
catch
(
Exception
)
{
Utils
.
Tip
(
$"字符串
{
pDecryptStr
}
解密不成功。"
);
Utils
.
Tip
(
$"字符串
{
src
}
解密不成功。"
);
return
null
;
}
}
/// <summary>
///
加密字符串
///
DES加密
/// </summary>
/// <param name="
pEncryptedStr">
</param>
/// <param name="
src">源字符串
</param>
/// <returns></returns>
public
static
string
Encrypt
String
(
string
pEncryptedStr
)
public
static
string
Encrypt
or
(
string
src
)
{
//des进行加密
DESCryptoServiceProvider
desc
=
new
DESCryptoServiceProvider
();
//产生key
PasswordDeriveBytes
db
=
new
PasswordDeriveBytes
(
KEY_ENCRYPT
,
null
);
byte
[]
key
=
db
.
GetBytes
(
8
);
//存储加密后的数据
MemoryStream
ms
=
new
MemoryStream
();
CryptoStream
cs
=
new
CryptoStream
(
ms
,
desc
.
CreateEncryptor
(
key
,
key
),
CryptoStreamMode
.
Write
);
//取到密码的字节流
try
{
byte
[]
data
=
Encoding
.
UTF8
.
GetBytes
(
pEncryptedStr
);
//进行加密
var
key
=
new
PasswordDeriveBytes
(
KEY_ENCRYPT
,
null
).
GetBytes
(
8
);
var
transform
=
new
DESCryptoServiceProvider
().
CreateEncryptor
(
key
,
key
);
var
ms
=
new
MemoryStream
();
var
cs
=
new
CryptoStream
(
ms
,
transform
,
CryptoStreamMode
.
Write
);
var
data
=
Encoding
.
UTF8
.
GetBytes
(
src
);
cs
.
Write
(
data
,
0
,
data
.
Length
);
cs
.
FlushFinalBlock
();
//取加密后的数据
byte
[]
res
=
ms
.
ToArray
();
return
Convert
.
ToBase64String
(
res
);
return
Convert
.
ToBase64String
(
ms
.
ToArray
());
}
catch
catch
(
Exception
)
{
Utils
.
Tip
(
$"字符串
{
pEncryptedStr
}
加密不成功。"
);
Utils
.
Tip
(
$"字符串
{
src
}
加密不成功。"
);
return
null
;
}
...
...
Tools/UI/wLoginPwd.Designer.cs
View file @
ae2cc16a
...
...
@@ -28,19 +28,20 @@
/// </summary>
private
void
InitializeComponent
()
{
this
.
tb
Password
=
new
System
.
Windows
.
Forms
.
TextBox
();
this
.
tb
SrcString
=
new
System
.
Windows
.
Forms
.
TextBox
();
this
.
label1
=
new
System
.
Windows
.
Forms
.
Label
();
this
.
btnDecrypt
=
new
System
.
Windows
.
Forms
.
Button
();
this
.
tbResult
=
new
System
.
Windows
.
Forms
.
TextBox
();
this
.
label2
=
new
System
.
Windows
.
Forms
.
Label
();
this
.
btnEncrypt
=
new
System
.
Windows
.
Forms
.
Button
();
this
.
SuspendLayout
();
//
// tb
Password
// tb
SrcString
//
this
.
tb
Password
.
Location
=
new
System
.
Drawing
.
Point
(
116
,
51
);
this
.
tb
Password
.
Name
=
"tbPassword
"
;
this
.
tb
Password
.
Size
=
new
System
.
Drawing
.
Size
(
171
,
21
);
this
.
tb
Password
.
TabIndex
=
0
;
this
.
tb
SrcString
.
Location
=
new
System
.
Drawing
.
Point
(
116
,
51
);
this
.
tb
SrcString
.
Name
=
"tbSrcString
"
;
this
.
tb
SrcString
.
Size
=
new
System
.
Drawing
.
Size
(
171
,
21
);
this
.
tb
SrcString
.
TabIndex
=
0
;
//
// label1
//
...
...
@@ -49,7 +50,7 @@
this
.
label1
.
Name
=
"label1"
;
this
.
label1
.
Size
=
new
System
.
Drawing
.
Size
(
29
,
12
);
this
.
label1
.
TabIndex
=
1
;
this
.
label1
.
Text
=
"
密文
"
;
this
.
label1
.
Text
=
"
文本
"
;
//
// btnDecrypt
//
...
...
@@ -77,16 +78,27 @@
this
.
label2
.
TabIndex
=
4
;
this
.
label2
.
Text
=
"结果"
;
//
// btnEncrypt
//
this
.
btnEncrypt
.
Location
=
new
System
.
Drawing
.
Point
(
212
,
78
);
this
.
btnEncrypt
.
Name
=
"btnEncrypt"
;
this
.
btnEncrypt
.
Size
=
new
System
.
Drawing
.
Size
(
75
,
23
);
this
.
btnEncrypt
.
TabIndex
=
5
;
this
.
btnEncrypt
.
Text
=
"加密"
;
this
.
btnEncrypt
.
UseVisualStyleBackColor
=
true
;
this
.
btnEncrypt
.
Click
+=
new
System
.
EventHandler
(
this
.
btnEncrypt_Click
);
//
// wLoginPwd
//
this
.
AutoScaleDimensions
=
new
System
.
Drawing
.
SizeF
(
6F
,
12F
);
this
.
AutoScaleMode
=
System
.
Windows
.
Forms
.
AutoScaleMode
.
Font
;
this
.
ClientSize
=
new
System
.
Drawing
.
Size
(
377
,
213
);
this
.
Controls
.
Add
(
this
.
btnEncrypt
);
this
.
Controls
.
Add
(
this
.
label2
);
this
.
Controls
.
Add
(
this
.
tbResult
);
this
.
Controls
.
Add
(
this
.
btnDecrypt
);
this
.
Controls
.
Add
(
this
.
label1
);
this
.
Controls
.
Add
(
this
.
tb
Password
);
this
.
Controls
.
Add
(
this
.
tb
SrcString
);
this
.
FormBorderStyle
=
System
.
Windows
.
Forms
.
FormBorderStyle
.
FixedToolWindow
;
this
.
Name
=
"wLoginPwd"
;
this
.
Text
=
"wLoginPwd"
;
...
...
@@ -97,10 +109,11 @@
#
endregion
private
System
.
Windows
.
Forms
.
TextBox
tb
Password
;
private
System
.
Windows
.
Forms
.
TextBox
tb
SrcString
;
private
System
.
Windows
.
Forms
.
Label
label1
;
private
System
.
Windows
.
Forms
.
Button
btnDecrypt
;
private
System
.
Windows
.
Forms
.
TextBox
tbResult
;
private
System
.
Windows
.
Forms
.
Label
label2
;
private
System
.
Windows
.
Forms
.
Button
btnEncrypt
;
}
}
\ No newline at end of file
Tools/UI/wLoginPwd.cs
View file @
ae2cc16a
...
...
@@ -18,7 +18,7 @@ namespace Tools.UI
/// <summary>
/// 密文
/// </summary>
string
Password
=>
this
.
tbPassword
.
Text
.
Trim
();
string
Source
=>
this
.
tbSrcString
.
Text
.
Trim
();
/// <summary>
/// 结果
/// </summary>
...
...
@@ -40,8 +40,25 @@ namespace Tools.UI
/// <param name="e"></param>
private
void
btnDecrypt_Click
(
object
sender
,
EventArgs
e
)
{
//解密
this
.
Result
=
Utils
.
DecryptString
(
this
.
Password
);
this
.
Result
=
Utils
.
Decryptor
(
this
.
Source
);
this
.
FocusResult
();
}
/// <summary>
/// 按钮事件:加密
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
btnEncrypt_Click
(
object
sender
,
EventArgs
e
)
{
this
.
Result
=
Utils
.
Encryptor
(
this
.
Source
);
this
.
FocusResult
();
}
/// <summary>
/// 选中结果
/// </summary>
private
void
FocusResult
()
{
//获取焦点,并选中文本
this
.
tbResult
.
Focus
();
this
.
tbResult
.
SelectAll
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment