2016-09-20 4 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を受け入れることもできます。その後、MemoryStreamまたはFileStreamを渡すことができます。

関連する問題