資源簡介
C#編寫的文件加、解密,使用RSA算法實現(xiàn)。選擇本地文件后,對文件進(jìn)行加密,再對加密后的文件進(jìn)行解密。代碼在VS 2017下直接運行。(源代碼,C#實現(xiàn))

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?RSA文件加密
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????????
????????RSA?rSA?=?new?RSA();
????????public?static?string?PublicKey;
????????public?static?string?PrivateKey;
????????string?filePath?=?string.Empty;???//文件路徑
????????private?void?buttonBrowse_Click(object?sender?EventArgs?e)
????????{
????????????this.openFileDialogOne.Filter?=?“文本文件|*.txt“;
????????????if?(this.openFileDialogOne.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????try
????????????????{
????????????????????string?path?=?this.openFileDialogOne.FileName;
????????????????????filePath?=?path;
????????????????????this.textBoxFilePath.Text?=?path;
????????????????????this.richTextBoxValue.LoadFile(path?RichTextBoxStreamType.PlainText);
????????????????}
????????????????catch?(Exception?ex)
????????????????{
????????????????????throw?ex;
????????????????}
????????????}
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????if?(this.textBoxFilePath.Text.Length?>?0)
????????????????{
????????????????????PublicKey?=?rSA.GetPublicKey();
????????????????????PrivateKey?=?rSA.GetPrivateKey();
????????????????????string?strPath?=?this.textBoxFilePath.Text;//要加密文件絕對路徑
????????????????????string?folderPath?=?strPath.Substring(0?strPath.LastIndexOf(“\\“)?+?1);//根目錄
????????????????????string?fileName?=?strPath.Substring(strPath.LastIndexOf(“\\“)?+?1?strPath.LastIndexOf(“.“)?-?strPath.LastIndexOf(“\\“)?-?1);//文件名
????????????????????fileName?+=?“(加密).txt“;
????????????????????string?encryptedFilePath?=?folderPath?+?fileName;//加密后得到文件的絕對路徑
????????????????????rSA.Encrypt(strPath?encryptedFilePath?PublicKey);
????????????????????this.richTextBoxValue.LoadFile(encryptedFilePath?RichTextBoxStreamType.PlainText);
????????????????????this.textBoxFilePath.Text?=?encryptedFilePath;
????????????????????MessageBox.Show(“文件加密成功“?“提示“?MessageBoxButtons.OK?MessageBoxIcon.Information);
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.Message?“錯誤“?MessageBoxButtons.OK?MessageBoxIcon.Error);
????????????????return;
????????????}
????????}
????????private?void?btnDecrypt_Click(object?sender?EventArgs?e)
????????{
????????????string?strPath?=?this.textBoxFilePath.Text;//要加密文件絕對路徑
????????????string?folderPath?=?strPath.Substring(0?strPath.LastIndexOf(“\\“)?+?1);//根目錄
????????????string?fileName?=?strPath.Substring(strPath.LastIndexOf(“\\“)?+?1?strPath.LastIndexOf(“.“)?-?strPath.LastIndexOf(“\\“)?-?1);//文件名
????????????fileName?+=?“(解密).txt“;
????????????string?e
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1144??2018-12-20?20:20??RSA文件加密.sln
?????文件????????189??2018-12-20?20:20??RSA文件加密\App.config
?????文件??????11776??2018-12-21?17:03??RSA文件加密\bin\Debug\RSA文件加密.exe
?????文件????????189??2018-12-20?20:20??RSA文件加密\bin\Debug\RSA文件加密.exe.config
?????文件??????34304??2018-12-21?17:03??RSA文件加密\bin\Debug\RSA文件加密.pdb
?????文件???????3576??2018-12-21?17:04??RSA文件加密\Form1.cs
?????文件???????6781??2018-12-20?22:07??RSA文件加密\Form1.Designer.cs
?????文件???????6022??2018-12-20?22:07??RSA文件加密\Form1.resx
?????文件???????1443??2018-12-20?21:29??RSA文件加密\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7188??2018-12-20?20:22??RSA文件加密\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????42??2018-12-20?20:22??RSA文件加密\obj\Debug\RSA文件加密.csproj.CoreCompileInputs.cache
?????文件????????915??2018-12-20?20:28??RSA文件加密\obj\Debug\RSA文件加密.csproj.FileListAbsolute.txt
?????文件???????1012??2018-12-20?22:11??RSA文件加密\obj\Debug\RSA文件加密.csproj.GenerateResource.cache
?????文件??????11490??2018-12-20?20:20??RSA文件加密\obj\Debug\RSA文件加密.csprojAssemblyReference.cache
?????文件??????11776??2018-12-21?17:03??RSA文件加密\obj\Debug\RSA文件加密.exe
?????文件????????180??2018-12-20?22:11??RSA文件加密\obj\Debug\RSA文件加密.Form1.resources
?????文件??????34304??2018-12-21?17:03??RSA文件加密\obj\Debug\RSA文件加密.pdb
?????文件????????180??2018-12-20?20:28??RSA文件加密\obj\Debug\RSA文件加密.Properties.Resources.resources
?????文件??????????0??2018-12-20?20:20??RSA文件加密\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2018-12-20?20:20??RSA文件加密\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2018-12-20?20:20??RSA文件加密\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件????????527??2018-12-20?20:20??RSA文件加密\Program.cs
?????文件???????1328??2018-12-20?20:20??RSA文件加密\Properties\AssemblyInfo.cs
?????文件???????2843??2018-12-20?20:20??RSA文件加密\Properties\Resources.Designer.cs
?????文件???????5612??2018-12-20?20:20??RSA文件加密\Properties\Resources.resx
?????文件???????1102??2018-12-20?20:20??RSA文件加密\Properties\Settings.Designer.cs
?????文件????????249??2018-12-20?20:20??RSA文件加密\Properties\Settings.settings
?????文件???????2783??2018-12-20?22:31??RSA文件加密\RSA.cs
?????文件???????3652??2018-12-20?20:28??RSA文件加密\RSA文件加密.csproj
?????目錄??????????0??2018-12-20?20:20??RSA文件加密\obj\Debug\TempPE
............此處省略9個文件信息
評論
共有 條評論