Beispiel #1
0
        private CX509CertificateRequestCertificate CreateRequest(KeyLengths keyLength)
        {
            var pk = new CX509PrivateKey
            {
                ProviderName = provName
            };
            var algId  = new CObjectId();
            var algVal = Oid.FromFriendlyName("RSA", OidGroup.PublicKeyAlgorithm);

            algId.InitializeFromValue(algVal.Value);
            pk.Algorithm      = algId;
            pk.KeySpec        = X509KeySpec.XCN_AT_KEYEXCHANGE; // If this value is anything other KEYEXCHANGE, the certificate cannot be used for decrypting content.
            pk.Length         = (int)keyLength;
            pk.MachineContext = true;
            pk.ExportPolicy   = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_NONE;
            pk.Create();

            var req    = new CX509CertificateRequestCertificate();
            var useCtx = (X509CertificateEnrollmentContext)StoreLocation.LocalMachine;

            req.InitializeFromPrivateKey(useCtx, pk, string.Empty);
            return(req);
        }
 public NewCertificate(string subject, string friendlyName, DateTime validUntil, HashAlgorithm hash, KeyLengths keyLength,
                       StoreLocation location)
 {
     SubjectName  = subject;
     FriendlyName = friendlyName;
     ValidUntil   = validUntil.ToUniversalTime();
     Algorithm    = hash.ToString();
     KeyLength    = (int)keyLength;
     Store        = location;
 }