2016-09-20 20 views
1

我目前正在學習密碼學,這是我迄今爲止學到的。如何用存根包裹我的生成器?

一個計算器由一個建造者和一個存根組成。

構建者的作用是加密一個文件,存根包裝文件 並使其運行在正在解密的機器的內存緩衝區中。 (請糾正,如果我錯了)

我已經創建了我的文件加密器(生成器),說實話我不知道如何創建一個存根。我一直在尋找整個一天,但所有我能看到的是,這些真的是老了控制檯應用程序不解釋真的什麼..

所以我的問題是... 如何換我當前的文件加密與存根..或如何創建一個存根。不確定如何形成這個問題,因爲我對存根很陌生。

這是我的文件加密器。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

using System.Windows; 
using Microsoft.Win32; 
using System.Security.Cryptography; 
using System.IO; 

namespace FileEncrypter 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     string key; 
     public MainWindow() 
     { 
      InitializeComponent(); 
      key = generateKey(); 
     } 

     public string generateKey() 
     { 
      DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); 
      return ASCIIEncoding.ASCII.GetString(desCrypto.Key); 
     } 

     private void EncryptBtn_Click(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       OpenFileDialog ofd = new OpenFileDialog(); 
       ofd.ShowDialog(); 

       inputencryptFileTextBox.Text = ofd.FileName; 

       SaveFileDialog sfd = new SaveFileDialog(); 
       sfd.ShowDialog(); 

       outputencryptFileTextBox.Text = sfd.FileName; 

       encrypt(inputencryptFileTextBox.Text, outputencryptFileTextBox.Text, key); 
       MessageBox.Show("File has been encrypted.", "File"); 

      } 
      catch(Exception encEx) 
      { 
       MessageBox.Show(encEx.ToString()); 
      } 

     } 

     private void encrypt(string input, string output, string strhash) 
     { 
      FileStream inFs, outFs; 
      CryptoStream cs; 
      TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider(); 
      MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 

      byte[] byteHash, byteTexto; 

      inFs = new FileStream(input, FileMode.Open, FileAccess.Read); 
      outFs = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write); 

      byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strhash)); 
      byteTexto = File.ReadAllBytes(input); 

      md5.Clear(); 

      TDC.Key = byteHash; 
      TDC.Mode = CipherMode.ECB; 

      cs = new CryptoStream(outFs, TDC.CreateEncryptor(), CryptoStreamMode.Write); 

      int byteRead; 
      long length, position = 0; 
      length = inFs.Length; 

      while (position < length) 
      { 
       byteRead = inFs.Read(byteTexto, 0, byteTexto.Length); 
       position += byteRead; 

       cs.Write(byteTexto, 0, byteRead); 

      } 

      inFs.Close(); 
      outFs.Close(); 

     } 

     private void DecryptBtn_Click(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       OpenFileDialog ofd = new OpenFileDialog(); 
       ofd.ShowDialog(); 

       inputdecryptFileTextBox.Text = ofd.FileName; 

       SaveFileDialog sfd = new SaveFileDialog(); 
       sfd.ShowDialog(); 

       outputdecryptFileTextBox.Text = sfd.FileName; 

       decrypt(inputdecryptFileTextBox.Text, outputdecryptFileTextBox.Text, key); 
       MessageBox.Show("File has been decrypted.", "File"); 
      } 
      catch(Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 



     private void decrypt(string input, string output, string strhash) 
     { 
      FileStream inFs, outFs; 
      CryptoStream cs; 
      TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider(); 
      MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 

      byte[] byteHash, byteTexto; 

      inFs = new FileStream(input, FileMode.Open, FileAccess.Read); 
      outFs = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write); 

      byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strhash)); 
      byteTexto = File.ReadAllBytes(input); 

      md5.Clear(); 

      TDC.Key = byteHash; 
      TDC.Mode = CipherMode.ECB; 

      cs = new CryptoStream(outFs, TDC.CreateDecryptor(), CryptoStreamMode.Write); 

      int byteRead; 
      long length, position = 0; 
      length = inFs.Length; 

      while (position < length) 
      { 
       byteRead = inFs.Read(byteTexto, 0, byteTexto.Length); 
       position += byteRead; 

       cs.Write(byteTexto, 0, byteRead); 

      } 

      inFs.Close(); 
      outFs.Close(); 

     } 
    } 
} 

回答

0

存根可以像一個小型項目,加密部分作爲資源。當存根被加載時,它將從資源中描述程序集並使用反射來查找「主入口點」。問題是,如何保持私鑰....呃...私人..

0

我還沒有聽說過在這種情況下使用的存根。我只聽到它的測試或添加「佔位符」API。

我想你所要求的是如何讓建設者使用接口,可以包裝文件或包裝內存流?如果是這樣,你的加密方法可以接受一個接口而不是一個字符串。該接口可以提供讀取和寫入數據的API。在調用encrypt方法時,可以新建接口的文件實現或內存實現,並將其傳遞到encrypt

由於您正在處理encrypt方法中的接口,因此它將同等對待兩個對象。您也可以只接受Stream。然後你可以通過MemoryStreamFileStream