【问题标题】:How do I use C# to encrypt another program?如何使用 C# 加密另一个程序?
【发布时间】:2011-06-11 03:24:08
【问题描述】:

所以,在 Visual C#.NET 中,我希望它能够以某种方式接收程序(通过打开的文件对话框),然后以某种方式获取该程序的字节并加密字节,以便稍后执行。

我该怎么做?如何使用 Visual C#.NET 加密然后解密程序?

【问题讨论】:

  • 我想补充一点,程序(二进制)数据的存储和读取方式与常规数据完全相同,因此您可以以与常规文件完全相同的方式对其进行加密和解密( txt、jpeg 等)。

标签: c# encryption


【解决方案1】:

This answer 向您展示如何执行字节数组。需要注意的是,这可能会导致病毒扫描程序出现问题,因为它在恶意软件中很常见。

如果您不想从内存中执行,我提供了一个示例,说明如何加密存储然后解密并运行可执行文件。

 public class FileEncryptRunner
 {
    Byte[] key = ASCIIEncoding.ASCII.GetBytes("thisisakeyzzzzzz");
    Byte[] IV = ASCIIEncoding.ASCII.GetBytes("thisisadeltazzzz");

    public void SaveEncryptedFile(string sourceFileName)
    {
       using (FileStream fStream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read),
              outFStream = new FileStream(Environment.SpecialFolder.MyDocuments+"test.crp", FileMode.Create))
       {
          Rijndael RijndaelAlg = Rijndael.Create();
          using (CryptoStream cStream = new CryptoStream(outFStream, RijndaelAlg.CreateEncryptor(key, IV), CryptoStreamMode.Write))
          {
              StreamWriter sWriter = new StreamWriter(cStream);
              fStream.CopyTo(cStream);
          }
       }
    }

    public void ExecuteEncrypted()
    {
       using (FileStream fStream = new FileStream(Environment.SpecialFolder.MyDocuments + "test.crp", FileMode.Open, FileAccess.Read, FileShare.Read),
              outFStream = new FileStream(Environment.SpecialFolder.MyDocuments + "crpTemp.exe", FileMode.Create))
       {
          Rijndael RijndaelAlg = Rijndael.Create();
          using (CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateDecryptor(key, IV), CryptoStreamMode.Read))
          {   //Here you have a choice. If you want it to only ever exist decrypted in memory then you have to use the method in
              // the linked answer.
              //If you want to run it from a file than it's easy and you save the file and run it, this is simple.                                               
              cStream.CopyTo(outFStream);
          }
       }
       Process.Start(Environment.SpecialFolder.MyDocuments + "crpTemp.exe");
    }
 }

【讨论】:

  • 我编辑了这个,因为我不喜欢我链接到的文件加密示例。这是一个更简洁的示例。
  • 是的,谢谢,这有点令人困惑。我感谢您的所有帮助。这是我正在寻找的确切答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2011-10-08
  • 2019-11-09
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 2022-07-09
相关资源
最近更新 更多