当我传递string给一个函数时,是一个指向传递给字符串内容的指针,还是整个字符串传递给堆栈上的函数就像struct是?
这是我目前的情况 - 我有一个应用程序使用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);
Run Code Online (Sandbox Code Playgroud)
我找不到办法做到这一点,我想这实际上是不可能的,而我可能只是将密码存储在加密文件中并从中读取?
我们有一个IIS WCF服务,它以另一个用户的身份启动另一个进程(app.exe).我完全控制了两个应用程序(现在这是一个开发环境).IIS应用程序池以我的身份运行,域用户(DOMAIN \nirvin),也是该框的本地管理员.第二个进程应该作为本地用户运行(svc-low).我正在System.Diagnostics.Process.Start(ProcessStartInfo)用来启动这个过程.该过程成功启动 - 我知道因为没有抛出异常,我得到了一个进程ID.但是进程立即死亡,我在事件日志中看到如下错误:
错误应用程序名称:app.exe,版本:1.0.3.0,时间戳:0x514cd763
错误模块名称:KERNELBASE.dll,版本:6.2.9200.16451,时间戳:0x50988aa6
异常代码:0xc06d007e
故障偏移:0x000000000003811c
错误进程id:0x10a4
错误应用程序启动时间:0x01ce274b3c83d62d
错误的应用程序路径:C:\ Program Files\company\app\app.exe
错误模块路径:C:\ Windows\system32\KERNELBASE.dll
报告编号:7a45cd1c-933e-11e2-93f8-005056b316dd
错误包全名:
错误包相关的应用程序ID:
我已经在app.exe(现在)中进行了非常彻底的日志记录,因此我认为它不会在.NET代码中引发错误(不再).
这是真正令人讨厌的部分:我认为我只是启动了错误的进程,所以我Process.Start()在一个愚蠢的WinForms应用程序中复制了我的调用并在机器上像我一样运行它,希望能够修改,直到我得到正确的参数.因此,当然这是第一次和每次都有效:我能够始终如一地启动第二个流程并使其按预期运行.它只从IIS启动不起作用.
我已经尝试过"以批处理作业登录"的svc-low权限,并且我已尝试授予自己"替换进程级别令牌"(在本地安全策略中)的权限,但似乎都没有任何区别.
救命!
起初app.exe是一个控制台应用程序.尝试启动是让conhost.exe在事件日志中生成错误,因此我将app.exe切换为Windows应用程序.这让conhost脱离了这个等式,但是留给我这里描述的情况.(通过这个问题引导了这条道路.)
ProcessStartInfo我使用的对象如下所示:
new ProcessStartInfo
{
FileName = fileName,
Arguments = allArguments,
Domain = domainName,
UserName = userName,
Password = securePassword,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = false
//LoadUserProfile = true //I've done it with and without this set …Run Code Online (Sandbox Code Playgroud) 我怎样才能获得byte[]相当于a SecureString(我从中得到的PasswordBox)?
我的目标是使用a将这些字节写入CryptoStream文件,并且Write该类的方法接受byte[]输入,因此我想将其转换SecureString为byte[]所以我可以使用a CryptoStream.
编辑:我不想使用,string因为它打败了一个点SecureString
假设我需要在Powershell中执行此操作:
$SecurePass = Get-Content $CredPath | ConvertTo-SecureString -Key (1..16)
[String]$CleartextPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($CredPass));
Run Code Online (Sandbox Code Playgroud)
$ CredPath的内容是一个包含ConvertFrom-SecureString -Key(1..16)输出的文件.
如何ConvertTo-SecureString -key (1..16)在C#/ .NET中完成该部分?
我知道如何创建SecureString,但我不确定应该如何处理加密.
我是否使用AES加密每个字符,或者解密字符串然后为每个字符创建一个安全字符串?
我对密码学几乎一无所知,但从我收集的内容中我可能只想使用C#调用Powershell命令.
作为参考,我在这里发现了类似的关于AES加密/解密的帖子: 在C#中使用AES加密
UPDATE
我已经回顾了Keith发布的链接,但我还面临其他未知数.DecryptStringFromBytes_Aes有三个参数:
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
Run Code Online (Sandbox Code Playgroud)
第一个参数是字节数组表示加密文本.这里的问题是,如何在字节数组中表示字符串?它应该用或不用编码表示?
byte[] ciphertext = Encoding.ASCII.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.UTF8.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.Unicode.GetBytes(encrypted_text);
byte[] ciphertext = new byte[encrypted_password.Length * sizeof(char)];
System.Buffer.BlockCopy(encrypted_password.ToCharArray(), 0, text, 0, text.Length);
Run Code Online (Sandbox Code Playgroud)
第二个字节数组的键应该只是一个整数数组:
byte[] key = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
Run Code Online (Sandbox Code Playgroud)
第三个字节数组是"初始化向量" - 看起来Aes.Create()调用将随机生成一个用于IV的byte [].阅读,我发现我可能需要使用相同的IV.由于ConvertFrom-SecureString和ConvertTo-SecureString能够使用简单的密钥加密/解密,因此我假设IV []可以是随机的 - 或者 - 具有静态定义. …
我相信我误解了一个基本部分SecureString.我知道这string是不可变的,并且在堆上找到密码或敏感数据作为明文.
我正在努力理解的是,如何SecureString在需要验证数据库中哈希密码的客户端应用程序中使用?
这是我的背景:
SecureString通过SecurePassword属性将密码存储在a中.怎么办?如何SecureString首先将WITHOUT重新转换为字符串?
到目前为止我收到的所有建议都是编写扩展方法转换SecureString为String,哈希,然后将其发送到db进行验证.但这打败了整个演习!
我必须接受SecureString在我提到的上下文中没用的东西并使用普通的string吗?
c# ×6
.net ×3
encryption ×2
securestring ×2
wpf ×2
aes ×1
codedom ×1
function ×1
hash ×1
linq-to-sql ×1
linqpad ×1
powershell ×1
process ×1
sql-server ×1
string ×1