Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.Net;
- using System.Net.Sockets;
- using System.Windows.Forms;
- using System.Security.Cryptography;
- using System.Security.Permissions;
- using System.Security.Cryptography.Pkcs;
- using System.IO;
- using System.Security.Cryptography.X509Certificates;
- namespace PASOIB9
- {
- public partial class Form1 : Form
- {
- Socket connectionSocket;
- byte[] signedMessage;
- byte[] encryptedMessage;
- int opflag = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void ConnectButton_Click(object sender, EventArgs e)
- {
- string ip = Convert.ToString(ipBox.Text);
- int port = Convert.ToInt32(portBox.Text);
- // Trying to establish a client-server connection
- try
- {
- IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ip), port);
- connectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- connectionSocket.Connect(ipPoint);
- }
- catch (Exception exc)
- {
- Console.WriteLine(exc.Message);
- }
- }
- private void DisconnectButton_Click(object sender, EventArgs e)
- {
- // Closing connection with server and releasing socket
- byte[] stopMessage = Encoding.Unicode.GetBytes("stop");
- connectionSocket.Send(stopMessage);
- connectionSocket.Shutdown(SocketShutdown.Both);
- connectionSocket.Close();
- }
- private void SendButton_Click(object sender, EventArgs e)
- {
- string msgText = "";
- string msgCaption = "";
- if (UserMessageBox.Text.Length == 0)
- {
- msgText = "Empty message for encryption";
- msgCaption = "Encryption error";
- }
- else if (ipBox.Text.Length == 0)
- {
- msgText = "Enter connection IP";
- msgCaption = "IP error";
- }
- else if (portBox.Text.Length == 0)
- {
- msgText = "Enter connection port";
- msgCaption = "Port error";
- }
- if (msgCaption != "" && msgText != "")
- MessageBox.Show(msgText, msgCaption, MessageBoxButtons.OK);
- switch (opflag)
- {
- case 0:
- {
- string flagMsg = "message";
- byte[] data = Encoding.Unicode.GetBytes(flagMsg);
- connectionSocket.Send(data);
- string message = Convert.ToString(UserMessageBox);
- byte[] data2 = Encoding.Unicode.GetBytes(message);
- break;
- }
- case 1:
- {
- string flagMsg = "sign";
- byte[] data = Encoding.Unicode.GetBytes(flagMsg);
- connectionSocket.Send(data);
- connectionSocket.Send(signedMessage);
- break;
- }
- case 2:
- {
- string flagMsg = "encrypt";
- byte[] data = Encoding.Unicode.GetBytes(flagMsg);
- connectionSocket.Send(data);
- connectionSocket.Send(encryptedMessage);
- break;
- }
- }
- opflag = 0;
- }
- private void EncryptButton_Click(object sender, EventArgs e)
- {
- // Receiving certificate for encryption
- var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
- certStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
- X509Certificate2Collection certCollection = certStore.Certificates;
- X509Certificate2Collection selectCert = X509Certificate2UI.SelectFromCollection(certCollection, "Certificate Select",
- "Select a certificate from the following list", X509SelectionFlag.SingleSelection);
- X509Certificate2 curCert = selectCert[0];
- // Get current user message
- encryptedMessage = Encoding.Unicode.GetBytes(Convert.ToString(UserMessageBox.Text));
- ContentInfo contentInfo = new ContentInfo(encryptedMessage);
- SignedCms cms = new SignedCms(contentInfo);
- CmsSigner signer = new CmsSigner(curCert);
- cms.ComputeSignature(signer, false);
- signedMessage = cms.Encode();
- var contentInfo2 = new ContentInfo(signedMessage);
- var recipients = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, curCert);
- var cms2 = new EnvelopedCms(contentInfo2);
- cms2.Encrypt(recipients);
- encryptedMessage = cms2.Encode();
- StringBuilder sb = new StringBuilder();
- foreach (var b in encryptedMessage)
- {
- sb.Append(b);
- }
- File.WriteAllBytes("file.enc", encryptedMessage);
- encMesssageBox.Text = sb.ToString();
- opflag = 2;
- }
- private void SignButton_Click(object sender, EventArgs e)
- {
- // Receiving certificate for encryption
- var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
- certStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
- X509Certificate2Collection certCollection = certStore.Certificates;
- X509Certificate2Collection selectCert = X509Certificate2UI.SelectFromCollection(certCollection, "Certificate Select",
- "Select a certificate from the following list", X509SelectionFlag.SingleSelection);
- X509Certificate2 curCert = selectCert[0];
- // Get current user message
- signedMessage = Encoding.Unicode.GetBytes(Convert.ToString(UserMessageBox.Text));
- ContentInfo contentInfo = new ContentInfo(signedMessage);
- SignedCms cms = new SignedCms(contentInfo);
- CmsSigner signer = new CmsSigner(curCert);
- cms.ComputeSignature(signer, false);
- signedMessage = cms.Encode();
- File.WriteAllBytes("file.sign", signedMessage);
- StringBuilder sb = new StringBuilder();
- foreach (var b in signedMessage)
- {
- sb.Append(b);
- }
- signMessageBox.Text = sb.ToString();
- opflag = 1;
- }
- private void HashButton_Click(object sender, EventArgs e)
- {
- HashAlgorithm hashAlg = HashAlgorithm.Create("GOST3411");
- byte[] messageHash = new UTF8Encoding().GetBytes(Convert.ToString(UserMessageBox.Text));
- var hash = hashAlg.ComputeHash(messageHash);
- StringBuilder sb = new StringBuilder();
- foreach (var b in hash)
- {
- sb.Append(b);
- }
- MessageHash.Text = sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement