Advertisement
saleks28

pasoib9_client

Jun 8th, 2020
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Windows.Forms;
  12. using System.Security.Cryptography;
  13. using System.Security.Permissions;
  14. using System.Security.Cryptography.Pkcs;
  15. using System.IO;
  16. using System.Security.Cryptography.X509Certificates;
  17.  
  18.  
  19. namespace PASOIB9
  20. {
  21.     public partial class Form1 : Form
  22.     {
  23.         Socket connectionSocket;
  24.         byte[] signedMessage;
  25.         byte[] encryptedMessage;
  26.         int opflag = 0;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.  
  33.  
  34.         private void ConnectButton_Click(object sender, EventArgs e)
  35.         {
  36.             string ip = Convert.ToString(ipBox.Text);
  37.             int port = Convert.ToInt32(portBox.Text);
  38.             // Trying to establish a client-server connection
  39.             try
  40.             {
  41.                 IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ip), port);
  42.                 connectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  43.                 connectionSocket.Connect(ipPoint);
  44.             }
  45.             catch (Exception exc)
  46.             {
  47.                 Console.WriteLine(exc.Message);
  48.             }
  49.         }
  50.  
  51.         private void DisconnectButton_Click(object sender, EventArgs e)
  52.         {
  53.             // Closing connection with server and releasing socket
  54.             byte[] stopMessage = Encoding.Unicode.GetBytes("stop");
  55.             connectionSocket.Send(stopMessage);
  56.             connectionSocket.Shutdown(SocketShutdown.Both);
  57.             connectionSocket.Close();
  58.         }
  59.  
  60.         private void SendButton_Click(object sender, EventArgs e)
  61.         {
  62.             string msgText = "";
  63.             string msgCaption = "";
  64.             if (UserMessageBox.Text.Length == 0)
  65.             {
  66.                 msgText = "Empty message for encryption";
  67.                 msgCaption = "Encryption error";
  68.  
  69.             }
  70.             else if (ipBox.Text.Length == 0)
  71.             {
  72.                 msgText = "Enter connection IP";
  73.                 msgCaption = "IP error";
  74.             }
  75.             else if (portBox.Text.Length == 0)
  76.             {
  77.                 msgText = "Enter connection port";
  78.                 msgCaption = "Port error";
  79.             }
  80.             if (msgCaption != "" && msgText != "")
  81.                 MessageBox.Show(msgText, msgCaption, MessageBoxButtons.OK);
  82.  
  83.             switch (opflag)
  84.             {
  85.                 case 0:
  86.                     {
  87.                         string flagMsg = "message";
  88.                         byte[] data = Encoding.Unicode.GetBytes(flagMsg);
  89.                         connectionSocket.Send(data);
  90.                         string message = Convert.ToString(UserMessageBox);
  91.                         byte[] data2 = Encoding.Unicode.GetBytes(message);
  92.                         break;
  93.                     }
  94.                 case 1:
  95.                     {
  96.                         string flagMsg = "sign";
  97.                         byte[] data = Encoding.Unicode.GetBytes(flagMsg);
  98.                         connectionSocket.Send(data);
  99.                         connectionSocket.Send(signedMessage);
  100.                         break;
  101.                     }
  102.                 case 2:
  103.                     {
  104.                         string flagMsg = "encrypt";
  105.                         byte[] data = Encoding.Unicode.GetBytes(flagMsg);
  106.                         connectionSocket.Send(data);
  107.                         connectionSocket.Send(encryptedMessage);
  108.                         break;
  109.                     }
  110.             }
  111.             opflag = 0;
  112.  
  113.         }
  114.  
  115.         private void EncryptButton_Click(object sender, EventArgs e)
  116.         {
  117.             // Receiving certificate for encryption
  118.             var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  119.             certStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
  120.             X509Certificate2Collection certCollection = certStore.Certificates;
  121.             X509Certificate2Collection selectCert = X509Certificate2UI.SelectFromCollection(certCollection, "Certificate Select",
  122.                 "Select a certificate from the following list", X509SelectionFlag.SingleSelection);
  123.             X509Certificate2 curCert = selectCert[0];
  124.             // Get current user message
  125.             encryptedMessage = Encoding.Unicode.GetBytes(Convert.ToString(UserMessageBox.Text));
  126.             ContentInfo contentInfo = new ContentInfo(encryptedMessage);
  127.             SignedCms cms = new SignedCms(contentInfo);
  128.             CmsSigner signer = new CmsSigner(curCert);
  129.             cms.ComputeSignature(signer, false);
  130.             signedMessage = cms.Encode();
  131.             var contentInfo2 = new ContentInfo(signedMessage);
  132.             var recipients = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, curCert);
  133.             var cms2 = new EnvelopedCms(contentInfo2);
  134.             cms2.Encrypt(recipients);
  135.             encryptedMessage = cms2.Encode();
  136.             StringBuilder sb = new StringBuilder();
  137.             foreach (var b in encryptedMessage)
  138.             {
  139.                 sb.Append(b);
  140.             }
  141.             File.WriteAllBytes("file.enc", encryptedMessage);
  142.             encMesssageBox.Text = sb.ToString();
  143.  
  144.             opflag = 2;
  145.         }
  146.  
  147.         private void SignButton_Click(object sender, EventArgs e)
  148.         {
  149.             // Receiving certificate for encryption
  150.             var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  151.             certStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
  152.             X509Certificate2Collection certCollection = certStore.Certificates;
  153.             X509Certificate2Collection selectCert = X509Certificate2UI.SelectFromCollection(certCollection, "Certificate Select",
  154.                 "Select a certificate from the following list", X509SelectionFlag.SingleSelection);
  155.             X509Certificate2 curCert = selectCert[0];
  156.  
  157.             // Get current user message
  158.             signedMessage = Encoding.Unicode.GetBytes(Convert.ToString(UserMessageBox.Text));
  159.             ContentInfo contentInfo = new ContentInfo(signedMessage);
  160.             SignedCms cms = new SignedCms(contentInfo);
  161.             CmsSigner signer = new CmsSigner(curCert);
  162.             cms.ComputeSignature(signer, false);
  163.             signedMessage = cms.Encode();
  164.             File.WriteAllBytes("file.sign", signedMessage);
  165.  
  166.             StringBuilder sb = new StringBuilder();
  167.             foreach (var b in signedMessage)
  168.             {
  169.                 sb.Append(b);
  170.             }
  171.  
  172.             signMessageBox.Text = sb.ToString();
  173.             opflag = 1;
  174.         }
  175.  
  176.         private void HashButton_Click(object sender, EventArgs e)
  177.         {
  178.             HashAlgorithm hashAlg = HashAlgorithm.Create("GOST3411");
  179.             byte[] messageHash = new UTF8Encoding().GetBytes(Convert.ToString(UserMessageBox.Text));
  180.             var hash = hashAlg.ComputeHash(messageHash);
  181.             StringBuilder sb = new StringBuilder();
  182.             foreach (var b in hash)
  183.             {
  184.                 sb.Append(b);
  185.             }
  186.             MessageHash.Text = sb.ToString();
  187.         }
  188.  
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement