以前使用的File是操作小的文本文件,用的并不常见,FileStream(操作字节),可以操作所有格式的文件,用途较广泛

下面做一个通过文件流给文件加密解密的小软件.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 文件加密
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择要加密的文件";
//初始路径
ofd.InitialDirectory = @"C:\Users\home\Desktop";
ofd.Filter = "所有文件|*.*";
ofd.ShowDialog();
txtFile.Text = ofd.FileName;
} private void btnCode_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择要保存文件的路径";
sfd.InitialDirectory = @"C:\Users\home\Desktop";
sfd.Filter = "所有文件|*.*";
sfd.ShowDialog();
txtCodeFile.Text = sfd.FileName; //调用方法
MakeCode( "加密");
}
private void btnUnCode_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择要保存文件的路径";
sfd.InitialDirectory = @"C:\Users\home\Desktop";
sfd.Filter = "所有文件|*.*";
sfd.ShowDialog();
txtCodeFile.Text = sfd.FileName; //调用方法
MakeCode("解密");
} public void MakeCode(string content)
{
//1 先读取文件
using (FileStream fsRead = new FileStream(txtFile.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Read))
{
using (FileStream fsWrite = new FileStream(txtCodeFile.Text.Trim(), FileMode.OpenOrCreate, FileAccess.Write))
{
//设置进度条
progressBar1.Maximum = (int)fsRead.Length; byte[] buffer = new byte[ * * ];
while (true)
{
int r = fsRead.Read(buffer, , buffer.Length);
if (r == )
{
break;
}
byte passWord = ;
if (byte.TryParse(txtPassword.Text.Trim(), out passWord)&& content.Equals("加密"))
{
for (int i = ; i < buffer.Length; i++)
{
buffer[i] += passWord;
}
}
if (byte.TryParse(txtPassword.Text.Trim(), out passWord) && content.Equals("解密"))
{
for (int i = ; i < buffer.Length; i++)
{
buffer[i] -= passWord;
}
} fsWrite.Write(buffer, , r);
progressBar1.Value = (int)fsWrite.Length;
}
}
MessageBox.Show("保存成功!");
}
} }
}

1 将普通视频文件通过此工具加密

2 现在打开文件

3 解密成功后打开文件

最新文章

  1. 小程序用户反馈 - HotApp小程序统计仿微信聊天用户反馈组件,开源
  2. vuejs件同一个挂载点上切换组
  3. linux内核调优参考
  4. 【流媒體】live555—VS2010 下live555编译、使用及测试
  5. css背景图片位置:background的position
  6. WPF窗体属性
  7. jQuery中的$.extend方法总结
  8. 22. Generate Parentheses
  9. ABP官方文档翻译 9.3 NHibernate集成
  10. 爬虫之正则和xpath
  11. golang初识5 - interface
  12. python 基础 ------字符串的调用详解(1)
  13. Leetcode 80.删除排序数组中的重复项 II By Python
  14. 关于Java中System.gc() 与System.runFinalization()
  15. kettle的资源库
  16. 小型Http服务器
  17. Oracle等待事件之db file scattered read
  18. winform 中设置窗体的默认焦点
  19. DDX_Control、SubclassWindow和SubclassDlgItem
  20. 探索Web Office Apps服务

热门文章

  1. 读取Easy UI的DATa grid里面的所有数据
  2. oracle job入门【原】
  3. POJ - 2187 Beauty Contest(最远点对)
  4. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
  5. Python基础(函数部分)-day04
  6. ECSHOP /mobile/admin/edit_languages.php
  7. Winform中使用WPF控件并动态读取Xaml
  8. JavaScript之DOM等级概述
  9. luogu P2779 [AHOI2016初中组]黑白序列
  10. tcp的连接断开