C#实现对文件进行加密保护的示例代码|新资讯
(相关资料图)
目录
实践过程效果代码实践过程
效果
代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
FileMenu(Application.ExecutablePath + ",0", Application.ExecutablePath);
string[] str = Environment.GetCommandLineArgs();
try
{
string strFile = "";
for (int i = 2; i < str.Length; i++)
strFile += str[i];
FileInfo FInfo = new FileInfo(strFile);
if (FInfo.Extension.ToLower() == ".mr")
{
textBox1.Text = strFile;
button2.Enabled = false;
button3.Enabled = true;
}
}
catch
{
}
}
//选择加密、解密文件
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
FileInfo FInfo = new FileInfo(textBox1.Text);
if (FInfo.Extension.ToLower() == ".mr")
{
button2.Enabled = false;
button3.Enabled = true;
}
else
{
button2.Enabled = true;
button3.Enabled = false;
}
}
}
//加密
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text.Length < 6)
MessageBox.Show("密码不能小于6位!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
EDncrypt myEDncrypt = new EDncrypt(textBox1.Text, textBox2.Text, progressBar1);
myEDncrypt.StartEncrypt();
progressBar1.Value = 0;
}
}
}
//解密
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text.Length < 6)
MessageBox.Show("密码不能小于6位!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
EDncrypt myEDncrypt = new EDncrypt(textBox1.Text, textBox2.Text, progressBar1);
myEDncrypt.StartDncrypt();
progressBar1.Value = 0;
}
}
}
//创建快捷菜单
public static void FileMenu(string strPath, string strName)
{
try
{
Registry.ClassesRoot.CreateSubKey(".mr");
RegistryKey RKey1 = Registry.ClassesRoot.OpenSubKey(".mr", true);
RKey1.SetValue("", "mrfile");
RKey1.Close();
Registry.ClassesRoot.CreateSubKey("mrfile");
RegistryKey RKey2 = Registry.ClassesRoot.OpenSubKey("mrfile", true);
RKey2.CreateSubKey("DefaultIcon");
RKey2.CreateSubKey("shell");
RKey2.Close();
RegistryKey RKey3 = Registry.ClassesRoot.OpenSubKey("mrfile\\DefaultIcon", true);
RKey3.SetValue("", strPath);
RKey3.Close();
RegistryKey RKey4 = Registry.ClassesRoot.OpenSubKey("mrfile\\shell", true);
RKey4.CreateSubKey("解密");
RKey4.Close();
RegistryKey RKey5 = Registry.ClassesRoot.OpenSubKey("mrfile\\shell\\解密", true);
RKey5.CreateSubKey("command");
RKey5.Close();
RegistryKey RKey6 = Registry.ClassesRoot.OpenSubKey("mrfile\\shell\\解密\\command", true);
RKey6.SetValue("", strName + " \\F %1");
RKey6.Close();
}
catch
{
}
}
}
#region 加密、解密类
class EDncrypt
{
#region 定义全局变量及类对象
private string strFile = "";
private string strNewFile = "";
private string strPwd = "";
private ProgressBar PBar = null;
private Thread EThread = null;
private Thread DThread = null;
#endregion
//含参数的构造函数,用来初始化全局变量及对象
public EDncrypt(string name, string pwd, ProgressBar pb)
{
strFile = name;
strPwd = pwd;
PBar = pb;
EThread = new Thread(new ThreadStart(this.myEThread));
DThread = new Thread(new ThreadStart(this.myDThread));
}
//文件加密
private void myEThread()
{
byte[] btRKey = new byte[0];
if (strPwd.Length == 6)
{
btRKey = new byte[]
{
(byte) strPwd[0], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[5], (byte) strPwd[0], (byte) strPwd[1]
};
}
if (strPwd.Length == 7)
{
btRKey = new byte[]
{
(byte) strPwd[0], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[5], (byte) strPwd[6], (byte) strPwd[0]
};
}
if (strPwd.Length >= 8)
{
btRKey = new byte[]
{
(byte) strPwd[0], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[5], (byte) strPwd[6], (byte) strPwd[7]
};
}
FileStream FStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
FileStream NewFStream = new FileStream(strFile + ".mr", FileMode.OpenOrCreate, FileAccess.Write);
NewFStream.SetLength((long) 0);
byte[] buffer = new byte[0x400];
int MinNum = 0;
long length = FStream.Length;
int MaxNum = (int) (length / ((long) 0x400));
PBar.Maximum = MaxNum;
DES myDES = new DESCryptoServiceProvider();
CryptoStream CStream =
new CryptoStream(NewFStream, myDES.CreateEncryptor(btRKey, btRKey), CryptoStreamMode.Write);
while (MinNum < length)
{
int count = FStream.Read(buffer, 0, 0x400);
CStream.Write(buffer, 0, count);
MinNum += count;
try
{
if (PBar.Value < PBar.Maximum)
{
PBar.Value++;
}
}
catch
{
}
}
CStream.Close();
NewFStream.Close();
FStream.Close();
File.Delete(strFile);
MessageBox.Show("文件加密成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//运行加密线程
public void StartEncrypt()
{
EThread.Start();
}
//文件解密
private void myDThread()
{
FileStream FStream = null;
FileStream NewFStream = null;
CryptoStream CStream = null;
try
{
try
{
byte[] btRKey = new byte[0];
if (strPwd.Length == 6)
{
btRKey = new byte[]
{
(byte) strPwd[0], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[5], (byte) strPwd[0], (byte) strPwd[1]
};
}
if (strPwd.Length == 7)
{
btRKey = new byte[]
{
(byte) strPwd[0], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[5], (byte) strPwd[6], (byte) strPwd[0]
};
}
if (strPwd.Length >= 8)
{
btRKey = new byte[]
{
(byte) strPwd[0], (byte) strPwd[1], (byte) strPwd[2], (byte) strPwd[3], (byte) strPwd[4],
(byte) strPwd[5], (byte) strPwd[6], (byte) strPwd[7]
};
}
FStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
strNewFile = strFile.Substring(0, strFile.Length - 3);
NewFStream = new FileStream(strNewFile, FileMode.OpenOrCreate, FileAccess.Write);
NewFStream.SetLength((long) 0);
byte[] buffer = new byte[0x400];
int MinNum = 0;
long length = FStream.Length;
int MaxNum = (int) (length / ((long) 0x400));
PBar.Maximum = MaxNum;
DES myDES = new DESCryptoServiceProvider();
CStream = new CryptoStream(NewFStream, myDES.CreateDecryptor(btRKey, btRKey),
CryptoStreamMode.Write);
while (MinNum < length)
{
int count = FStream.Read(buffer, 0, 0x400);
CStream.Write(buffer, 0, count);
MinNum += count;
try
{
if (PBar.Value < PBar.Maximum)
{
PBar.Value++;
}
}
catch
{
}
}
MessageBox.Show("文件解密成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("文件解密失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
finally
{
CStream.Close();
FStream.Close();
NewFStream.Close();
}
}
//运行解密线程
public void StartDncrypt()
{
DThread.Start();
}
}
#endregion
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 17);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(161, 12);
this.label1.TabIndex = 0;
this.label1.Text = "请选择要加密或解密的文件:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 35);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(263, 21);
this.textBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(301, 33);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(33, 23);
this.button1.TabIndex = 2;
this.button1.Text = "…";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 71);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(83, 12);
this.label2.TabIndex = 3;
this.label2.Text = "加/解密密码:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(92, 68);
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = "*";
this.textBox2.Size = new System.Drawing.Size(151, 21);
this.textBox2.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.ForeColor = System.Drawing.Color.Red;
this.label3.Location = new System.Drawing.Point(243, 71);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(95, 12);
this.label3.TabIndex = 5;
this.label3.Text = "(密码应大于6位)";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(7, 101);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(83, 12);
this.label4.TabIndex = 6;
this.label4.Text = "加/解密进度:";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(92, 99);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(242, 18);
this.progressBar1.TabIndex = 7;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.progressBar1);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Location = new System.Drawing.Point(5, 5);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(342, 123);
this.groupBox1.TabIndex = 8;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "文件加/解密设置";
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(223, 134);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 27);
this.button2.TabIndex = 9;
this.button2.Text = "加密";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(288, 134);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(59, 27);
this.button3.TabIndex = 9;
this.button3.Text = "解密";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(353, 167);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "对文件进行加密保护";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
}
到此这篇关于C#实现对文件进行加密保护的示例代码的文章就介绍到这了,更多相关C#文件加密内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
标签:
- 抢占2023大市场,叼嘴巴叼嘴王胶姆糖在行动!聚焦热销品类,掀起销售热潮!
- 黄山提升政府采购透明度 助力实体经济持续健康稳定发展
- 1-2月黄山新签“双招双引”项目103个 总投资额139.7亿元
- 池州海关共签发RCEP原产地证书22份 签证金额92.7万美元
- 宿州泗县深入推进文旅融合发展 擦亮城市品牌
- 河北工业生产平稳开局 固定资产投资较快增长
- 昆明西山区深入实施人才强区战略 建立健全招商招才引资并轨新模式
- 2月唐山新建商品住宅销售价格与上月持平 同比下降0.8%
- 去年河北电子信息产业实现主营业务收入2367.7亿元 同比增长22.4%
- 绥化望奎以工业化思维为引领 推动肉类加工制造产业腾飞
- 绥化市监局推出多项举措 大力促进有机产品产业发展
- 半路出家无心插柳 杭州西湖区“菌菇宝宝”变废为宝
- 温州鹿城区藤桥主打藤桥熏鸡 近两年销售额年均增长20%以上
- 1-2月安徽限额以上消费品零售额1030.4亿元 同比增长10.4%
- 2021年合肥市茶园面积13.6万亩 产值176350万元
- 淡季不忘引流 京郊民宿市场将很快迎来回暖
- 郴州安仁文旅项目集中开工 总投资1000万元
- 江苏服务业继续保持发展强劲势头 为高质量发展提供有力支撑
- 黄山休宁经开区摸排基础设施项目17个 计划总投资29709万元
- 前两个月宣城出口31亿元 增长34.4%
- 滁州凤阳2021年共接待游客225万人次 旅游综合收入20.25亿元
- 1-2月宿州市民间投资同比增长19.2% 居全省第3位
- 宿州严格审核把关 抢抓发行地方政府专项债券政策机遇
- 1-2月亳州市进出口总值5.5亿元 同比下降17.4%
- 合肥:弘扬茶文化 初步形成一条生态发展之路
- 合肥:建成“数字中国”领先城市 推进城市数字化转型
- 甘肃再续“艾黎情”:探职业教育德技并修
- 【城市守望者】致敬抗“疫”一线的“拆弹专家”
- 浙江绍兴越城区核酸检测结果公布 除1例阳性外其余均为阴性
- 内地首例奥密克戎变异株感染者身体状况如何?来自哪里?专家解读→
- 对变异病毒已有准备!关于中国新冠药物,钟南山发声→
- 江苏睢宁小网格大担当 织就乡村振兴“幸福网”
- 改造老旧小区 共享幸福生活
- 天津静海:群众在哪里,文明实践就延伸到哪里
- 齐齐哈尔:初步判断疫情感染来源为接触新冠病毒污染环境和物品
- 重庆大竹林派出所副所长因对群众态度简单粗暴被停职
- 黑龙江讷河病例感染源初步判断为新冠病毒污染的环境和物品
- 致敬2021
- 浙江瑞安民警捐献造血干细胞:14年前的心愿终将如愿
- “考研房”涨价离谱 律师:借机宰客有违市场伦理
- 广州白云机场:14天内有东莞旅居史的旅客须凭48小时核酸阴性证明乘机
- 浙江绍兴本轮疫情已报告确诊病例145例 无症状感染者1例
- 福建龙岩一男子和前妻斗气 扛着126斤硬币到法院“还钱”
- 重庆这座立交酷似“悟空” 走红 设计师揭秘(图)
- 青海警方破获特大电诈案 涉案流水高达1.7亿
- 云南新增境外输入确诊病例3例
- 黑龙江讷河市5名核酸阳性人员流调溯源:接触被新冠病毒污染的环境和物品
- 男子爱上女主播 假扮女主播闺蜜教其他男粉丝刷单
- 广西三市警方联手破获毒品案 全链条摧毁跨境贩毒团伙
- 广东东莞发现2例无症状感染者,部分镇今起全员筛查
- 从百二秦关到闻道凯旋 一个殉职医生最后的朋友圈
- 浙江发补充说明:三地铁路出行政策随风险等级同步调整
- 内蒙古新增本土确诊病例5例 均在呼伦贝尔满洲里市
- 陕西新增本土确诊病例1例 系隔离酒店工作人员
- 31省份新增新冠肺炎确诊病例76例 其中本土51例
- 浙江新增新冠肺炎确诊病例45例 其中本土44例
- 技能就是财富 技工也是人才
- 黑龙江新增本土确诊病例1例、本土无症状感染者4例
- 冷空气“调休”!我国大部陆续迎回暖 中东部雨雪稀少
- 华北黄淮等地大气扩散条件转差 冷空气将影响中东部
- 别误读了野猪或将不再是“三有”动物
- 您的ETC已到期?当心这个诈骗短信!
- 对回家的“宝贝”少一些关注,也是一种帮助
- 升温!北京今日阳光在线 最高气温将升至8℃
- 那年今日 | 一张漫画涨知识之12月14日
- 40岁男子一觉醒来突然听不见了 原因是……
- 本年度星空压轴大赏上演 双子座流星雨观赏地图来了
- 广东东莞大朗镇报告2例新冠肺炎无症状感染者
- 商丘4885份被盗出生证去哪了?10年“悬案”引关注
- 浙江海宁警方通报国家公祭日女子穿和服逛街
- 厨艺不精调料凑?懒人调料:年轻人的“下厨神器”
- “您的ETC已到期?”警方提醒:当心这个诈骗短信
- “网红”局长的热度 自述:走红后我就没有周末了
- 寻回被拐10年的儿子后又送走 儿子:害我没家了
- 小城里的三张面孔和警号301137
- 倡导“就地过年”,需因地制宜科学防疫
- 别用“入乡随俗”为星巴克找借口
- 北京地铁14号线年底全线贯通运营
- 天津市从入境人员中检出奥密克戎变异株
- “外滩活地图”黄俊:一个不想出圈的段子手交警
- 寻找一双儿女的25年
- 无锡市场监管部门责成星巴克涉事门店停业整改
- 海岛警事:为了一座岛和2900平方公里的海
- 北京民警宏福苑抗疫26天:“今夜我和雪花一起出发”
- 星巴克的“金标准”缘何败给了“潜规则”
- 患者被低价药“惊呆”的场面应该更多些
- 影视剧“超前点评”不止是“低级错误”
- “南昌鹦鹉案”下发不起诉决定书 网店上架费氏牡丹鹦鹉被拒
- 河南商丘4885份出生医学证明被盗始末追踪
- 绍兴市病例62-109活动轨迹公布
- 12月7日以来,杭州累计报告新冠肺炎确诊病例19例
- 浙江绍兴新增确诊病例37例 上虞区占36例
- 河南高院对张成功案作出死刑判决
- 四川一滑雪场停电游客被困索道 官方回应
- 浙江绍兴越城区新增1例新冠肺炎确诊病例 当地对防控区域划分进行调整
- 中国内地首次检出新冠病毒奥密克戎变异株
- 知网除了涉及著作权纠纷,是否涉嫌违反《反垄断法》?
- 浙江绍兴越城区新增1例新冠肺炎确诊病例
- 四川眉山千箱柑橘送往呼和浩特市抗疫一线
- 两名青年男女探险三亚落笔洞遗址被困沼泽 消防成功救援
广告
广告
- C#实现对文件进行加密保护的示例代码|新资讯
- 全球新动态:美国“幽灵枪”追凶:3D打印枪何以成为“法外之徒”?
- 再无瓜葛!人人影视与人人视频彻底说再见 快讯
- 男生圆脸适合什么发型和刘海(男生圆脸适合什么发型)
- 林业强_关于林业强概略 天天快看
- DDR5内存便宜了 装机选64G还是7000MHz?|即时
- 全球资讯:逆水寒手游生死奇门怎么触发[多图]
- 废柴逆袭冰山倾城妃小说(废柴逆袭冰山倾城妃) 天天最新
- 快报:绝压和表压的关系_绝压
- 本田crv2021款u盘听歌在哪_本田crv2021款怎么用u盘听歌?
- 天天要闻:浮天水送无穷树带雨云埋一半山蕴含了什么样的思想感情(浮天水送无穷树带雨云埋一半山的意思)
- 天天看点:应对需求压力 激发内生动力——当前工业经济走势观察
- 全球微头条丨非遗与普法“双向奔赴”——醴陵“星子灯”点亮法治之光
- 圣诞树折纸教程高难度_圣诞树折纸教程
- “上四休三”是企业和员工双赢的办公方式|世界消息
- 学区不理想,“托关系”给儿子上中学,一女子被骗近百万元
- 最小的“画家”只有三岁!大学美术馆举办“童画展”
- Doinb一月直播370小时,荣膺劳模
- 5座中型车67万左右哪款好?购车指数选车:第一名你绝对想不到|焦点热议
- 人大代表建言营商环境建设,助力经济高质量发展 全球头条






