Har*_*h R 1 c# powershell cryptography x509certificate
我正在生成一个证书并将该证书加密为本地磁盘的 bin 文件。我正在使用以下 powershell 代码执行此操作:
$bytes = $CertificateResponse.ToCharArray() | % {[byte] $_}
[Byte[]]$entropy = 4,17,9,88,236,1688,1486,41,182,16,72,93,2,188,2,23
# Encrypt the byte array.
$encryptedBytes = [System.Security.Cryptography.ProtectedData]::Protect(
$bytes,
$entropy,
[System.Security.Cryptography.DataProtectionScope]::LocalMachine)
$encryptedBytes | Set-Content C:\certificate\xxx.bin -Encoding Byte
Run Code Online (Sandbox Code Playgroud)
后来相同的二进制文件保存在其他机器上,我正在尝试按如下方式读取和解密文件(使用 c#):
try
{
this.EntropyKey = new byte[] { 4,17,9,88,236,1688,1486,41,182,16,72,93,2,188,2,23};
var data = File.ReadAllBytes(Path.Combine(localPath, "xxx.bin"));
var decryptedData = ProtectedData.Unprotect(data, this.EntropyKey, DataProtectionScope.LocalMachine);
this.Certificate = new X509Certificate(decryptedData);
return Task.CompletedTask;
}
catch (Exception ex)
{
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我在尝试解密的行中收到异常为“密钥在指定状态下无效”。如果我尝试使用 powershell 命令解密,它工作正常。任何帮助将非常感激。注意:我已授予该文件的读取权限。
| 归档时间: |
|
| 查看次数: |
461 次 |
| 最近记录: |