Подписание XML файлов с помощью x509 с использованием контроллов OFD & SFD
#38952623
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Здравствуйте, уважаемые форумчане!
Есть такой код:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace WindowsFormsApplication1
{
public partial class CertificateForm : Form
{
public CertificateForm()
{
InitializeComponent();
}
//private void CertificateForm_Load(object sender, EventArgs e)
//{
//}
private void SignToolStripButton_Click(object sender, EventArgs e)
{
try
{
byte[] plainData, signatureData;
plainData = Encoding.UTF8.GetBytes(PlainRichTextBox.Text);
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection foundCertificates = certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
X509Certificate2Collection selectedCertificates = X509Certificate2UI.SelectFromCollection(foundCertificates,
"Select a Certificate.", "Select a Certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection);
if (selectedCertificates.Count > 0)
{
X509Certificate2 certificate = selectedCertificates[0];
if (certificate.HasPrivateKey)
{
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)certificate.PrivateKey;
signatureData = rsaEncryptor.SignData(plainData, new SHA1CryptoServiceProvider());
CipherRichTextBox.Text = Convert.ToBase64String(signatureData);
}
else
{
MessageBox.Show("the selected certificate does not contain a Private Key to use in signing data",
"No Private Key Available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void VerifyToolStripButton_Click(object sender, EventArgs e)
{
try
{
byte[] plainData, signatureData;
plainData = Encoding.UTF8.GetBytes(PlainRichTextBox.Text);
signatureData = Convert.FromBase64String(CipherRichTextBox.Text);
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection foundCertificates = certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
X509Certificate2Collection selectedCertificates = X509Certificate2UI.SelectFromCollection(foundCertificates,
"Select a Certificate.", "Select a Certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection);
if (selectedCertificates.Count > 0)
{
X509Certificate2 certificate = selectedCertificates[0];
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)certificate.PublicKey.Key;
if (rsaEncryptor.VerifyData(plainData, new SHA1CryptoServiceProvider(), signatureData))
{
MessageBox.Show("Your signature has been verified successfully.", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Your Data cannot be verified against the Signature.", "Failure",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Он работает, но так не пойдет. Нужно не вручную текст вбивать или вставлять в RichTextBox,а так сделать, чтобы можно было взять много файлов (от 500 вроде) из одной папки, подписать их с ЭЦП и переложить в другую.
Думал с помощью Openfiledialog & SaveFileDialog делать, но, кажется, что так неправильно... Но пока что хотелось бы, чтобы хоть так работало.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace WindowsFormsApplication1
{
public partial class CertificateForm : Form
{
public CertificateForm()
{
InitializeComponent();
}
//private void CertificateForm_Load(object sender, EventArgs e)
//{
//}
private void SignToolStripButton_Click(object sender, EventArgs e)
{
try
{
//byte[] plainData, signatureData;
//plainData = Encoding.UTF8.GetBytes(PlainRichTextBox.Text);//как вот без этой плейндейты и ричтекстбокса обойтись?
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;//это что-то тоже не работает
ofd.Filter = "XML(*.xml)|*.xml|All files (*.*)| *.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection foundCertificates = certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
X509Certificate2Collection selectedCertificates = X509Certificate2UI.SelectFromCollection(foundCertificates,
"Select a Certificate.", "Select a Certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection);
if (selectedCertificates.Count > 0)
{
X509Certificate2 certificate = selectedCertificates[0];
if (certificate.HasPrivateKey)
{
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)certificate.PrivateKey;
signatureData = rsaEncryptor.SignData(plainData, new SHA1CryptoServiceProvider());
CipherRichTextBox.Text = Convert.ToBase64String(signatureData);
}
else
{
MessageBox.Show("the selected certificate does not contain a Private Key to use in signing data",
"No Private Key Available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void VerifyToolStripButton_Click(object sender, EventArgs e)
{
try
{
byte[] plainData, signatureData;
plainData = Encoding.UTF8.GetBytes(PlainRichTextBox.Text);
signatureData = Convert.FromBase64String(CipherRichTextBox.Text);
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection foundCertificates = certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
X509Certificate2Collection selectedCertificates = X509Certificate2UI.SelectFromCollection(foundCertificates,
"Select a Certificate.", "Select a Certificate from the following list to get information on that certificate", X509SelectionFlag.SingleSelection);
if (selectedCertificates.Count > 0)
{
X509Certificate2 certificate = selectedCertificates[0];
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)certificate.PublicKey.Key;
if (rsaEncryptor.VerifyData(plainData, new SHA1CryptoServiceProvider(), signatureData))
{
MessageBox.Show("Your signature has been verified successfully.", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Your Data cannot be verified against the Signature.", "Failure",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
SaveFileDialog sfd = new SaveFileDialog();
//sfd.SupportMultiDottedExtensions = true;
sfd.Filter = "XML (*.xml)|*.xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
}
}
}
}
Тут подскажите,пожалуйста, как сделать так, если это возможно , чтобы без ричбоксов: открылся диалог, выбрали файлы, они подписались, проверились, открылся другой диалог, их сохранили.
Заранее большое спасибо!
|
|