首先,我建议您将证书保存在证书存储中,并将私钥保存为不可导出。现在有些争论。
有多种方法可以在机器上保存私人秘密或其他私人信息。最古老的方法是LsaStorePrivateData
API LsaRetrievePrivateData
(参见http://msdn.microsoft.com/en-us/library/ms721818%28VS.85%29.aspx)。它对秘密的数量有限制,但所有秘密都可以分为本地、全局和机器。
下一个方法是使用 DPAPI(参见http://msdn.microsoft.com/en-us/library/ms995355.aspx):在我们的例子中CryptProtectData
。CryptUnprotectData
我添加了对这两种方式的引用,因为您想比较不同的可能方式,以确保您的方式最适合您的任务。
我认为您应该问的最重要的问题是:保护我的私钥的最佳方法是什么?我认为您应该选择保护您的密钥被复制的方式。所以我建议你使用证书存储。在证书存储中,您可以持有标记为non exportable的私钥。这是我认为的主要优势。您可以通过不同的方式部署具有相应私钥的证书。请确保,保存在机器上的私钥未标记为可导出。
Using of PFX file on disk gives you not this advantage. Moreover either your PFX is not encrypted or you receive a problem where you should save the password to the PFX file. So you have to use DPAPI (CryptProtectData
and CryptUnprotectData
) or LSA API (LsaStorePrivateData
and LsaRetrievePrivateData
) and the password can be exported.