Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Security.Cryptography;
- using System.ServiceModel;
- using System.Text;
- namespace PubSupMea
- {
- [ServiceContract]
- public interface IMeasuringDevice
- {
- [OperationContract]
- void Init(string keyPath);
- [OperationContract]
- void Write(string message, byte[] signature);
- }
- [ServiceContract]
- public interface IClient
- {
- [OperationContract]
- Message GetMeasurement();
- }
- class Service1 : IMeasuringDevice, IClient
- {
- static string javniKljuc;
- static Dictionary<int, string> javniKljucevi = new Dictionary<int, string>();
- static List<Message> poruke = new List<Message>();
- static CspParameters csp = new CspParameters();
- static RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
- public void Init(string keyPath)
- {
- javniKljuc = keyPath;
- }
- public void Write(string message, byte[] signature)
- {
- byte[] hash = null;
- using (SHA256 sha = SHA256Managed.Create())
- {
- hash = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
- }
- using (StreamReader sr = new StreamReader(javniKljuc))
- {
- rsa.FromXmlString(sr.ReadToEnd());
- }
- RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(rsa);
- deformatter.SetHashAlgorithm("SHA256");
- if(deformatter.VerifySignature(hash, signature))
- {
- Message m = new Message(message);
- poruke.Add(m);
- }
- }
- public Message GetMeasurement()
- {
- int highest = 0;
- int ind = 0;
- Message zaSlanje = null;
- for(int i = 0; i < poruke.Count; i++)
- {
- if(poruke[i].priority > highest)
- {
- highest = poruke[i].priority;
- zaSlanje = new Message(poruke[i]);
- ind = i;
- }
- }
- poruke.RemoveAt(ind);
- return zaSlanje;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.ServiceModel;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Client
- {
- class Program
- {
- static ServiceReference1.ClientClient cl = new ServiceReference1.ClientClient();
- static void Main(string[] args)
- {
- while(true)
- {
- ServiceReference1.Message poruka = cl.GetMeasurement();
- Console.WriteLine($"Poruka: [Priority:{ poruka.priority}, Value: { poruka.value}, TimeStamp: { poruka.TimeStamp}");
- Thread.Sleep(5000);
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MeasuringDevice
- {
- class Program
- {
- static ServiceReference1.MeasuringDeviceClient mdc = new ServiceReference1.MeasuringDeviceClient();
- static CspParameters csp = null;
- static RSACryptoServiceProvider rsa = null;
- const string KEY_PATH = @"C:\temp\key2.txt";
- static void CreateAsmKeys()
- {
- csp = new CspParameters();
- rsa = new RSACryptoServiceProvider(csp);
- }
- static void ExportPublicKey()
- {
- using (StreamWriter sw = new StreamWriter(KEY_PATH))
- {
- sw.WriteLine(rsa.ToXmlString(false));
- }
- }
- static byte[] SignMessage(string message)
- {
- using (SHA256 sha = SHA256Managed.Create())
- {
- var hashValue = sha.ComputeHash(Encoding.UTF8.GetBytes(message));
- var formatter = new RSAPKCS1SignatureFormatter(rsa);
- formatter.SetHashAlgorithm("SHA256");
- return formatter.CreateSignature(hashValue);
- }
- }
- static void Main(string[] args)
- {
- CreateAsmKeys();
- ExportPublicKey();
- mdc.Init(KEY_PATH);
- Random rnd = new Random();
- while(true)
- {
- int priority = rnd.Next(1, 4);
- int value = rnd.Next(0, 101);
- String timeStamp = DateTime.Now.ToString("dd.MM.yyyy. HH.mm.ss");
- string message = $"{priority}:{value}:{timeStamp}";
- Console.WriteLine($"Salje se poruka: [Priority:{priority}, Value:{value}, TimeStamp:{timeStamp}");
- byte[] signature = SignMessage(message);
- mdc.Write($"{message}", signature);
- Thread.Sleep(5000);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement