public X509Certificate GenerateRsaKey(int keySize = 2048) { var privateKey = new RsaPrivateKey(keySize); var publicKey = privateKey.CreatePublicKey(); Tbs.SubjectPublicKeyInfo = publicKey.GetSubjectPublicKeyInfo(); PrivateKey = privateKey; return(SetSubjectKeyIdentifier()); }
private static X509Certificate GenerateIntermediateCertificate(X509Certificate root) { var intermediatePrivateKey = new RsaPrivateKey(2048); var csr = new CertificationRequestInfo { Subject = new X509Name { CommonName = "Intermediate CA", Organization = "EasySSL" }, SubjectPublicKeyInfo = intermediatePrivateKey.CreatePublicKey().GetSubjectPublicKeyInfo() }.SetBasicConstraint(new BasicConstraintExtension { Authority = true, PathLengthConstraint = 2 }).SetAuthorityInfoAccess(new AuthorityInfoAccessExtension { Methods = { new AuthorityAccessDescription { Url = "http://ssl.vcap.me/ca.crt" } } }); return(Sign(csr, root).SetPrivateKey(intermediatePrivateKey)); }
public static void Main(string[] rawArgs) { var args = EasySslArgs.Parse(rawArgs); switch (args.Command) { case "genrsa": GenRsa(args); break; default: Help(); return; } var root = GenerateCaCertificate().Export("ca.crt").ExportPrivateKey("ca.key"); Console.WriteLine($"Root authority has been generated\r\n{Utils.StringUtils.GetHexString(root.SignatureValue)}"); var intermediateCertificate = GenerateIntermediateCertificate(root).Export("intermediate.crt"); Console.WriteLine($"Intermediate authority has been generated\r\n{Utils.StringUtils.GetHexString(intermediateCertificate.SignatureValue)}"); var endCertificate = GenerateEndCertificate(intermediateCertificate).Export("vcap.me.crt"); Console.WriteLine($"End certificate has been generated\r\n{Utils.StringUtils.GetHexString(endCertificate.SignatureValue)}"); Console.ReadKey(); //var inputBuffer = new byte[1024]; //var inputStream = Console.OpenStandardInput(inputBuffer.Length); //Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)); var key = new RsaPrivateKey(2048); var publicKey = key.CreatePublicKey(); var pem = publicKey.GetSubjectPublicKeyInfo().ToPem(); Console.WriteLine(pem); Console.ReadKey(); var privatePem = key.GetPrivateKeyInfo().ToPem(); Console.WriteLine(privatePem); Console.ReadKey(); //for (var i = 0; i < 3; i++) { // var line1 = Console.ReadLine(); // var data1 = Encoding.UTF8.GetBytes(line1); // var signature = key.SignData(data1); // Console.WriteLine(signature.GetHexString()); //} //Console.ReadKey(); //var pvk = PrivateKeyFile.Read(@"test.pvk"); //var blob = RsaPrivateKeyBlob.Read(pvk.Key); //var para = blob.ToRsaParamaters(); //var rsa = new RsaPrivateKey(para); //var signature = rsa.SignData(Encoding.UTF8.GetBytes("test")); var ca = GenerateCaCertificate(); ca.Export(@"d:\temp\ca.pfx", false); ca.ExportPvk(@"d:\temp\ca.pvk"); var end = GenerateEndCertificate(ca); end.Export(@"d:\temp\end.pfx", false); Console.WriteLine("certs generated"); Console.ReadKey(); }