我正在尝试在 C# 中创建一个具有不可导出私有的自签名证书,用于加密/解密对称密钥。
现在我最初的尝试是使用充气城堡,另见上一篇文章。现在这行不通,因为我没有发现将私钥明确设置为不可导出的任何运气。
继续前进,我发现了以下堆栈溢出帖子。还在这篇文章的底部发布了我从那里得到的代码。
出于我的目的,我将删除扩展密钥使用部分,将 X509PrivateKeyExportFlags 更改为不可导出并将 X509CertificateEnrollmentContext 更改为 CurrentUser Context。但是,当我这样做时,我得到:
CertEnroll::CX509CertificateRequestCertificate::InitializeFromPrivateKey: 参数不正确。0x80070057 (WIN32: 87 ERROR_INVALID_PARAMETER)
希望有人能帮助我。
删除旧代码 - 请参阅下面的当前版本
编辑:当前状态
public static X509Certificate2 CreateSelfSignedCertificate(string subjectName)
{
// create DN for subject and issuer
var dn = new CX500DistinguishedName();
dn.Encode("CN=" + subjectName, X500NameFlags.XCN_CERT_NAME_STR_NONE);
// create a new private key for the certificate
CX509PrivateKey privateKey = new CX509PrivateKey();
privateKey.ProviderName = "Microsoft Base Cryptographic Provider v1.0";
privateKey.MachineContext = false;
privateKey.Length = 2048;
privateKey.KeySpec = X509KeySpec.XCN_AT_KEYEXCHANGE; // use is not limited
privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_NONE;
privateKey.Create();
// Use the stronger SHA512 hashing algorithm
var hashobj = new CObjectId();
hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY,
AlgorithmFlags.AlgorithmFlagsNone, "SHA256");
// Create the self signing request
var cert = new CX509CertificateRequestCertificate();
cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, "");
cert.Subject = dn;
cert.Issuer = dn; // the issuer and the subject are the same
cert.NotBefore = DateTime.Now;
// this cert expires immediately. Change to whatever makes sense for you
cert.NotAfter = cert.NotBefore.AddYears(5);
cert.HashAlgorithm = hashobj; // Specify the hashing algorithm
cert.Encode(); // encode the certificate
// Do the final enrollment process
var enroll = new CX509Enrollment();
enroll.InitializeFromRequest(cert); // load the certificate
enroll.CertificateFriendlyName = subjectName; // Optional: add a friendly name
string csr = enroll.CreateRequest(); // Output the request in base64
// and install it back as the response
enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate,
csr, EncodingType.XCN_CRYPT_STRING_BASE64, ""); // no password
// output a base64 encoded PKCS#12 so we can import it back to the .Net security classes
var base64encoded = enroll.CreatePFX("", // no password, this is for internal consumption
PFXExportOptions.PFXExportChainWithRoot);
// instantiate the target class with the PKCS#12 data (and the empty password)
return new System.Security.Cryptography.X509Certificates.X509Certificate2(
System.Convert.FromBase64String(base64encoded), "",
// mark the private key as exportable (this is usually what you want to do)
System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable
);
}
将 KeySpec 更改为“AT_KeyEchange”后,我现在得到:
抛出异常:MyTool.exe 中的“System.Runtime.InteropServices.COMException”抛出异常:MyTool.exe 中的“System.NullReferenceException” CertEnroll::CX509PrivateKey::Create:指定的标志无效。0x80090009 (-2146893815 NTE_BAD_FLAGS)。
我更改了它,因为加密/解密不再起作用,认为这是因为 KeySet 模式,但现在我需要进行更多更改。我假设 hashobject 在这里可能不正确?它不应该像 SHA256withRSA 或类似的东西吗?(在我的充气城堡实现中,我使用了:“SHA256WithRSA”。
还是我错过了其他东西?