📄 filecrypt.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace DES
{
public partial class filecrypt : Form
{
public filecrypt()
{
InitializeComponent();
}
private void openButton_Click(object sender, EventArgs e)
{
OpenFileDialog fileChooser = new OpenFileDialog();
DialogResult result = fileChooser.ShowDialog();
if (result == DialogResult.Cancel)
return;
openTextBox.Text = fileChooser.FileName;
}
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog fileChooser = new SaveFileDialog();
DialogResult result = fileChooser.ShowDialog();
fileChooser.CheckFileExists = false;
if (result == DialogResult.Cancel)
return;
saveTextBox.Text= fileChooser.FileName;
}
private void EnButton_Click(object sender, EventArgs e)
{
if (openTextBox.Text == "" || saveTextBox.Text == "" || pwsTextBox.Text == "")
return;
Encrypt(openTextBox.Text, saveTextBox.Text, pwsTextBox.Text);
openTextBox.Text = "";
saveTextBox.Text = "";
pwsTextBox.Text = "";
}
private void DeButton_Click(object sender, EventArgs e)
{
if (openTextBox.Text == "" || saveTextBox.Text == "" || pwsTextBox.Text == "")
return;
Decrypt(openTextBox.Text, saveTextBox.Text, pwsTextBox.Text);
openTextBox.Text = "";
saveTextBox.Text = "";
pwsTextBox.Text = "";
}
public void Encrypt(string pathIn, string pathOut, string password)
{
try
{
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
outfs.SetLength(0);
byte[] myBytes = new byte[100];
long myInLength = 0;
long myLength = fs.Length;
System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
while (myInLength < myLength)
{
int myLen = fs.Read(myBytes, 0, 100);
cs.Write(myBytes, 0, myLen);
myInLength += myLen;
}
cs.Close();
fs.Close();
outfs.Close();
}
catch (IOException ioex)
{
// Log.debug("Encrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
}
}
public void Decrypt(string pathIn, string pathOut, string password)
{
try
{
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
outfs.SetLength(0);
long myLength = fs.Length;
long myInLength = 0;
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);
byte[] myBytes = new byte[100];
while (myInLength < myLength)
{
int myLen = fs.Read(myBytes, 0, 100);
cs.Write(myBytes, 0, myLen);
myInLength += myLen;
}
cs.Close();
fs.Close();
outfs.Close();
}
catch (IOException ioex)
{
// Log.debug("Dncrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -