SaveCerficateControl.xaml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. private string _filePath;
  19. public string FilePath
  20. {
  21. get => _filePath;
  22. set => UpdateProper(ref _filePath, value);
  23. }
  24. public SaveCerficateControl()
  25. {
  26. InitializeComponent();
  27. this.DataContext = this;
  28. }
  29. private void SelectFileBtn_Click(object sender, RoutedEventArgs e)
  30. {
  31. string filePath = CommonHelper.GetGeneratePathOrEmpty("PFX Files(*.pfx) | *.pfx");
  32. if (filePath != string.Empty)
  33. {
  34. FilePath = filePath;
  35. }
  36. }
  37. private void DoneBtn_Click(object sender, RoutedEventArgs e)
  38. {
  39. if(FilePath == string.Empty)
  40. {
  41. return;
  42. }
  43. if (SetPasswordPbx.Password != string.Empty && SetPasswordPbx.Password == ConfirmPasswordPbx.Password)
  44. {
  45. CertificateInfo.Password = SetPasswordPbx.Password;
  46. }
  47. else
  48. {
  49. return;
  50. }
  51. string certificateInfo = "/";
  52. certificateInfo += "C=" + CertificateInfo.Area;
  53. if (CertificateInfo.Organization != string.Empty)
  54. {
  55. certificateInfo += "/O=" + CertificateInfo.Organization;
  56. }
  57. if (CertificateInfo.OrganizationUnit != string.Empty)
  58. {
  59. certificateInfo += "/D=" + CertificateInfo.OrganizationUnit;
  60. }
  61. certificateInfo += "/CN=" + CertificateInfo.GrantorName;
  62. bool is_2048 = CertificateInfo.AlgorithmType == AlgorithmType.RSA2048bit;
  63. CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048);
  64. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog();
  65. fillDigitalSignatureDialog.FilePath = FilePath;
  66. fillDigitalSignatureDialog.ShowDialog();
  67. }
  68. protected void UpdateProper<T>(ref T properValue,
  69. T newValue,
  70. [CallerMemberName] string properName = "")
  71. {
  72. if (object.Equals(properValue, newValue))
  73. return;
  74. properValue = newValue;
  75. OnPropertyChanged(properName);
  76. }
  77. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  78. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  79. }
  80. }