基本信息
源码名称:加密解密文件 工具源码下载
源码大小:0.65M
文件格式:.zip
开发语言:C#
更新时间:2017-03-01
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
生成加密解密文件
生成加密解密文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EncryptFileTool
{
public partial class KeysightForm : FormBase
{
#region Field
//窗体圆角矩形半径
private int _radius = 5;
//是否允许窗体改变大小
private bool _canResize = true;
private Image _fringe = Image.FromFile(@"Res/fringe_bkg.png");
private Image _formBkg = Image.FromFile(@"Res/FormBkg/Kesight bg.jpg");
private Button btnDecryptFile;
private Button btnEncryptFile;
private TextBox txtPassword;
private Label label2;
private Button btnSelectFile;
private TextBox txtFileName;
private Label label1;
private OpenFileDialog openFileDialog1;
//系统按钮管理器
private SystemButtonManager _systemButtonManager;
#endregion
public KeysightForm()
{
InitializeComponent();
this.CenterToScreen();
FormExIni();
_systemButtonManager = new SystemButtonManager(this);
this.CanResize = false;
}
#region Properties
[DefaultValue(typeof(byte), "5")]
public int Radius
{
get
{
return _radius;
}
set
{
if (_radius != value)
{
_radius = value;
this.Invalidate();
}
}
}
public bool CanResize
{
get
{
return _canResize;
}
set
{
if (_canResize != value)
{
_canResize = value;
}
}
}
public override Image BackgroundImage
{
get
{
return _formBkg;
}
set
{
if (_formBkg != value)
{
_formBkg = value;
Invalidate();
}
}
}
internal Rectangle IconRect
{
get
{
if (base.ShowIcon && base.Icon != null)
{
return new Rectangle(8, 6, SystemInformation.SmallIconSize.Width, SystemInformation.SmallIconSize.Width);
}
return Rectangle.Empty;
}
}
internal Rectangle TextRect
{
get
{
if (base.Text.Length != 0)
{
return new Rectangle(IconRect.Right 2, 4, Width - (8 IconRect.Width 2), Font.Height);
}
return Rectangle.Empty;
}
}
internal SystemButtonManager SystemButtonManager
{
get
{
if (_systemButtonManager == null)
{
_systemButtonManager = new SystemButtonManager(this);
}
return _systemButtonManager;
}
}
#endregion
#region Overrides
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode)
{
if (MaximizeBox) { cp.Style |= (int)WindowStyle.WS_MAXIMIZEBOX; }
if (MinimizeBox) { cp.Style |= (int)WindowStyle.WS_MINIMIZEBOX; }
//cp.ExStyle |= (int)WindowStyle.WS_CLIPCHILDREN; //防止因窗体控件太多出现闪烁
cp.ClassStyle |= (int)ClassStyle.CS_DropSHADOW; //实现窗体边框阴影效果
}
return cp;
}
}
protected override void OnCreateControl()
{
base.OnCreateControl();
RenderHelper.SetFormRoundRectRgn(this, Radius);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
RenderHelper.SetFormRoundRectRgn(this, Radius);
UpdateSystemButtonRect();
UpdateMaxButton();
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Win32.WM_ERASEBKGND:
m.Result = IntPtr.Zero;
break;
case Win32.WM_NCHITTEST:
WmNcHitTest(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Move);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Down);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Up);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
SystemButtonManager.ProcessMouseOperate(Point.Empty, MouseOperate.Leave);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//draw BackgroundImage
e.Graphics.DrawImage(_formBkg, ClientRectangle, new Rectangle(0, 0, _formBkg.Width, _formBkg.Height), GraphicsUnit.Pixel);
//draw form main part
RenderHelper.DrawFromAlphaMainPart(this, e.Graphics);
//draw system buttons
SystemButtonManager.DrawSystemButtons(e.Graphics);
//draw fringe
RenderHelper.DrawFormFringe(this, e.Graphics, _fringe, Radius);
//draw icon
if (Icon != null && ShowIcon)
{
e.Graphics.DrawIcon(Icon, IconRect);
}
//draw text
if (Text.Length != 0)
{
TextRenderer.DrawText(
e.Graphics,
Text, Font,
TextRect,
Color.White,
TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (_systemButtonManager != null)
{
_systemButtonManager.Dispose();
_systemButtonManager = null;
_formBkg.Dispose();
_formBkg = null;
_fringe.Dispose();
_fringe = null;
}
}
}
#endregion
#region Private Methods
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(KeysightForm));
this.btnDecryptFile = new System.Windows.Forms.Button();
this.btnEncryptFile = new System.Windows.Forms.Button();
this.txtPassword = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnSelectFile = new System.Windows.Forms.Button();
this.txtFileName = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// btnDecryptFile
//
this.btnDecryptFile.BackColor = System.Drawing.Color.Gainsboro;
this.btnDecryptFile.FlatAppearance.BorderSize = 0;
this.btnDecryptFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDecryptFile.Location = new System.Drawing.Point(380, 200);
this.btnDecryptFile.Name = "btnDecryptFile";
this.btnDecryptFile.Size = new System.Drawing.Size(83, 36);
this.btnDecryptFile.TabIndex = 13;
this.btnDecryptFile.Text = "解密";
this.btnDecryptFile.UseVisualStyleBackColor = false;
this.btnDecryptFile.Click = new System.EventHandler(this.btnDecryptFile_Click);
//
// btnEncryptFile
//
this.btnEncryptFile.BackColor = System.Drawing.Color.Gainsboro;
this.btnEncryptFile.FlatAppearance.BorderSize = 0;
this.btnEncryptFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnEncryptFile.Location = new System.Drawing.Point(32, 200);
this.btnEncryptFile.Name = "btnEncryptFile";
this.btnEncryptFile.Size = new System.Drawing.Size(83, 36);
this.btnEncryptFile.TabIndex = 12;
this.btnEncryptFile.Text = "加密";
this.btnEncryptFile.UseVisualStyleBackColor = false;
this.btnEncryptFile.Click = new System.EventHandler(this.btnEncryptFile_Click);
//
// txtPassword
//
this.txtPassword.BackColor = System.Drawing.SystemColors.Window;
this.txtPassword.Location = new System.Drawing.Point(157, 75);
this.txtPassword.Name = "txtPassword";
this.txtPassword.Size = new System.Drawing.Size(179, 25);
this.txtPassword.TabIndex = 11;
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Location = new System.Drawing.Point(28, 78);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(118, 19);
this.label2.TabIndex = 10;
this.label2.Text = "输入密码 (可选择):";
//
// btnSelectFile
//
this.btnSelectFile.BackColor = System.Drawing.Color.Gainsboro;
this.btnSelectFile.FlatAppearance.BorderSize = 0;
this.btnSelectFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnSelectFile.Location = new System.Drawing.Point(380, 34);
this.btnSelectFile.Name = "btnSelectFile";
this.btnSelectFile.Size = new System.Drawing.Size(83, 25);
this.btnSelectFile.TabIndex = 9;
this.btnSelectFile.Text = "选择文件";
this.btnSelectFile.UseVisualStyleBackColor = false;
this.btnSelectFile.Click = new System.EventHandler(this.btnSelectFile_Click);
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(157, 34);
this.txtFileName.Name = "txtFileName";
this.txtFileName.ReadOnly = true;
this.txtFileName.Size = new System.Drawing.Size(179, 25);
this.txtFileName.TabIndex = 8;
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Location = new System.Drawing.Point(25, 38);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 19);
this.label1.TabIndex = 7;
this.label1.Text = "选择操作文件:";
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// KeysightForm
//
this.ClientSize = new System.Drawing.Size(513, 350);
this.Controls.Add(this.btnDecryptFile);
this.Controls.Add(this.btnEncryptFile);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnSelectFile);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.label1);
this.Font = new System.Drawing.Font("Microsoft YaHei", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "KeysightForm";
this.Text = "加密解密";
this.ResumeLayout(false);
this.PerformLayout();
}
private void FormExIni()
{
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
SetStyles();
}
private void SetStyles()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
}
private void WmNcHitTest(ref Message m) //调整窗体大小
{
int wparam = m.LParam.ToInt32();
Point mouseLocation = new Point(RenderHelper.LOWORD(wparam), RenderHelper.HIWORD(wparam));
mouseLocation = PointToClient(mouseLocation);
if (WindowState != FormWindowState.Maximized)
{
if (CanResize == true)
{
if (mouseLocation.X < 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPLEFT);
return;
}
if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPRIGHT);
return;
}
if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
return;
}
if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
return;
}
if (mouseLocation.Y < 3)
{
m.Result = new IntPtr(Win32.HTTOP);
return;
}
if (mouseLocation.Y > Height - 3)
{
m.Result = new IntPtr(Win32.HTBOTTOM);
return;
}
if (mouseLocation.X < 3)
{
m.Result = new IntPtr(Win32.HTLEFT);
return;
}
if (mouseLocation.X > Width - 3)
{
m.Result = new IntPtr(Win32.HTRIGHT);
return;
}
}
}
m.Result = new IntPtr(Win32.HTCLIENT);
}
private void UpdateMaxButton() //根据窗体的状态更换最大(还原)系统按钮
{
bool isMax = WindowState == FormWindowState.Maximized;
if (isMax)
{
SystemButtonManager.SystemButtonArray[1].NormalImg = Image.FromFile(@"Res\SystemButton\restore_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = Image.FromFile(@"Res\SystemButton\restore_highlight.png");
SystemButtonManager.SystemButtonArray[1].PressedImg = Image.FromFile(@"Res\SystemButton\restore_press.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "还原";
}
else
{
SystemButtonManager.SystemButtonArray[1].NormalImg = Image.FromFile(@"Res\SystemButton\max_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = Image.FromFile(@"Res\SystemButton\max_highlight.png");
SystemButtonManager.SystemButtonArray[1].PressedImg = Image.FromFile(@"Res\SystemButton\max_press.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "最大化";
}
}
protected void UpdateSystemButtonRect()
{
bool isShowMaxButton = MaximizeBox;
bool isShowMinButton = MinimizeBox;
Rectangle closeRect = new Rectangle(
Width - SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
SystemButtonManager.SystemButtonArray[0].NormalImg.Height);
//update close button location rect.
SystemButtonManager.SystemButtonArray[0].LocationRect = closeRect;
//Max
if (isShowMaxButton)
{
SystemButtonManager.SystemButtonArray[1].LocationRect = new Rectangle(
closeRect.X - SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
SystemButtonManager.SystemButtonArray[1].NormalImg.Height);
}
else
{
SystemButtonManager.SystemButtonArray[1].LocationRect = Rectangle.Empty;
}
//Min
if (!isShowMinButton)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = Rectangle.Empty;
return;
}
if (isShowMaxButton)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = new Rectangle(
SystemButtonManager.SystemButtonArray[1].LocationRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
}
else
{
SystemButtonManager.SystemButtonArray[2].LocationRect = new Rectangle(
closeRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
}
}
#endregion
private void btnSelectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog1.FileName;
}
}
private void btnEncryptFile_Click(object sender, EventArgs e)
{
string inFile = txtFileName.Text;
if (string.IsNullOrEmpty(inFile))
{
MessageBox.Show("请选择加密文件");
return;
}
string outFile = inFile ".dat";
string password = txtPassword.Text;
try
{
DESFile.Encrypt(inFile, outFile, password);
File.Delete(inFile);
txtFileName.Text = string.Empty;
MessageBox.Show("加密成功");
}
catch (Exception)
{
}
}
private void btnDecryptFile_Click(object sender, EventArgs e)
{
string inFile = txtFileName.Text;
if (string.IsNullOrEmpty(inFile))
{
MessageBox.Show("请选择解密文件");
return;
}
string outFile = inFile.Substring(0, inFile.Length - 4);
string password = txtPassword.Text;
try
{
DESFile.DecryptFile(inFile, outFile, password);
File.Delete(inFile);
txtFileName.Text = string.Empty;
MessageBox.Show("解密成功");
}
catch (Exception)
{
}
}
}
}