Advertisement
MikecIT

Untitled

Dec 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Security.Cryptography;
  7. using System.ServiceModel;
  8. using System.Text;
  9.  
  10. namespace PubSupMea
  11. {
  12.     [ServiceContract]
  13.     public interface IMeasuringDevice
  14.     {
  15.         [OperationContract]
  16.         void Init(string keyPath);
  17.  
  18.         [OperationContract]
  19.         void Write(string message, byte[] signature);
  20.     }
  21.  
  22.     [ServiceContract]
  23.     public interface IClient
  24.     {
  25.         [OperationContract]
  26.         Message GetMeasurement();
  27.     }
  28.  
  29.     class Service1 : IMeasuringDevice, IClient
  30.     {
  31.         static string javniKljuc;
  32.         static Dictionary<int, string> javniKljucevi = new Dictionary<int, string>();
  33.         static List<Message> poruke = new List<Message>();
  34.         static CspParameters csp = new CspParameters();
  35.         static RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
  36.  
  37.         public void Init(string keyPath)
  38.         {
  39.             javniKljuc = keyPath;
  40.         }
  41.  
  42.         public void Write(string message, byte[] signature)
  43.         {
  44.             byte[] hash = null;
  45.  
  46.             using (SHA256 sha = SHA256Managed.Create())
  47.             {
  48.                 hash = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
  49.             }
  50.  
  51.             using (StreamReader sr = new StreamReader(javniKljuc))
  52.             {
  53.                 rsa.FromXmlString(sr.ReadToEnd());
  54.             }
  55.  
  56.             RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(rsa);
  57.             deformatter.SetHashAlgorithm("SHA256");
  58.  
  59.             if(deformatter.VerifySignature(hash, signature))
  60.             {
  61.                 Message m = new Message(message);
  62.                 poruke.Add(m);
  63.             }
  64.         }
  65.  
  66.  
  67.         public Message GetMeasurement()
  68.         {
  69.             int highest = 0;
  70.             int ind = 0;
  71.             Message zaSlanje = null;
  72.  
  73.             for(int i = 0; i < poruke.Count; i++)
  74.             {
  75.                 if(poruke[i].priority > highest)
  76.                 {
  77.                     highest = poruke[i].priority;
  78.                     zaSlanje = new Message(poruke[i]);
  79.                     ind = i;
  80.                 }
  81.             }
  82.             poruke.RemoveAt(ind);
  83.             return zaSlanje;
  84.         }
  85.     }
  86. }
  87.  
  88. using System;
  89. using System.Collections.Generic;
  90. using System.Linq;
  91. using System.ServiceModel;
  92. using System.Text;
  93. using System.Threading;
  94. using System.Threading.Tasks;
  95.  
  96. namespace Client
  97. {
  98.     class Program
  99.     {
  100.         static ServiceReference1.ClientClient cl = new ServiceReference1.ClientClient();
  101.  
  102.         static void Main(string[] args)
  103.         {
  104.             while(true)
  105.             {
  106.                 ServiceReference1.Message poruka = cl.GetMeasurement();
  107.                 Console.WriteLine($"Poruka: [Priority:{ poruka.priority}, Value: { poruka.value}, TimeStamp: { poruka.TimeStamp}");
  108.                 Thread.Sleep(5000);
  109.             }
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115. using System;
  116. using System.Collections.Generic;
  117. using System.IO;
  118. using System.Linq;
  119. using System.Security.Cryptography;
  120. using System.Text;
  121. using System.Threading;
  122. using System.Threading.Tasks;
  123.  
  124. namespace MeasuringDevice
  125. {
  126.     class Program
  127.     {
  128.         static ServiceReference1.MeasuringDeviceClient mdc = new ServiceReference1.MeasuringDeviceClient();
  129.         static CspParameters csp = null;
  130.         static RSACryptoServiceProvider rsa = null;
  131.         const string KEY_PATH = @"C:\temp\key2.txt";
  132.  
  133.         static void CreateAsmKeys()
  134.         {
  135.             csp = new CspParameters();
  136.             rsa = new RSACryptoServiceProvider(csp);
  137.         }
  138.  
  139.         static void ExportPublicKey()
  140.         {
  141.             using (StreamWriter sw = new StreamWriter(KEY_PATH))
  142.             {
  143.                 sw.WriteLine(rsa.ToXmlString(false));
  144.             }
  145.         }
  146.  
  147.         static byte[] SignMessage(string message)
  148.         {
  149.             using (SHA256 sha = SHA256Managed.Create())
  150.             {
  151.                 var hashValue = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
  152.  
  153.                 var formatter = new RSAPKCS1SignatureFormatter(rsa);
  154.                 formatter.SetHashAlgorithm("SHA256");
  155.  
  156.                 return formatter.CreateSignature(hashValue);
  157.             }
  158.         }
  159.  
  160.         static void Main(string[] args)
  161.         {
  162.             CreateAsmKeys();
  163.             ExportPublicKey();
  164.             mdc.Init(KEY_PATH);
  165.  
  166.             Random rnd = new Random();
  167.  
  168.             while(true)
  169.             {
  170.                 int priority = rnd.Next(1, 4);
  171.                 int value = rnd.Next(0, 101);
  172.                 String timeStamp = DateTime.Now.ToString("dd.MM.yyyy. HH.mm.ss");
  173.                 string message = $"{priority}:{value}:{timeStamp}";
  174.                 Console.WriteLine($"Salje se poruka: [Priority:{priority}, Value:{value}, TimeStamp:{timeStamp}");
  175.                 byte[] signature = SignMessage(message);
  176.                 mdc.Write($"{message}", signature);
  177.  
  178.                 Thread.Sleep(5000);
  179.             }
  180.  
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement