我正在尝试在 Windows 10 中保护证书的私钥,但看起来我误解了“管理私钥”的作用。
这是我遵循的过程:
编辑 - 今天早上我尝试重复整个过程,现在我得到了预期的错误。请看下面我的回答。
- 启动 powershell(作为管理员),创建一个自签名证书并将其安装在 localmachine 个人存储中:
New-SelfSignedCertificate -DnsName test.pfx -CertStoreLocation "cert:\LocalMachine\My" -NotAfter (Get-Date).AddDays(7)
跑
certlm.msc
找到
test.pfx
证书,右键单击它并选择all tasks > manage private keys
从列表中删除所有人,包括管理员,因此您会收到以下消息:“没有组或用户有权访问此对象。但是,此对象的所有者可以分配权限。” 并单击确定。
双击证书,转到详细信息选项卡,从下拉列表中选择“仅限属性”,单击指纹,然后将指纹复制并粘贴到记事本中
打开Visual Studio(作为普通用户,而不是管理员,即不同的帐户)并创建一个新的控制台应用程序,并输入以下代码:
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
const string thumbprint = "<THUMBPRINT>";
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (certs.Count != 1)
throw new Exception("certificate count <> 1");
var cert = certs[0];
// why doesn't this fail?
var privateKey = cert.GetRSAPrivateKey();
privateKey.Dispose();
}
}
}
}
从记事本复制/粘贴指纹以替换代码
启动代码
我期待 GetRSAPrivateKey() 失败,但事实并非如此。(如果我添加更多代码,我也可以使用私钥解密内容)。
我还查看了 %ALLUSERSPROFILE%\Application Data\Microsoft\Crypto\Keys 并且最近修改的私钥“文件”的权限符合预期(即任何人都无法访问)。