1

SO, in Visual C#.NET I would like it to somehow be able to taken in a program (through an open file dialog), then somehow take the bytes of that program and encrypt the bytes, to be executed later.

How would I do that? How would I encrypt, then later decrypt, a program using Visual C#.NET?

Alper
  • 1
  • 12
  • 39
  • 78
  • 1
    http://www.codeproject.com/KB/security/SimpleEncryption.aspx – Mitch Wheat Jun 11 '11 at 03:27
  • 2
    I would like to add that program (binary) data is stored and read in the exact same way as regular data, so you can encrypt and decrypt it in exactly the same way you would a regular file (txt, jpeg, etc). – Roadrunner-EX Jun 11 '11 at 03:41

1 Answers1

2

This answer shows you how to execute a byte array. One caution, this may cause problems with virus scanners because it is common in malware.

If you don't want to execute from memory, I whipped up an example of how you could encrypt store then decrypt and run an executable.

 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");
    }
 }
Community
  • 1
  • 1
ExCodeCowboy
  • 870
  • 7
  • 10
  • I edited this because I didn't like the example of file encryption that I linked to. This is a cleaner example. – ExCodeCowboy Jun 11 '11 at 04:56
  • Yeah, thanks, it was a bit confusing. I appreciate all your help. This is the exact answer I was looking for. – Alper Jun 11 '11 at 04:59