Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建数字证书时出现强密钥保护错误_C#_Digital Certificate_Pfx - Fatal编程技术网

C# 创建数字证书时出现强密钥保护错误

C# 创建数字证书时出现强密钥保护错误,c#,digital-certificate,pfx,C#,Digital Certificate,Pfx,我正在使用此代码创建pfx: public static void createpfx(string password) { // create DN for subject var dnsubject = new CX500DistinguishedName(); dnsubject.Encode("CN=FileHasher", X500NameFlags.XCN_CERT_NAME_STR_NONE); // cr

我正在使用此代码创建pfx:

public static void createpfx(string password)
    {
        // create DN for subject 
        var dnsubject = new CX500DistinguishedName();
        dnsubject.Encode("CN=FileHasher", 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.ContainerName = "Hasher Private Key";
        privateKey.MachineContext = false;
        privateKey.Length = 2048;
        privateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE; // use is not limited
        privateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_ALL_USAGES; 
        privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
        privateKey.KeyProtection = X509PrivateKeyProtection.XCN_NCRYPT_UI_FORCE_HIGH_PROTECTION_FLAG;
        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, "SHA512");

        // Create the self signing request
        var cert = new CX509CertificateRequestCertificate();

        cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, "");
        cert.Subject = dnsubject;
        cert.Issuer = dnsubject; // the issuer and the subject are the same
        cert.NotBefore = new DateTime(2013,1,1);
        // this cert expires immediately. Change to whatever makes sense for you
        cert.NotAfter = new DateTime(2029, 12, 31);
        //cert.X509Extensions.Add((CX509Extension)eku); // add the EKU
        cert.HashAlgorithm = hashobj; // Specify the hashing algorithm
        cert.Encode(); // encode the certificate

        Console.WriteLine("cert rawdata: "+cert.RawData);

        // Do the final enrollment process
        var enroll = new CX509Enrollment();
        enroll.InitializeFromRequest(cert); // load the certificate
        enroll.CertificateFriendlyName = "File Hashing Certificate"; // Optional: add a friendly name
        enroll.CertificateDescription = "Signed Hasher Certificate";

        string csr = enroll.CreateRequest(); // Output the request in base64
        //Console.WriteLine("csr==="+csr);
        // and install it back as the response
        enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate,csr, EncodingType.XCN_CRYPT_STRING_BASE64, password); // no password
        // output a base64 encoded PKCS#12 so we can import it back to the .Net security classes
        var base64encoded = enroll.CreatePFX(password,PFXExportOptions.PFXExportChainWithRoot); // no password, this is for internal consumption
        //Console.WriteLine("base64==="+base64encoded);
        var fs = new System.IO.FileStream("hasher.pfx", System.IO.FileMode.Create);
        fs.Write(Convert.FromBase64String(base64encoded), 0, Convert.FromBase64String(base64encoded).Length);
        fs.Close();}

创建pfx时,它会提示输入密码,但当我的应用程序使用私钥时,它不会提示输入密码。我在输入密码密钥保护值更改为XCN_NCRYPT_UI_NO_PROTECTION_标志后监视privatekey对象。我做错了什么?

您能更清楚地说明您的要求吗?从您的代码中我可以看出,您将私钥保护设置为高。在这种情况下,你必须提供密码。我需要密码的强密钥保护。这样恶意软件就无法从存储中转储我的证书。我正在使用数字证书对文件哈希进行签名。privateKey.KeyProtection=X509PrivateKeyProtection.XCN_NCRYPT_UI_FORCE_HIGH_PROTECTION_标志;Create()//