希望多多指教[C#] 纯文本查看 复制代码 using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace desfile
{
public partial class Form1 : Form
{
string fileName;
string desFileName;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c://";
openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog.FileName;
textBox1.Text = fileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
desFileName = fileName + "E";
FileStream fin = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(desFileName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[1000];
long rdlen = 0;
//This is the total number of bytes written.
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
byte[] desKey = ASCIIEncoding.ASCII.GetBytes("luckyprg");
//the keys length must be morethan 8
byte[] desIV = ASCIIEncoding.ASCII.GetBytes("luckyprg");
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 1000);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
private void button3_Click(object sender, EventArgs e)
{
desFileName = fileName + "D";
FileStream fin = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(desFileName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[1000];
long rdlen = 0;
//This is the total number of bytes written.
long totlen = fin.Length;
int len;
DES des = new DESCryptoServiceProvider();
byte[] desKey = ASCIIEncoding.ASCII.GetBytes("luckyprg");
//the keys length must be morethan 8
byte[] desIV = ASCIIEncoding.ASCII.GetBytes("luckyprg");
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 1000);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
}
}
|