📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace EncryptFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSelFile_Click(object sender, EventArgs e)
{
if (openFile.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = openFile.FileName;
}
}
private void btnEncFile_Click(object sender, EventArgs e)
{
// After the user chose where he wants the key file saved
if (saveKeyFile.ShowDialog() == DialogResult.OK)
{
// And after the user chose where he wants the encrypted file saved
if (saveEncFile.ShowDialog() == DialogResult.OK)
{
FileStream fsFileOut = File.Create(saveEncFile.FileName);
// The chryptographic service provider we're going to use
TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
// This object links data streams to cryptographic values
CryptoStream csEncrypt = new CryptoStream(fsFileOut, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
// This stream writer will write the new file
StreamWriter swEncStream = new StreamWriter(csEncrypt);
// This stream reader will read the file to encrypt
StreamReader srFile = new StreamReader(txtFilePath.Text);
// Loop through the file to encrypt, line by line
string currLine = srFile.ReadLine();
while (currLine != null)
{
// Write to the encryption stream
swEncStream.Write(currLine);
currLine = srFile.ReadLine();
}
// Wrap things up
srFile.Close();
swEncStream.Flush();
swEncStream.Close();
// Create the key file
FileStream fsFileKey = File.Create(saveKeyFile.FileName);
BinaryWriter bwFile = new BinaryWriter(fsFileKey);
bwFile.Write(cryptAlgorithm.Key);
bwFile.Write(cryptAlgorithm.IV);
bwFile.Flush();
bwFile.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -