SaveCerficateControl.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(FilePath == string.Empty)
  41. {
  42. return;
  43. }
  44. if (SetPasswordPbx.Password != string.Empty && SetPasswordPbx.Password == ConfirmPasswordPbx.Password)
  45. {
  46. CertificateInfo.Password = SetPasswordPbx.Password;
  47. }
  48. else
  49. {
  50. return;
  51. }
  52. string certificateInfo = "/";
  53. certificateInfo += "C=" + CertificateInfo.Area;
  54. if (CertificateInfo.Organization != string.Empty)
  55. {
  56. certificateInfo += "/O=" + CertificateInfo.Organization;
  57. }
  58. if (CertificateInfo.OrganizationUnit != string.Empty)
  59. {
  60. certificateInfo += "/D=" + CertificateInfo.OrganizationUnit;
  61. }
  62. certificateInfo += "/CN=" + CertificateInfo.GrantorName;
  63. bool is_2048 = CertificateInfo.AlgorithmType == AlgorithmType.RSA2048bit;
  64. if(CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048))
  65. {
  66. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog();
  67. CertificateAccess certificateAccess = new CertificateAccess()
  68. {
  69. filePath = FilePath,
  70. password = CertificateInfo.Password
  71. };
  72. FillSignatureEvent?.Invoke(sender, certificateAccess);
  73. }
  74. }
  75. protected void UpdateProper<T>(ref T properValue,
  76. T newValue,
  77. [CallerMemberName] string properName = "")
  78. {
  79. if (object.Equals(properValue, newValue))
  80. return;
  81. properValue = newValue;
  82. OnPropertyChanged(properName);
  83. }
  84. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  85. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  86. }
  87. }