Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- using System.Security.Cryptography;
- namespace CipherTest
- {
- static class Program
- {
- const long KeySize = 64;
- // OpenSSL DH Parameters
- static readonly BigInteger Generator = new BigInteger(2);
- static readonly BigInteger Prime = BigInteger.Parse("11435638110073884015312138951374632602058080675070521707579703088370446597672067452229024566834732449017970455481029703480957707976441965258194321262569523");
- static void Main()
- {
- RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
- byte[] bytes = new byte[KeySize];
- provider.GetBytes(bytes);
- BigInteger priv = new BigInteger(bytes, true);
- BigInteger pub = BigInteger.ModPow(Generator, priv, Prime);
- Console.WriteLine($"Your public key is:\n{pub}");
- string input;
- BigInteger other;
- while (true)
- {
- Console.WriteLine("\nEnter other public key:");
- input = Console.ReadLine();
- if (input.Length == 0)
- {
- Console.WriteLine("\nProgram terminated!");
- return;
- }
- else if (BigInteger.TryParse(input, out other))
- {
- break;
- }
- else
- {
- Console.WriteLine("\nCould not parse input!");
- }
- }
- BigInteger key = BigInteger.ModPow(other, priv, Prime);
- Cipher cipher = new Cipher(provider, key);
- Console.WriteLine("\nSetup complete!");
- string output;
- while (true)
- {
- Console.WriteLine("\nAwaiting input...");
- input = Console.ReadLine();
- if (input.Length == 0)
- {
- Console.WriteLine("\nProgram terminated!");
- return;
- }
- else if (input.StartsWith('~'))
- {
- input = input.Substring(1);
- if (Helpers.IsHexString(input))
- {
- output = cipher.Decrypt(input);
- if (output != null)
- {
- Console.WriteLine($"\n{output}");
- }
- else
- {
- Console.WriteLine("\nCould not parse input!");
- }
- }
- else
- {
- Console.WriteLine("\nCould not parse input!");
- }
- }
- else if (Helpers.IsValidString(input))
- {
- output = cipher.Encrypt(input);
- if (output != null)
- {
- Console.WriteLine($"\n{output}");
- }
- else
- {
- Console.WriteLine("\nCould not parse input!");
- }
- }
- else
- {
- Console.WriteLine("\nCould not parse input!");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement