SaveCerficateControl.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. bool is_2048 = CertificateInfo.AlgorithmType == AlgorithmType.RSA2048bit;
  71. if(CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048))
  72. {
  73. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog();
  74. CertificateAccess certificateAccess = new CertificateAccess()
  75. {
  76. filePath = FilePath,
  77. password = CertificateInfo.Password
  78. };
  79. FillSignatureEvent?.Invoke(sender, certificateAccess);
  80. }
  81. }
  82. protected void UpdateProper<T>(ref T properValue,
  83. T newValue,
  84. [CallerMemberName] string properName = "")
  85. {
  86. if (object.Equals(properValue, newValue))
  87. return;
  88. properValue = newValue;
  89. OnPropertyChanged(properName);
  90. }
  91. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  92. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  93. }
  94. }