2013-10-29 83 views
-2

我想寫一個代碼來使用公鑰加密文本並使用私鑰和密碼進行解密。如何使用打開文件對話框?

我不是很擅長編程語言,因爲我不是編程的學生。但對於我的迷你項目,我需要編寫一些關於加密的程序。

對於下面的代碼使用我的C盤中的文本文件使用公鑰進行編碼。 但我想使用openfiledialog來選擇文件,而不是手動指揮它(不太實際)

真的很感謝任何人都可以幫我編輯代碼。 P.S.我真的不知道如何將openfiledialog應用於我的代碼。當我使用youtubes和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) 
     { 

     } 
    } 
} 
+0

創建一個OpenFileDialog實例,並調用它的ShowDialog方法。你沒有嘗試。 –

+0

我與大衛,這是一個非常容易的話題谷歌。 – Gusdor

回答

4

假設您使用的是WinForms。

只需創建一個OpenFileDialog的實例,請致電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 (*.*)|*.*"; 

你甚至可以允許多個選擇,設置Multiselecttrue你會得到所有選定的文件在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); 
    // ... 
} 
0
private void decryptButton_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 

    openFileDialog1.InitialDirectory = "c:\\" ; 
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; 
    openFileDialog1.FilterIndex = 2 ; 
    openFileDialog1.RestoreDirectory = true ; 

    if(openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
    try 
      { 
       String plainString = pgp.DecryptString(encryptedtextTextBox.Text,new FileInfo(openFileDialog1.FileName), passphraseTextBox.Text); 
       decryptedtextTextBox.Text = plainString; 
       encryptedtextTextBox.Text = ""; 
       passphraseTextBox.Text = ""; 
      } 
      catch 
      { 
       MessageBox.Show("ERROR! Please check passphrase and do not attempt to edit cipher text"); 
      } 
    } 
}