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

时间:2013-04-19 08:23:10

标签: 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);

        // 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_FLAG后,我监视了privatekey对象。我做错了什么?

0 个答案:

没有答案