MikecIT

Untitled

Dec 26th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.23 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 PubSubDup
  11. {
  12.     [ServiceContract]
  13.     public interface IPub
  14.     {
  15.         [OperationContract]
  16.         void PubInit(string keyPath);
  17.  
  18.         [OperationContract]
  19.         void Write(string message, byte[] signature, List<int> subscriberIds);
  20.     }
  21.  
  22.     public interface ISubCallback
  23.     {
  24.         [OperationContract(IsOneWay = true)]
  25.         void WriteToConsole(string message);
  26.     }
  27.  
  28.     [ServiceContract(CallbackContract = typeof(ISubCallback))]
  29.     public interface ISub
  30.     {
  31.         [OperationContract]
  32.         void SubInit(int id);
  33.     }
  34.  
  35.     public class PubSubService : IPub, ISub
  36.     {
  37.         delegate void SubDelegate(string message, int subId);
  38.  
  39.         static event SubDelegate onMessageReceived = null;
  40.         static Dictionary<int, ISubCallback> proxies = new Dictionary<int, ISubCallback>();
  41.         static CspParameters csp = new CspParameters();
  42.         static RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
  43.         static string publicKey = null;
  44.  
  45.         public void PubInit(string keyPath)
  46.         {
  47.             if (publicKey == null)
  48.             {
  49.                 publicKey = keyPath;
  50.                 onMessageReceived += (string message, int id) => proxies[id].WriteToConsole(message);
  51.             }
  52.         }
  53.  
  54.         public void SubInit(int id)
  55.         {
  56.             var proxy = OperationContext.Current.GetCallbackChannel
  57.                 <ISubCallback>();
  58.             if(proxies.ContainsKey(id))
  59.             {
  60.                 proxy.WriteToConsole("Vec postoji unet ID.");
  61.             }
  62.             else
  63.             {
  64.                 proxies.Add(id, proxy);
  65.             }
  66.         }
  67.  
  68.         public void Write(string message, byte[] signature, List<int> subscriberIds)
  69.         {
  70.             byte[] hash = null;
  71.  
  72.             using (SHA256 sha = SHA256Managed.Create())
  73.             {
  74.                 hash = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
  75.             }
  76.  
  77.             if (publicKey != null)
  78.             {
  79.                 using (StreamReader sr = new StreamReader(publicKey))
  80.                 {
  81.                     rsa.FromXmlString(sr.ReadToEnd());
  82.                 }
  83.             }
  84.             else
  85.             {
  86.                 return;
  87.             }
  88.  
  89.             RSAPKCS1SignatureDeformatter defformater = new RSAPKCS1SignatureDeformatter(rsa);
  90.             defformater.SetHashAlgorithm("SHA256");
  91.  
  92.             if(defformater.VerifySignature(hash, signature))
  93.             {
  94.                 if (onMessageReceived != null)
  95.                 {
  96.                     foreach (int id in subscriberIds)
  97.                     {
  98.                         if(proxies.ContainsKey(id))
  99.                         {
  100.                             onMessageReceived(message, id);
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110. using System;
  111. using System.Collections.Generic;
  112. using System.Linq;
  113. using System.Text;
  114. using System.Threading.Tasks;
  115. using System.ServiceModel;
  116.  
  117. namespace Subscriber
  118. {
  119.     public class SubscriberCallback : ServiceReference1.ISubCallback
  120.     {
  121.         public void WriteToConsole(string message)
  122.         {
  123.             Console.WriteLine(message);
  124.         }
  125.     }
  126.  
  127.     class Program
  128.     {
  129.         static void Main(string[] args)
  130.         {
  131.             InstanceContext ic = new InstanceContext(new SubscriberCallback());
  132.             ServiceReference1.SubClient proxy = new
  133.                 ServiceReference1.SubClient(ic);
  134.  
  135.             Console.WriteLine("Unesite ID subscribera: ");
  136.             int id = 0;
  137.             id = Convert.ToInt32(Console.ReadLine());
  138.             proxy.SubInit(id);
  139.  
  140.             Console.ReadKey();
  141.         }
  142.     }
  143. }
  144.  
  145.  
  146. using System;
  147. using System.Collections.Generic;
  148. using System.IO;
  149. using System.Linq;
  150. using System.Security.Cryptography;
  151. using System.Text;
  152. using System.Threading;
  153. using System.Threading.Tasks;
  154.  
  155. namespace Publisher
  156. {
  157.     class Program
  158.     {
  159.         static ServiceReference1.PubClient pubClient = new ServiceReference1.PubClient();
  160.         static CspParameters csp = new CspParameters();
  161.         static RSACryptoServiceProvider rsa = null;
  162.         const string KEY_PATH = @"C:\temp\publicKey.txt";
  163.  
  164.         static void CreateAsmKeys()
  165.         {
  166.             rsa = new RSACryptoServiceProvider(csp);
  167.         }
  168.  
  169.         static void ExportPublicKey()
  170.         {
  171.             using (StreamWriter sw = new StreamWriter(KEY_PATH))
  172.             {
  173.                 sw.Write(rsa.ToXmlString(false));
  174.             }
  175.         }
  176.  
  177.         static byte[] SignMessage(string message)
  178.         {
  179.             byte[] hash = null;
  180.  
  181.             using (SHA256 sha = SHA256Managed.Create())
  182.             {
  183.                 hash = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
  184.             }
  185.  
  186.             var formatter = new RSAPKCS1SignatureFormatter(rsa);
  187.             formatter.SetHashAlgorithm("SHA256");
  188.  
  189.             return formatter.CreateSignature(hash);
  190.         }
  191.  
  192.         static void Main(string[] args)
  193.         {
  194.             CreateAsmKeys();
  195.             ExportPublicKey();
  196.             pubClient.PubInit(KEY_PATH);
  197.  
  198.             Random rnd = new Random();
  199.             string message = "";
  200.  
  201.             while(true)
  202.             {
  203.                 List<int> subscribers = new List<int>() { -1, -1, -1};
  204.                 for(int i = 0; i < subscribers.Count; i++)
  205.                     subscribers[i] = rnd.Next(0, 3);
  206.  
  207.                 if(message.Length > 10)
  208.                 {
  209.                     message = "a";
  210.                 }
  211.                 else
  212.                 {
  213.                     message += "a";
  214.                 }
  215.  
  216.                 byte[] signature = SignMessage(message);
  217.                 pubClient.Write(message, signature, subscribers.ToArray());
  218.                 Console.Write($"Poruka {message} je poslata na subscriber-e:");
  219.                 foreach (int i in subscribers)
  220.                     Console.Write($"{i}, ");
  221.                 Console.WriteLine();
  222.                 Thread.Sleep(5000);
  223.             }
  224.         }
  225.     }
  226. }
Add Comment
Please, Sign In to add comment