Commit ae2cc16a authored by tr's avatar tr

1、登录密码-加密解密优化

parent be173998
...@@ -87,68 +87,55 @@ namespace Tools.MyUtils ...@@ -87,68 +87,55 @@ namespace Tools.MyUtils
#endregion #endregion
#region 加密解密 #region 加密解密
/// <summary>
/// 秘钥
/// </summary>
private static string KEY_ENCRYPT = "c_dc"; private static string KEY_ENCRYPT = "c_dc";
/// <summary> /// <summary>
/// 解密字符串 /// DES解密
/// </summary> /// </summary>
/// <param name="pDecryptStr"></param> /// <param name="src">源字符串</param>
/// <returns></returns> /// <returns></returns>
public static string DecryptString(string pDecryptStr) public static string Decryptor(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 try
{ {
byte[] databytes = Convert.FromBase64String(pDecryptStr); var key = new PasswordDeriveBytes(KEY_ENCRYPT, null).GetBytes(8);
//解密数据 var transform = new DESCryptoServiceProvider().CreateDecryptor(key, key);
cs.Write(databytes, 0, databytes.Length); 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(); cs.FlushFinalBlock();
byte[] res = ms.ToArray(); return Encoding.UTF8.GetString(ms.ToArray());
//返回解密后的数据,这里返回的数据应该和参数pwd的值相同。
return System.Text.Encoding.UTF8.GetString(res);
} }
catch catch (Exception)
{ {
Utils.Tip($"字符串{pDecryptStr}解密不成功。"); Utils.Tip($"字符串{src}解密不成功。");
return null; return null;
} }
} }
/// <summary> /// <summary>
/// 加密字符串 /// DES加密
/// </summary> /// </summary>
/// <param name="pEncryptedStr"></param> /// <param name="src">源字符串</param>
/// <returns></returns> /// <returns></returns>
public static string EncryptString(string pEncryptedStr) public static string Encryptor(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 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.Write(data, 0, data.Length);
cs.FlushFinalBlock(); cs.FlushFinalBlock();
//取加密后的数据 return Convert.ToBase64String(ms.ToArray());
byte[] res = ms.ToArray();
return Convert.ToBase64String(res);
} }
catch catch (Exception)
{ {
Utils.Tip($"字符串{pEncryptedStr}加密不成功。"); Utils.Tip($"字符串{src}加密不成功。");
return null; return null;
} }
......
...@@ -28,19 +28,20 @@ ...@@ -28,19 +28,20 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.tbPassword = new System.Windows.Forms.TextBox(); this.tbSrcString = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.btnDecrypt = new System.Windows.Forms.Button(); this.btnDecrypt = new System.Windows.Forms.Button();
this.tbResult = new System.Windows.Forms.TextBox(); this.tbResult = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.btnEncrypt = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
// tbPassword // tbSrcString
// //
this.tbPassword.Location = new System.Drawing.Point(116, 51); this.tbSrcString.Location = new System.Drawing.Point(116, 51);
this.tbPassword.Name = "tbPassword"; this.tbSrcString.Name = "tbSrcString";
this.tbPassword.Size = new System.Drawing.Size(171, 21); this.tbSrcString.Size = new System.Drawing.Size(171, 21);
this.tbPassword.TabIndex = 0; this.tbSrcString.TabIndex = 0;
// //
// label1 // label1
// //
...@@ -49,7 +50,7 @@ ...@@ -49,7 +50,7 @@
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12); this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 1; this.label1.TabIndex = 1;
this.label1.Text = "密文"; this.label1.Text = "文本";
// //
// btnDecrypt // btnDecrypt
// //
...@@ -77,16 +78,27 @@ ...@@ -77,16 +78,27 @@
this.label2.TabIndex = 4; this.label2.TabIndex = 4;
this.label2.Text = "结果"; 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 // wLoginPwd
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(377, 213); this.ClientSize = new System.Drawing.Size(377, 213);
this.Controls.Add(this.btnEncrypt);
this.Controls.Add(this.label2); this.Controls.Add(this.label2);
this.Controls.Add(this.tbResult); this.Controls.Add(this.tbResult);
this.Controls.Add(this.btnDecrypt); this.Controls.Add(this.btnDecrypt);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Controls.Add(this.tbPassword); this.Controls.Add(this.tbSrcString);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "wLoginPwd"; this.Name = "wLoginPwd";
this.Text = "wLoginPwd"; this.Text = "wLoginPwd";
...@@ -97,10 +109,11 @@ ...@@ -97,10 +109,11 @@
#endregion #endregion
private System.Windows.Forms.TextBox tbPassword; private System.Windows.Forms.TextBox tbSrcString;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnDecrypt; private System.Windows.Forms.Button btnDecrypt;
private System.Windows.Forms.TextBox tbResult; private System.Windows.Forms.TextBox tbResult;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnEncrypt;
} }
} }
\ No newline at end of file
...@@ -18,7 +18,7 @@ namespace Tools.UI ...@@ -18,7 +18,7 @@ namespace Tools.UI
/// <summary> /// <summary>
/// 密文 /// 密文
/// </summary> /// </summary>
string Password => this.tbPassword.Text.Trim(); string Source => this.tbSrcString.Text.Trim();
/// <summary> /// <summary>
/// 结果 /// 结果
/// </summary> /// </summary>
...@@ -40,8 +40,25 @@ namespace Tools.UI ...@@ -40,8 +40,25 @@ namespace Tools.UI
/// <param name="e"></param> /// <param name="e"></param>
private void btnDecrypt_Click(object sender, EventArgs e) private void btnDecrypt_Click(object sender, EventArgs e)
{ {
//解密 this.Result = Utils.Decryptor(this.Source);
this.Result = Utils.DecryptString(this.Password); 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.Focus();
this.tbResult.SelectAll(); this.tbResult.SelectAll();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment