【发布时间】:2015-10-19 08:22:45
【问题描述】:
这是我目前的情况 - 我有一个应用程序,它使用 CodeDom 编译作为字符串接收的 C# 代码。我有一个存储密码的 SecureString,我想知道是否有任何方法可以将该 SecureString 变量作为 SecureString 传递到编译后的代码中?
下面是一些示例代码:
SecureString securePassword = getSecurePass();
string codeString =
@"using System;
using System.Security;
namespace SomeProgram
{
class MyClass
{
static void Main(string[] args)
{
SecureString securePass = new SecureString();
// somehow set this equal to the securePassword variable
}
}
}";
// Compiler Code
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string outFile = "output.exe";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = outFile;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, codeString);
我找不到这样做的方法,我想这实际上是不可能的,而是我应该将密码存储在加密文件中并从中读取?
【问题讨论】:
-
您正在编写一个 exe,因此您无论如何都希望将密码以序列化形式嵌入其中。这不像是在为正在运行的程序提供价值。
-
(adding a resource 可以方便地完成包含加密 blob 的程序集)。
-
@GSerg 这是一个天才的想法,是内置在实际 EXE 中的资源还是单独存储(在单独的文件中),因为如果它们单独存储,那么我不妨加密字符串并输出到txt文件?
-
Yes they do,因为他们是
EmbeddedResources。还有LinkedResources。
标签: c# compiler-construction codedom