我试图在C#中创建一个具有不可导出私有的自签名证书,用于加密/解密对称密钥。
现在我最初的尝试是使用充气城堡,也见prev post。现在还没有成功,因为我没有找到任何运气,明确地将私钥放在不可出口的地方。
继续前进我找到the following stack overflow post。还发布了我在这篇文章底部得到的代码。
出于我的目的,我删除扩展密钥使用部分,将X509PrivateKeyExportFlags更改为不可导出并更改X509CertificateEnrollmentContext tot 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"我现在得到:
抛出异常:' System.Runtime.InteropServices.COMException'在MyTool.exe中 抛出异常:' System.NullReferenceException'在MyTool.exe中 CertEnroll :: CX509PrivateKey :: Create:指定了无效的标志。 0x80090009(-2146893815 NTE_BAD_FLAGS)。
我改变了它,因为加密/解密不再起作用,认为这是因为KeySet模式,但现在我需要更改。我假设这里的hashobject可能不正确?不应该像SHA256withRSA或类似的东西吗? (在我使用的充气城堡实施中:" SHA256WithRSA"。
或者我错过了其他什么?