Commit ae2cc16a authored by tr's avatar tr

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

parent be173998
......@@ -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 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
{
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 EncryptString(string pEncryptedStr)
{
//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);
//取到密码的字节流
public static string Encryptor(string src)
{
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;
}
......
......@@ -28,19 +28,20 @@
/// </summary>
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.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();
//
// tbPassword
// tbSrcString
//
this.tbPassword.Location = new System.Drawing.Point(116, 51);
this.tbPassword.Name = "tbPassword";
this.tbPassword.Size = new System.Drawing.Size(171, 21);
this.tbPassword.TabIndex = 0;
this.tbSrcString.Location = new System.Drawing.Point(116, 51);
this.tbSrcString.Name = "tbSrcString";
this.tbSrcString.Size = new System.Drawing.Size(171, 21);
this.tbSrcString.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.tbPassword);
this.Controls.Add(this.tbSrcString);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "wLoginPwd";
this.Text = "wLoginPwd";
......@@ -97,10 +109,11 @@
#endregion
private System.Windows.Forms.TextBox tbPassword;
private System.Windows.Forms.TextBox tbSrcString;
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
......@@ -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();
......
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