如何用存根包装我的构建器?

时间:2016-09-20 19:24:07

标签: c# .net encryption stub

我目前正在学习加密器,这是我迄今为止所学到的。

一个crypter由一个构建器和一个存根组成。

构建器角色是加密文件,而存根包装文件 并使其在一个缓冲区中运行,也就是在机器的内存中进行解密。 (如果我错了,请纠正)

我已经创建了我的文件加密器(构建器)并且说实话我不知道如何创建存根..我一直在寻找整天,但我能找到的只是这些非常旧的控制台应用程序没有真正解释任何事情..

所以我的问题是...... 如何使用存根包装当前文件加密器..或者如何创建存根。因为我不熟悉存根,所以不确定如何形成这个问题。

这是我的文件加密器。

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();

        }
    }
}

2 个答案:

答案 0 :(得分:0)

存根可以像一个小项目,加密部分作为资源。当加载存根时,它将从资源中描述程序集并使用反射来查找主要入口点&#39;。问题是,如何保存私钥....呃...私有..

答案 1 :(得分:0)

我没有听说过在这种情况下使用的存根。我只是在测试或添加“占位符”API方面听到它。

我认为您要求的是如何让构建器使用可以包装文件或包装内存流的接口?如果是这样,您的加密方法可以接受接口而不是字符串。该接口可以提供用于读取和写入数据的API。当您调用encrypt方法时,您可以新建接口的文件实现或内存实现,并将其传递给encrypt

由于您正在处理encrypt方法中的接口,因此它将平等对待这两个对象。

或者,您也可以接受Stream。然后,您可以传递MemoryStreamFileStream