如何用存根包装我的构建器? C#.NET [英] How do I wrap my builder with a stub? C# .NET

查看:51
本文介绍了如何用存根包装我的构建器? C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



加油器由一个建造者和一个存根组成。



构建器角色是加密文件,并且存根包装文件并使其在缓冲区中运行,该缓冲区也称为正在解密的机器的内存中。 (如果我错了,请更正)



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



所以我的问题是..怎么做我用一个存根包裹我当前的文件加密器..或者我如何创建一个存根。不知道如何形成这个问题,因为我不熟悉存根。



这是我的文件加密器。





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

;
使用Microsoft.Win32;
使用System.Security.Cryptography;
使用System.IO;

命名空间Encrypter
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
string key;
public MainWindow()
{
InitializeComponent();
key = generateKey();
}

公共字符串generateKey()
{
DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
返回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(文件已加密。,文件);

}
catch(异常加密)
{
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;
长度,位置= 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(对象发送者,RoutedEventArgs e)
{
尝试
{
OpenFileDialog ofd = new打开文件对话框();
ofd.ShowDialog();

inputdecryptFileTextBox.Text = ofd.FileName;

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

outputdecryptFileTextBox.Text = sfd.FileName;

decrypt(inputdecryptFileTextBox.Text,outputdecryptFileTextBox.Text,key);
MessageBox.Show(文件已被解密。,文件);
}
catch(exception ex)
{
MessageBox.Show(ex.ToString());
}
}



private void decrypt(字符串输入,字符串输出,字符串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;
长度,位置= 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();

}
}
}





我尝试过:



我试过在Google和一些论坛上查看一些例子,但我能找到的只是2009年的旧控制台应用程序。

解决方案

嗯,在您发布的代码中,构建器和存根基本上组合成加密和解密函数本身 - 例如,它们使用文件流在你的例子中,你会得到记忆 - 嘿,如果他们工作,那很好



一种接近建造者/存根的方法你提出的解决方案,就是加密/解密函数只接受(例如)一个字节数组..所以构建器的工作是获取一个字节数组 - 例如,从一个文件,然后呈现字节数组到存根,加密它/解密它



不确定我喜欢'builder'和'stub'的定义,比如t无论如何 - 我要做的是,进一步细分处理,以便你可以指定你希望在字节数组上执行什么类型的加密 - 你在这里使用的TripleDES或'AES'等等 - 所以你可能有一个TripleDES加密类 - 实际上,它可以接受FileStream,或ByteArray,或MemoryStream例如



不确定是否有任何waffle帮助

I'm currently learning about crypters and this is what I've learned so far.

A crypter consists of a builder and a stub.

The builders role is to encrypt a file and the stub wraps the file and makes it run in a buffer aka in the memory of the machine it is being decrypted on. (Please do correct if I am wrong)

I have created my File Encrypter (The builder) and to be honest I have no idea how to create a stub.. I've been looking around the entire day but all I can find are these really old Console applications not explaining anything really..

So my question is.. How do I wrap my current file encrypter with a stub.. or how do I create a stub. Not to sure how to form that question since I am new to stubs.

Here is my file encrypter.


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 Encrypter
{
    /// <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();

        }
    }
}



What I have tried:

I've tried looking over some examples on Google and some forums but all I could find was these old Console Applications from 2009.

解决方案

Well, in the code you've posted, the 'builder' and 'stub' are basically combined into the encrypt and decrypt functions themselves - they are using 'filestreams' for example which is as close you'll get to 'memory' in your example - hey, if they work, thats fine

One way to get closer to a builder/stub solution as you put it, would be for the encrypt/decrypt functions to accept (for example) a byte array only .. so the work of the builder is to get a byte array - from a file for example, then present the byte array to the stub, with encrypts it/decrypts it

not sure I like the definition of 'builder' and 'stub' like that anyway - what I would do, is break down the processing even further, so that you could specify what type of encryption you wish to perform on a byte array - TripleDES as you're using here or 'AES' or such - so you might have a TripleDES encrypt class - really, it could accept a FileStream, or a ByteArray, or a MemoryStream for example

not sure if any of that waffle helps


这篇关于如何用存根包装我的构建器? C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆