?? 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 + -