CertC#自签名证书问题
我试图在C#中创建一个自签名证书,该证书具有不可导出的私有密钥,用于加密/解密对称密钥 现在,我最初的尝试是使用bouncy castle,也请参见。现在,这是行不通的,因为我没有找到任何运气明确地把私钥非导出 继续前进,我发现。在这篇文章的底部也贴了我从那里得到的代码 出于我的目的,我将删除扩展密钥使用部分,将X509PrivateKeyExportFlags更改为不可导出,并将X509CertificateRollmentContext更改为CurrentUser上下文。但是,当我这样做时,我得到: CertEnroll::CX509CertificateRequestCertificate::InitializeFromPrivateKey:参数不正确。0x80070057(WIN32:87错误\u无效\u参数) 希望有人能帮我 删除旧代码-请参阅下面的当前版本 编辑:当前状态CertC#自签名证书问题,c#,x509certificate2,certenroll,C#,X509certificate2,Certenroll,我试图在C#中创建一个自签名证书,该证书具有不可导出的私有密钥,用于加密/解密对称密钥 现在,我最初的尝试是使用bouncy castle,也请参见。现在,这是行不通的,因为我没有找到任何运气明确地把私钥非导出 继续前进,我发现。在这篇文章的底部也贴了我从那里得到的代码 出于我的目的,我将删除扩展密钥使用部分,将X509PrivateKeyExportFlags更改为不可导出,并将X509CertificateRollmentContext更改为CurrentUser上下文。但是,当我这样做时,
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
);
}
将按键更改为“AT_KeyEchange”后,我现在得到:
引发异常:MyTool.exe中的“System.Runtime.InteropServices.COMException”
引发异常:MyTool.exe中的“System.NullReferenceException”
CertEnroll::CX509PrivateKey::Create:指定的标志无效。0x80090009(-2146893815 NTE_坏_标志)
我更改它是因为加密/解密不再工作,我认为这是因为密钥集模式,但现在我需要更改更多。我假设hashobject在这里可能不正确?它不应该像SHA256和RSA一样吗?(在我的bouncy castle实现中,我使用了:“SHA256WithRSA”
还是我遗漏了什么?发布的示例应该有效。是的,但如前所述,我想更改一些使其无效的内容。“出于我的目的,我会删除扩展密钥使用部分,将X509PrivateKeyExportFlags更改为不可导出,并将X509CertificateRollmentContext更改为CurrentUser上下文。但是,当我这样做时,我会得到:“当在
IX509CertificateRequestCertificate
接口调用中更改为用户上下文时,只需更新这一行:privateKey.MachineContext=true;
将true
更改为false
即可。感谢提示@Crypt32!现在它生成了证书。它按照我的要求存储在usestore中!(太棒了!)但是,当我尝试使用证书的pub/私钥加密然后解密数据时,我遇到以下错误:(解密/加密之前使用我使用bouncy castle创建的另一个证书进行了测试,但我无法将该证书设置为不可导出。)这可能是因为X509KeySpec.XCN_AT_签名;该标志?是的,很可能是KeySpec
的问题。出于加密目的,您需要使用AT_KEYEXCHANGE
值。