Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.SignalR;
  7.  
  8. namespace RsaChat.Hubs
  9. {
  10.   public class ChatHub : Hub
  11.   {
  12.     private const int KeySize = 1024;  
  13.     private static ConcurrentDictionary<string, EncryptorRSAKeys> KeyPairs;
  14.     private static IHubCallerClients CallerClients;
  15.    
  16.     static ChatHub()
  17.     {
  18.       KeyPairs = new ConcurrentDictionary<string, EncryptorRSAKeys>();
  19.  
  20.       Task.Run(RegenerateKeys);
  21.     }
  22.    
  23.     public async Task Send(string message)
  24.     {
  25.       var decrypted = EncryptorRSA.DecryptText(message, KeyPairs[Context.ConnectionId].PrivateKey);
  26.       var parts = decrypted.Split("_|_");
  27.      
  28.       await Clients.All.SendAsync("Receive", parts.First(), parts.Last());
  29.     }
  30.  
  31.     public async Task RequestPublicKey()
  32.     {
  33.       var keyPair = EncryptorRSA.GenerateKeys(KeySize);
  34.       if (KeyPairs.TryAdd(Context.ConnectionId, keyPair))
  35.       {
  36.         Console.WriteLine($"Generated new key pair for client with ConnectionId '{Context.ConnectionId}:" +
  37.                           $"\n\tPublic key: {keyPair.PublicKey}" +
  38.                           $"\n\tPrivate key: {keyPair.PrivateKey}");
  39.        
  40.         await Clients.Caller.SendAsync("PublicKey", keyPair.PublicKey);
  41.       }
  42.     }
  43.  
  44.     public override Task OnConnectedAsync()
  45.     {
  46.       CallerClients = Clients;
  47.      
  48.       return base.OnConnectedAsync();
  49.     }
  50.  
  51.     public override Task OnDisconnectedAsync(Exception exception)
  52.     {
  53.       if (KeyPairs.ContainsKey(Context.ConnectionId))
  54.       {
  55.         KeyPairs.TryRemove(Context.ConnectionId, out _);
  56.       }
  57.      
  58.       return base.OnDisconnectedAsync(exception);
  59.     }
  60.  
  61.     private static async Task RegenerateKeys()
  62.     {
  63.       while (true)
  64.       {
  65.         await Task.Delay(TimeSpan.FromMinutes(30));
  66.  
  67.         foreach (var keyValue in KeyPairs)
  68.         {
  69.           var newPair = EncryptorRSA.GenerateKeys(KeySize);
  70.  
  71.           keyValue.Value.PublicKey = newPair.PublicKey;
  72.           keyValue.Value.PrivateKey = newPair.PrivateKey;
  73.  
  74.           Console.WriteLine($"Generated new key pair for client with ConnectionId '{keyValue.Key}:" +
  75.                             $"\n\tPublic key: {keyValue.Value.PublicKey}" +
  76.                             $"\n\tPrivate key: {keyValue.Value.PrivateKey}");
  77.          
  78.           await CallerClients.Caller.SendAsync("PublicKey", keyValue.Value.PublicKey);
  79.         }
  80.       }
  81.     }
  82.   }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement