I would like to know how to generate a digital certificate request

Asked

Viewed 17 times

0

Below is the code I tried, but as I never did, it is not working.

            var subjectName = CN;

            // Create new Object for Issuer and Subject
            var issuer = new X509Name(subjectName);
            var subject = new X509Name(subjectName);

            // Generate the key Value Pair, which in our case is a public Key
            var randomGenerator = new CryptoApiRandomGenerator();
            var random = new SecureRandom(randomGenerator);
            AsymmetricCipherKeyPair subjectKeyPair = default(AsymmetricCipherKeyPair);
            const int strength = 2048;
            var keyGenerationParameters = new KeyGenerationParameters(random, strength);

            var keyPairGenerator = new RsaKeyPairGenerator();
            keyPairGenerator.Init(keyGenerationParameters);
            subjectKeyPair = keyPairGenerator.GenerateKeyPair();
            AsymmetricCipherKeyPair issuerKeyPair = subjectKeyPair;

            //PKCS #10 Certificate Signing Request
            Pkcs10CertificationRequest csr = new Pkcs10CertificationRequest("SHA1WITHRSA", subject, issuerKeyPair.Public, null, issuerKeyPair.Private);

            //Convert BouncyCastle CSR to .PEM file.
            StringBuilder CSRPem = new StringBuilder();
            PemWriter CSRPemWriter = new PemWriter(new StringWriter(CSRPem));
            CSRPemWriter.WriteObject(csr);
            CSRPemWriter.Writer.Flush();

            var CSRtxt = CSRPem.ToString();

            using (StreamWriter f = new StreamWriter(@"C:\Cert_TEST\DemoCSR.txt"))
            {
                f.Write(CSRtxt);
            }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.