Exemple #1
0
        static void Main(string[] args)
        {
            if (File.Exists(GetInfoFilePath()) == false ||
                File.Exists(GetHostsFilePath()) == false)
            {
                if (File.Exists(GetInfoFilePath()) == false)
                {
                    File.WriteAllText(
                        GetInfoFilePath(),
                        CreateText(
                            "#开头的是注释",
                            "#按位置决定参数的意义",
                            "#Day",
                            "#KeySize",
                            "#CaName",
                            "#SubCertName"),
                        Encoding.UTF8);
                }


                if (File.Exists(GetHostsFilePath()) == false)
                {
                    File.WriteAllText(GetHostsFilePath(), "", Encoding.UTF8);
                }

                Console.WriteLine("已创建配置文件请编辑后继续运行");
                Console.ReadLine();
                return;
            }



            var certInfo = CreateCertInfo();

            var hosts = CreateHosts();


            var caCert = TLSBouncyCastleHelper.GenerateCA(
                certInfo.CaName,
                certInfo.KeySize,
                certInfo.Day);


            var tlsCert = TLSBouncyCastleHelper.GenerateTls(
                CaPack.Create(caCert),
                certInfo.SubCertName,
                certInfo.KeySize,
                certInfo.Day,
                hosts);



            SaveCa(caCert);

            SaveTls(tlsCert);

            SaveTlsPem(tlsCert);
        }
        static void CreateCert()
        {
            var ca = TLSBouncyCastleHelper.GenerateCA(CANAME, 2048, 3000);


            var cert = TLSBouncyCastleHelper.GenerateTls(
                CaPack.Create(ca),
                "Leikaifng iwara.tv",
                2048,
                3000,
                GetSubName());


            var pemCert = TLSBouncyCastleHelper.CreatePem.AsPem(cert);


            var pemKey = TLSBouncyCastleHelper.CreatePem.AsKey(cert);


            using (X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser))
            {
                store.Open(OpenFlags.ReadWrite);

                store.Add(new X509Certificate2(ca.Export(X509ContentType.Cert)));
            }


            var basePath = GetNginxConfigFolderPath();


            Directory.CreateDirectory(basePath);



            var pemPath = Path.Combine(basePath, "cert.pem");
            var cerPath = Path.Combine(basePath, "cert.cer");
            var keyPath = Path.Combine(basePath, "cert.key");
            var caPath  = Path.Combine(basePath, "ca.cer");

            File.WriteAllBytes(cerPath, cert.Export(X509ContentType.Cert));

            File.WriteAllBytes(pemPath, pemCert);

            File.WriteAllBytes(keyPath, pemKey);

            File.WriteAllBytes(caPath, ca.Export(X509ContentType.Cert));
        }