static void CreateSelfSignCertificate(CertOption option) { var fileName = option.CertFileName; var subject = option.Subject; var password = option.Password; try { var securePassword = Certificate.ConvertSecureString(password); var startDate = DateTime.Now; var endDate = startDate.AddYears(option.Years); var certData = Certificate.CreateSelfSignCertificatePfx(subject, startDate, endDate, securePassword); using (var writer = new System.IO.BinaryWriter(System.IO.File.Open(fileName, System.IO.FileMode.Create))) { writer.Write(certData); writer.Flush(); writer.Close(); } securePassword = Certificate.ConvertSecureString(password); } catch (Exception e) { Console.WriteLine(e.Message); } }
internal static CertOption ParseCommand(string[] args) { string optionsw = ""; List<string> parameters = new List<string>(); string years = ""; // スイッチとパラメータを分離します for (var i = 0; i < args.Length; i++) { if (args[i].StartsWith("-")) optionsw = args[i].Substring(1).ToLower(); else parameters.Add(args[i]); } // 有効期間を省略した場合は、2年とする if (args.Length == 4) parameters.Add(YEARS.ToString()); var options = new CertOption(); try { if (optionsw == "create" || optionsw == "c") { options.IsCreate = true; options.CertFileName = parameters[0]; options.Subject = parameters[1]; options.Password = parameters[2]; years = parameters[3]; } else { options.IsCreate = false; options.CertFileName = parameters[0]; options.NewCertFileName = parameters[1]; options.Subject = parameters[2]; options.Password = parameters[3]; years = parameters[4]; } // 有効期間の変換に失敗すれば、デフォルトの2年にします int term = 0; var parseResult = int.TryParse(years, out term); if (!parseResult) term = YEARS; options.Years = term; return options; } catch (Exception e) { throw e; } }