SaveCertificateControl.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /// Interaction logic for SaveCertificateControlControl.xaml
  13. /// </summary>
  14. public partial class SaveCertificateControl : UserControl, INotifyPropertyChanged
  15. {
  16. public CertificateInfo CertificateInfo;
  17. public event EventHandler CancelSaveEvent;
  18. public event PropertyChangedEventHandler PropertyChanged;
  19. public event EventHandler<CertificateAccess> FillSignatureEvent;
  20. private string _filePath;
  21. public string FilePath
  22. {
  23. get => _filePath;
  24. set => UpdateProper(ref _filePath, value);
  25. }
  26. public SaveCertificateControl()
  27. {
  28. InitializeComponent();
  29. this.DataContext = this;
  30. }
  31. private void SelectFileBtn_Click(object sender, RoutedEventArgs e)
  32. {
  33. string filePath = CommonHelper.GetGeneratePathOrEmpty("PFX Files(*.pfx) | *.pfx|P12 Files(*.p12) | *.p12");
  34. if (filePath != string.Empty)
  35. {
  36. FilePath = filePath;
  37. }
  38. }
  39. private void DoneBtn_Click(object sender, RoutedEventArgs e)
  40. {
  41. if(string.IsNullOrEmpty(FilePath))
  42. {
  43. ErrorTipsText.Text = LanguageHelper.SigManager.GetString("Warn_NoFile");
  44. return;
  45. }
  46. if (string.IsNullOrEmpty(SetPasswordPbx.Password))
  47. {
  48. ErrorTipsText.Text = LanguageHelper.SigManager.GetString("Warn_NoPassword");
  49. return;
  50. }
  51. if (SetPasswordPbx.Password == ConfirmPasswordPbx.Password)
  52. {
  53. CertificateInfo.Password = SetPasswordPbx.Password;
  54. }
  55. else
  56. {
  57. ErrorTipsText.Text = LanguageHelper.SigManager.GetString("Warn_Password");
  58. return;
  59. }
  60. string certificateInfo = "/";
  61. certificateInfo += "C=" + CertificateInfo.Area;
  62. if (CertificateInfo.OrganizationUnit != string.Empty)
  63. {
  64. certificateInfo += "/OU=" + CertificateInfo.OrganizationUnit;
  65. }
  66. if (CertificateInfo.Organization != string.Empty)
  67. {
  68. certificateInfo += "/O=" + CertificateInfo.Organization;
  69. }
  70. if (CertificateInfo.OrganizationUnit != string.Empty)
  71. {
  72. certificateInfo += "/D=" + CertificateInfo.OrganizationUnit;
  73. }
  74. certificateInfo += "/CN=" + CertificateInfo.GrantorName;
  75. certificateInfo += "/emailAddress=" + CertificateInfo.Email;
  76. bool is_2048 = CertificateInfo.AlgorithmType == AlgorithmType.RSA2048bit;
  77. if(CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048))
  78. {
  79. CertificateAccess certificateAccess = new CertificateAccess()
  80. {
  81. filePath = FilePath,
  82. password = CertificateInfo.Password
  83. };
  84. FillSignatureEvent?.Invoke(sender, certificateAccess);
  85. }
  86. }
  87. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  88. {
  89. CancelSaveEvent?.Invoke(this, EventArgs.Empty);
  90. }
  91. protected void UpdateProper<T>(ref T properValue,
  92. T newValue,
  93. [CallerMemberName] string properName = "")
  94. {
  95. if (object.Equals(properValue, newValue))
  96. return;
  97. properValue = newValue;
  98. OnPropertyChanged(properName);
  99. }
  100. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  101. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  102. private void SetPasswordPbx_OnPasswordChanged(object sender, RoutedEventArgs e)
  103. {
  104. PasswordBox passwordBox = (PasswordBox)sender;
  105. TextBlock textBlock = PasswordTextBlock;
  106. if (passwordBox.Password.Length > 0)
  107. {
  108. textBlock.Visibility = Visibility.Hidden;
  109. }
  110. else
  111. {
  112. textBlock.Visibility = Visibility.Visible;
  113. }
  114. }
  115. private void ConfirmPasswordPbx_OnPasswordChanged(object sender, RoutedEventArgs e)
  116. {
  117. PasswordBox passwordBox = (PasswordBox)sender;
  118. TextBlock textBlock = ConfirmPasswordTextBlock;
  119. if (passwordBox.Password.Length > 0)
  120. {
  121. textBlock.Visibility = Visibility.Hidden;
  122. }
  123. else
  124. {
  125. textBlock.Visibility = Visibility.Visible;
  126. }
  127. }
  128. }
  129. }