SaveCerficateControl.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using ComPDFKit.DigitalSign;
  2. using Compdfkit_Tools.Helper;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Runtime.CompilerServices;
  6. using System.Security;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. namespace Compdfkit_Tools.PDFControl
  10. {
  11. /// <summary>
  12. /// SaveCerficateControlControl.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class SaveCerficateControl : UserControl, INotifyPropertyChanged
  15. {
  16. public CertificateInfo CertificateInfo;
  17. public event PropertyChangedEventHandler PropertyChanged;
  18. public event EventHandler<CertificateAccess> FillSignatureEvent;
  19. private string _filePath;
  20. public string FilePath
  21. {
  22. get => _filePath;
  23. set => UpdateProper(ref _filePath, value);
  24. }
  25. public SaveCerficateControl()
  26. {
  27. InitializeComponent();
  28. this.DataContext = this;
  29. }
  30. private void SelectFileBtn_Click(object sender, RoutedEventArgs e)
  31. {
  32. string filePath = CommonHelper.GetGeneratePathOrEmpty("PFX Files(*.pfx) | *.pfx");
  33. if (filePath != string.Empty)
  34. {
  35. FilePath = filePath;
  36. }
  37. }
  38. private void DoneBtn_Click(object sender, RoutedEventArgs e)
  39. {
  40. if(string.IsNullOrEmpty(FilePath))
  41. {
  42. ErrorTipsText.Text = "Please select a file path";
  43. return;
  44. }
  45. if (string.IsNullOrEmpty(SetPasswordPbx.Password))
  46. {
  47. ErrorTipsText.Text = "Please enter a password";
  48. return;
  49. }
  50. if (SetPasswordPbx.Password == ConfirmPasswordPbx.Password)
  51. {
  52. CertificateInfo.Password = SetPasswordPbx.Password;
  53. }
  54. else
  55. {
  56. ErrorTipsText.Text = "Please enter the same password";
  57. return;
  58. }
  59. string certificateInfo = "/";
  60. certificateInfo += "C=" + CertificateInfo.Area;
  61. if (CertificateInfo.Organization != string.Empty)
  62. {
  63. certificateInfo += "/O=" + CertificateInfo.Organization;
  64. }
  65. if (CertificateInfo.OrganizationUnit != string.Empty)
  66. {
  67. certificateInfo += "/D=" + CertificateInfo.OrganizationUnit;
  68. }
  69. certificateInfo += "/CN=" + CertificateInfo.GrantorName;
  70. certificateInfo += "/emailAddress" + CertificateInfo.Email;
  71. bool is_2048 = CertificateInfo.AlgorithmType == AlgorithmType.RSA2048bit;
  72. if(CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048))
  73. {
  74. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog();
  75. CertificateAccess certificateAccess = new CertificateAccess()
  76. {
  77. filePath = FilePath,
  78. password = CertificateInfo.Password
  79. };
  80. FillSignatureEvent?.Invoke(sender, certificateAccess);
  81. }
  82. }
  83. protected void UpdateProper<T>(ref T properValue,
  84. T newValue,
  85. [CallerMemberName] string properName = "")
  86. {
  87. if (object.Equals(properValue, newValue))
  88. return;
  89. properValue = newValue;
  90. OnPropertyChanged(properName);
  91. }
  92. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  93. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  94. }
  95. }