如何使用C#加密另一个程序?
因此,在Visual C#.NET中,我希望它能够以某种方式接收程序(通过打开的文件对话框),然后以某种方式接收该程序的字节并加密这些字节,以便稍后执行 我该怎么做?如何使用Visual C#.NET对程序进行加密,然后再进行解密?演示了如何执行字节数组。需要注意的是,这可能会导致病毒扫描程序出现问题,因为这在恶意软件中很常见 如果您不想从内存执行,我举了一个示例,说明如何对存储进行加密,然后解密并运行一个可执行文件如何使用C#加密另一个程序?,c#,encryption,C#,Encryption,因此,在Visual C#.NET中,我希望它能够以某种方式接收程序(通过打开的文件对话框),然后以某种方式接收该程序的字节并加密这些字节,以便稍后执行 我该怎么做?如何使用Visual C#.NET对程序进行加密,然后再进行解密?演示了如何执行字节数组。需要注意的是,这可能会导致病毒扫描程序出现问题,因为这在恶意软件中很常见 如果您不想从内存执行,我举了一个示例,说明如何对存储进行加密,然后解密并运行一个可执行文件 public class FileEncryptRunner {
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");
}
}
我想补充的是,程序(二进制)数据的存储和读取方式与常规数据完全相同,因此您可以使用与常规文件(txt、jpeg等)完全相同的方式对其进行加密和解密。我之所以编辑此内容,是因为我不喜欢链接到的文件加密示例。这是一个更清晰的例子。是的,谢谢,有点混乱。我感谢你的帮助。这正是我想要的答案。