我正在尝试编写代码来使用公钥加密文本并使用私钥和密码进行解密。
我不太擅长编程语言,因为我不是编程学生。但对于我的小型项目,我需要编写一些有关加密的程序。
对于下面的代码,使用我的 C 驱动器中的文本文件来使用公钥进行编码。
但我想使用 openfiledialog 来选择文件而不是手动引导它(不太实用)
如果有人能帮助我编辑代码,我真的很感激。
附:我真的不知道如何将 openfiledialog 应用于我的代码。当我使用来自 youtube 和 google 的信息时,我不断收到错误。
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;
using System.IO;
using DidiSoft.Pgp;
namespace TEST2
{
public partial class Form1 : Form
{
PGPLib pgp = new PGPLib();
public Form1()
{
InitializeComponent();
}
private void encryptButton_Click(object sender, EventArgs e)
{
string testingstring = pgp.EncryptString(testTextBox.Text, new FileInfo(@"c:\TCkeyPublic.txt"));
encryptedtextTextBox.Text = testingstring;
}
private void decryptButton_Click(object sender, EventArgs e)
{
try
{
String plainString = pgp.DecryptString(encryptedtextTextBox.Text,
new FileInfo(@"c:\TCkeyPrivate.txt"), passphraseTextBox.Text);
decryptedtextTextBox.Text = plainString;
encryptedtextTextBox.Text = "";
passphraseTextBox.Text = "";
}
catch
{
MessageBox.Show("ERROR! Please check passphrase and do not attempt to edit cipher text");
}
}
private void passphraseTextBox_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
假设您使用的是 WinForms。
只需创建一个实例OpenFileDialog
, call ShowDialog
如果用户没有取消操作,则读取FileName
属性:它将包含所选文件的完整路径。在代码中:
var dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK)
return;
new FileInfo(dlg.FileName, passphraseTextBox.Text);
当然你可能需要让用户快速过滤要显示的文件,你可以使用Filter
财产:
var dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
您甚至可以允许多项选择,设置Multiselect
to true
您将获得所有选定的文件FileNames
财产:
var dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
dlg.Multiselect = true;
if (dlg.ShowDialog() != DialogResult.OK)
return;
foreach (var path in dlg.FileNames)
{
new FileInfo(path, passphraseTextBox.Text);
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)