Advertisement
tomdodd4598

Untitled

Jun 2nd, 2021
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using System.Security.Cryptography;
  4.  
  5. namespace CipherTest
  6. {
  7.     static class Program
  8.     {
  9.         const long KeySize = 64;
  10.  
  11.         // OpenSSL DH Parameters
  12.         static readonly BigInteger Generator = new BigInteger(2);
  13.         static readonly BigInteger Prime = BigInteger.Parse("11435638110073884015312138951374632602058080675070521707579703088370446597672067452229024566834732449017970455481029703480957707976441965258194321262569523");
  14.  
  15.         static void Main()
  16.         {
  17.             RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
  18.  
  19.             byte[] bytes = new byte[KeySize];
  20.             provider.GetBytes(bytes);
  21.  
  22.             BigInteger priv = new BigInteger(bytes, true);
  23.             BigInteger pub = BigInteger.ModPow(Generator, priv, Prime);
  24.  
  25.             Console.WriteLine($"Your public key is:\n{pub}");
  26.  
  27.             string input;
  28.  
  29.             BigInteger other;
  30.  
  31.             while (true)
  32.             {
  33.                 Console.WriteLine("\nEnter other public key:");
  34.                 input = Console.ReadLine();
  35.  
  36.                 if (input.Length == 0)
  37.                 {
  38.                     Console.WriteLine("\nProgram terminated!");
  39.                     return;
  40.                 }
  41.                 else if (BigInteger.TryParse(input, out other))
  42.                 {
  43.                     break;
  44.                 }
  45.                 else
  46.                 {
  47.                     Console.WriteLine("\nCould not parse input!");
  48.                 }
  49.             }
  50.  
  51.             BigInteger key = BigInteger.ModPow(other, priv, Prime);
  52.             Cipher cipher = new Cipher(provider, key);
  53.  
  54.             Console.WriteLine("\nSetup complete!");
  55.  
  56.             string output;
  57.  
  58.             while (true)
  59.             {
  60.                 Console.WriteLine("\nAwaiting input...");
  61.                 input = Console.ReadLine();
  62.  
  63.                 if (input.Length == 0)
  64.                 {
  65.                     Console.WriteLine("\nProgram terminated!");
  66.                     return;
  67.                 }
  68.                 else if (input.StartsWith('~'))
  69.                 {
  70.                     input = input.Substring(1);
  71.                     if (Helpers.IsHexString(input))
  72.                     {
  73.                         output = cipher.Decrypt(input);
  74.                         if (output != null)
  75.                         {
  76.                             Console.WriteLine($"\n{output}");
  77.                         }
  78.                         else
  79.                         {
  80.                             Console.WriteLine("\nCould not parse input!");
  81.                         }
  82.                     }
  83.                     else
  84.                     {
  85.                         Console.WriteLine("\nCould not parse input!");
  86.                     }
  87.                 }
  88.                 else if (Helpers.IsValidString(input))
  89.                 {
  90.                     output = cipher.Encrypt(input);
  91.                     if (output != null)
  92.                     {
  93.                         Console.WriteLine($"\n{output}");
  94.                     }
  95.                     else
  96.                     {
  97.                         Console.WriteLine("\nCould not parse input!");
  98.                     }
  99.                 }
  100.                 else
  101.                 {
  102.                     Console.WriteLine("\nCould not parse input!");
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement