Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Org.BouncyCastle.Math;
  7.  
  8. namespace secret
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("Bartłomiej Wolnik, gr.A");
  15.             string secretString = getRandomSecretString(secretSize());
  16.             Console.WriteLine("Sekret: " + secretString);
  17.             Console.WriteLine("--------");
  18.             int[] senatorNumbers = { 6, 4, 10 };
  19.             int[] requiredAcceptances = { 3, 3, 6 };
  20.             BigInteger[] regionSecrets = new BigInteger[3];
  21.  
  22.             // podział na 3 regiony
  23.             for (int i = 0; i < 3; i++)
  24.             {
  25.                 string regionSecretString = secretString.Substring(i * (secretString.Length / 3), secretString.Length / 3);
  26.                 regionSecrets[i] = new BigInteger(regionSecretString);
  27.             }
  28.  
  29.             // każdy region osobno
  30.             for (int i = 0; i < 3; i++)
  31.             {
  32.                 BigInteger secret = regionSecrets[i];
  33.                 BigInteger prime = secret.NextProbablePrime();
  34.                 Console.WriteLine();
  35.                 Console.WriteLine("Sekret dla regionu " + i + ": " + secret);
  36.                 Console.WriteLine("Liczba pierwsza: " + prime);
  37.                 BigInteger[] elements = new BigInteger[requiredAcceptances[i] - 1];
  38.                 BigInteger[] shares = new BigInteger[senatorNumbers[i]];
  39.  
  40.                 // wylosowanie skalarów dla X
  41.                 for (int j = 0; j < requiredAcceptances[i] - 1; j++)
  42.                 {
  43.                     BigInteger randomBigInteger;
  44.                     do
  45.                     {
  46.                         randomBigInteger = new BigInteger(getRandomSecretString(secretSize()/3));
  47.                     } while (randomBigInteger.CompareTo(secret) > 0);
  48.                     elements[j] = randomBigInteger;
  49.                 }
  50.  
  51.                 Console.WriteLine("Udziały: ");
  52.                 // przydzielenie udziałów wg wzoru
  53.                 for (int x = 1; x <= senatorNumbers[i]; x++)
  54.                 {
  55.                     BigInteger W = new BigInteger("0");
  56.                     for (int power = 1; power < requiredAcceptances[i]; power++)
  57.                     {
  58.                         BigInteger xBigInt = new BigInteger(x.ToString());
  59.                         BigInteger el = xBigInt.Pow(power).Multiply(elements[power - 1]);
  60.                         W = W.Add(el);
  61.                     }
  62.                     W = W.Add(secret);
  63.                     shares[x - 1] = W.Mod(prime);
  64.                     Console.WriteLine(x + ", " + shares[x-1]);
  65.                 }
  66.  
  67.                 // odtwarzanie sekretu na podstawie znanych kilku udziałów
  68.                 var watch = System.Diagnostics.Stopwatch.StartNew();  // ZEGAREK
  69.                 BigInteger M = new BigInteger("0");
  70.                 for (int x = 1; x <= requiredAcceptances[i]; x++)
  71.                 {
  72.                     BigInteger xBigInt = new BigInteger(x.ToString());
  73.                     BigInteger part = shares[x-1];
  74.                     for (int xup = x+1; xup <= requiredAcceptances[i]; xup++)
  75.                     {
  76.                         BigInteger xupBigInt = new BigInteger(xup.ToString());
  77.                         BigInteger tmp = xupBigInt.Multiply(new BigInteger("-1")).Mod(prime);
  78.                         BigInteger tmp2 = xBigInt.Subtract(xupBigInt).Mod(prime).ModInverse(prime);  // TUTAJ ZMIANA
  79.                         part = part.Multiply(tmp).Multiply(tmp2);  // TUTAJ ZMIANA
  80.                     }
  81.                     for (int xdown = x-1; xdown >= 1; xdown--)
  82.                     {
  83.                         BigInteger xdownBigInt = new BigInteger(xdown.ToString());
  84.                         BigInteger tmp = xdownBigInt.Multiply(new BigInteger("-1")).Mod(prime); // CHYBA TUTAJ MAŁY FIX
  85.                         BigInteger tmp2 = xBigInt.Subtract(xdownBigInt).ModInverse(prime);
  86.                         part = part.Multiply(tmp).Multiply(tmp2);  // TUTAJ ZMIANA
  87.                     }
  88.                     M = M.Add(part);
  89.                 }
  90.                 M = M.Mod(prime);
  91.                 watch.Stop();   // ZEGAREK OFF
  92.                 var elapsedMs = watch.Elapsed.TotalMilliseconds;   // CZAS W MS
  93.                 Console.WriteLine("Odtworzony sekret: " + M);
  94.                 Console.WriteLine("Czas odtwarzania sekretu: " + elapsedMs + "ms");  // POKAZ CZAS
  95.             }
  96.             return;
  97.         }
  98.  
  99.         private static Random random = new Random();
  100.  
  101.         static int secretSize()
  102.         {
  103.             return 90;
  104.         }
  105.         static string getRandomSecretString(int length)
  106.         {
  107.             const string chars = "0123456789";
  108.             return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement