EL_Singature/AppView/FormMain.cs
2023-05-20 00:18:29 +04:00

118 lines
3.0 KiB
C#

using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows.Forms;
using ElectronicSignature.Certification;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
namespace AppView
{
public partial class FormMain : Form
{
private static readonly string keyPairPath = @"G:\tests\rsaKeyPair.pem";
public static int keySize = 50;
public FormMain()
{
InitializeComponent();
}
private void buttonFilePath_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog { };
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxFilePath.Text = dialog.FileName;
}
}
private void buttonSavepath_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog { };
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxSavePath.Text = dialog.FileName;
}
}
private void buttonKeyPath_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog { };
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxKeyPath.Text = dialog.FileName;
}
}
private void buttonFile2_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog { };
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxFile2.Text = dialog.FileName;
}
}
private void buttonCert2_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog { };
if (dialog.ShowDialog() == DialogResult.OK)
{
textBoxKey2.Text = dialog.FileName;
}
}
private void buttonWriteKey_Click(object sender, EventArgs e)
{
FileStream? fstream = null;
try
{
fstream = File.OpenRead(textBoxFilePath.Text);
var encoded = Cryptography.EncryptDataByPublicCert(fstream.ToString(), textBoxKeyPath.Text.GetPublicCert());
fstream?.Close();
fstream = new FileStream(textBoxSavePath.Text, FileMode.OpenOrCreate);
fstream?.Write(encoded);
fstream?.Close();
MessageBox.Show("Óñïåõ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCheckKey_Click(object sender, EventArgs e)
{
FileStream? fstream = null;
try
{
fstream = new FileStream(textBoxFile2.Text, FileMode.Open);
var privateCertPath = textBoxKey2.Text;
var signature = Cryptography.SignDataByPrivateCert(fstream.ToString(), privateCertPath.GetPrivateCert(textBoxPass.Text));
if (Cryptography.VerifySignedDataByCertIssuer(signature, textBoxKeyPath.Text.GetPublicCert(), out var data))
{
if (data != null)
MessageBox.Show(Encoding.UTF8.GetString(data));
}
else
{
MessageBox.Show("False");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ñïèñîêToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormKeys));
if (service is FormKeys form)
{
form.ShowDialog();
}
}
}
}