我使用 Start-Process 来启动进程,该进程也启动进程:
$p = Start-Process "ProcessRunner.exe" -ArgumentList "notepad.exe" -NoNewWindow -Wait -PassThru
$exitCode = $p.ExitCode
Run Code Online (Sandbox Code Playgroud)
ProcessRunner(父进程)启动ArgumentList(“notepad.exe”-子进程)中指定的应用程序,然后使用脚本另一部分中使用的某些特定 exitCode 退出。
在 powershell 2.0 中,这按我的预期工作:即使子进程继续执行,脚本也会在 ProcessRunner 退出后立即继续。
在 powershell 4.0 中,脚本执行会暂停,直到 notepad.exe 退出。
当子进程仍在运行时(即ps2的行为),是否可以获取(在ps4中)父进程的exitCode?
编辑:可用于重现问题的简单 ProcessRunner:
static int Main(string[] args)
{
int result;
try
{
Process prc = new Process();
prc.StartInfo.FileName = args[0];
result = prc.Start() ? 0 : 1;
}
catch (Exception e)
{
result = -1;
}
Console.WriteLine("ExitCode = {0}", result);
return result;
}
Run Code Online (Sandbox Code Playgroud)
使用$p.WaitForExit()
代替会-Wait …
我正在尝试使用ECDSA生成带有私钥的(自签名)证书.目标是获得与使用openssl时"相同"(pkcs12)的证书:
openssl ecparam -genkey -name secp256r1 -out mykey.key
openssl req -new -key mykey.key -out myreq.csr
openssl req -x509 -days 7 -key mykey.key -in myreq.csr -out mycert.crt
openssl pkcs12 -export -out mycert.pfx -inkey mykey.key -in mycert.crt
Run Code Online (Sandbox Code Playgroud)
我已经使用BouncyCastle来帮助我创建基于RSA的证书,因此接下来的步骤或多或少都遵循我用来创建RSA证书的方式.
(请注意,BC
前缀用于BouncyCastle的类,MS
适用于.NET类)
1生成密钥对:私钥和公钥
BC.IAsymmetricCipherKeyPairGenerator bcKpGen = BC.GeneratorUtilities.GetKeyPairGenerator("ECDSA");
bcKpGen.Init(new BC.ECKeyGenerationParameters(BC.SecObjectIdentifiers.SecP256r1, new BC.SecureRandom()));
BC.AsymmetricCipherKeyPair bcSubjKeys = bcKpGen.GenerateKeyPair();
Run Code Online (Sandbox Code Playgroud)
2使用私钥对公钥进行签名,并附加一些额外数据(主题,有效期等)
BC.X509V3CertificateGenerator bcXgen = new BC.X509V3CertificateGenerator();
// .. set subject, validity period etc
bcXgen.SetPublicKey(bcSubjKeys.Public);
BC.ISignatureFactory bcSigFac = new BC.Asn1SignatureFactory("SHA256WITHECDSA", bcSubjKeys.Private);
BC.X509Certificate bcCert = bcXgen.Generate(bcSigFac); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成 ECDSA 自签名证书,如使用 ECDSA 生成证书中所述。将 bartonjs 的答案中的所有部分放在一起并使用Net.Framework 4.7
(或Net.Core 2.0
) 以下代码似乎有效,尽管还存在一些歧义(至少一个):
我不确定如何正确地将私钥(“D”参数)从 转换BC-BigInteger
为MS-byte[]
. 使用BigInteger.ToByteArray()
抛出异常:
CryptographicException:指定的关键参数无效。QX 和 QY 是必填字段。QX、QY 的长度必须相同。如果指定了 D,则它必须与命名曲线的 QX 和 QY 长度相同,或者与显式曲线的 Order 长度相同。
同时验证 ECParameters(方法ECParameters.Validate()
)。使用BigInteger.ToByteArrayUnsigned()
提供了更好的结果(数百个生成的密钥对中的一个失败),但仍然......
使用ToByteArray()
转换后的“D”通常会长一个字节(“D”有 33 个字节,而 DX 和 DY 有 32 个字节)。使用ToByteArrayUnsigned()
“D”有时会短一个字节。
所以我的问题是是否可以使用ToByteArrayUnsigned()
。
private const string NCryptExportPolicyProperty = "Export Policy";
private const string SignatureAlgorithm = "Sha256WithECDSA";
private static readonly ECCurve MsCurve = ECCurve.NamedCurves.nistP256; …
Run Code Online (Sandbox Code Playgroud)