public ElgamalResultOfEncryptionHash(BigInteger a, BigInteger b, WrappedInteger y, WrappedInteger hash)
 {
     A = a;
     B = b;
     Y = y.Value;
     M = hash.Value;
 }
Ejemplo n.º 2
0
        public IResultOfEncryptionHash EncryptHashCode(BigInteger hash, WrappedInteger recipientOpenedKey)
        {
            var p = Subscriber.P;
            var g = Subscriber.G;
            var q = Subscriber.Q;
            var x = Subscriber.ClosedKey;
            var y = Subscriber.OpenedKey;

            var k = PrimeNumberGenerator.GeneratePrimeNumber(1, (q - 1).ToInt32()); // 3;

            //hash = 9;

            var r        = (g.ВСтепень(k) % p) % q;
            var inverseK = (CryptoFormula.АлгоритмЕвклида(q, k, out var euErr) ?? throw new Exception(euErr));
            var s        = inverseK * (hash + ((x * r) % q)) % q;

            var result = new DSAResultOfEncryptionHash(r, s, Subscriber.OpenedKey, hash)
            {
                P = Subscriber.P.Value,
                Q = Subscriber.G.Value,
                G = Subscriber.Q.Value,
                K = k,
                X = Subscriber.ClosedKey.Value,
            };

            return(result);
        }
 public DSAResultOfEncryptionHash(BigInteger r, BigInteger s, WrappedInteger y, WrappedInteger m)
 {
     R = r;
     S = s;
     Y = y.Value;
     M = m.Value;
 }
Ejemplo n.º 4
0
        public static BigInteger ВСтепень(this WrappedInteger число, WrappedInteger степень)
        {
            var result = число.Value;

            for (int i = 2; i <= степень; i++)
            {
                result *= число.Value;
            }

            return(result);
        }
Ejemplo n.º 5
0
        /// <summary> Нахождение наибольшего общего делителя чисел a и b. </summary>
        public static BigInteger НайтиНОД(WrappedInteger a, WrappedInteger b)
        {
            if (a == 0)
            {
                return(b.Value);
            }
            else if (b == 0)
            {
                return(a.Value);
            }

            else
            {
                return(НайтиНОД(b, a % b));
            }
        }
Ejemplo n.º 6
0
        public IResultOfEncryptionHash EncryptHashCode(BigInteger hash, WrappedInteger recipientOpenedKey)
        {
            var p = Subscriber.P;
            var g = Subscriber.G;
            //var k = 9;
            WrappedInteger k = PrimeNumberGenerator.GeneratePrimeNumber(1, (p - 2).ToInt32());

            while (CryptoFormula.НайтиНОД(k, p - 1) != 1)
            {
                k = PrimeNumberGenerator.GeneratePrimeNumber(1, (p - 2).ToInt32());
            }
            k    %= p - 1;
            hash %= p - 1;

            var a = g.ВСтепень(k) % p;
            var b = (CryptoFormula.АлгоритмЕвклида(p - 1, k, out var euErr) ?? throw new Exception(euErr))
                    * ((p - 1) + (hash - ((Subscriber.ClosedKey * a) % (p - 1)))) % (p - 1);

            return(new ElgamalResultOfEncryptionHash(a, b, Subscriber.OpenedKey, hash));
        }
Ejemplo n.º 7
0
        public static bool IsPrime(WrappedInteger num)
        {
            var number = num.Value;

            if ((number & 1) == 0)
            {
                return(number == 2);
            }

            var limit = Math.Sqrt((double)number);

            for (uint i = 3; i <= limit; i += 2)
            {
                if ((number % i) == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Формула Эйлера для нахождения мультипликативно обратных элементов.
        /// Если не будет решения, то придёт null.
        /// </summary>
        /// <param name="solutionError"> Подробности об ошибке, если была. </param>
        public static BigInteger?ФормулаЭйлера(WrappedInteger module, WrappedInteger num, out List <int> divisorsList, out string solutionError)
        {
            solutionError = null;
            divisorsList  = module.азложитьНаПростыеМножители();

            if (divisorsList.Count == 1)
            {
                return(num.ВСтепень(module - 2) % module);
            }
            else
            {
                if (divisorsList.Distinct().Count() == divisorsList.Count())
                {
                    return(num.ВСтепень(divisorsList.Select(x => x -= 1).Aggregate((x, y) => x * y) - 1));
                }
                else
                {
                    solutionError = $"Нет решения. Множители повторяются.";
                    return(null);
                }
            }
        }
Ejemplo n.º 9
0
        public static List <int> азложитьНаПростыеМножители(this WrappedInteger num)
        {
            var listDivisors = new List <int>();

            for (; num % 2 == 0; num /= 2)
            {
                listDivisors.Add(2);
            }

            for (int i = 3; i <= num;)
            {
                if (num % i == 0)
                {
                    listDivisors.Add(i);
                    num /= i;
                }
                else
                {
                    i += 2;
                }
            }

            return(listDivisors);
        }
Ejemplo n.º 10
0
 public static BigInteger CalculateOpenedKey(WrappedInteger closedKey, int g, int p)
 {
     return(g.ВСтепень(closedKey) % p);
 }
 public IResultOfEncryptionHash EncryptHashCode(BigInteger hashCode, WrappedInteger recipientOpenedKey)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 12
0
 public GOST_34_2012Subscriber(WrappedInteger openedKey, WrappedInteger closedKey, string name) : base(openedKey, closedKey, name)
 {
     Controller = new GOST_34_2012EDSController(this);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Алгоритм Евклида для нахождения мультипликативно обратных элементов. Если решения не было, то будет возвращён null.
        /// Так же, в этом случае будет выведена подробная информация о ошибке в переменную solutionError.
        /// </summary>
        /// <param name="solutionError"> Подробности об ошибке, если была. </param>
        public static BigInteger?АлгоритмЕвклида(WrappedInteger module, WrappedInteger num, out string solutionError)
        {
            if (num == 1)
            {
                solutionError = null;
                return(1);
            }

            solutionError = null;
            (BigInteger? ЧислоНапротивЕдиницы, bool?БылЛиМинусНапротивЕдиницы)result = (null, null);

            var leftList  = new Dictionary <(BigInteger, BigInteger), BigInteger>();
            var rightList = new List <BigInteger>()
            {
                0, 1
            };
            int i = 0;

            { // Ввод данных в алгоритм
                i++;
                leftList.Add((module.Value, num.Value), module % num);
                rightList.Add(module / num * rightList.Last() + rightList[rightList.Count - 2]);
                if (leftList.Last().Value == 1)
                {
                    result = (rightList.Last(), i % 2 == 1);
                }
            }

            while (leftList.Last().Value != 0)
            {
                i++;
                var currLeftItem  = leftList.Last().Key.Item2;
                var currRightItem = leftList.Last().Value;
                leftList.Add((currLeftItem, currRightItem), currLeftItem % currRightItem);
                rightList.Add((currLeftItem / currRightItem) * rightList.Last() + rightList[rightList.Count - 2]);

                if (leftList.Last().Value == 1)
                {
                    result = (rightList.Last(), i % 2 == 1);
                }
            }

            if (module != rightList.Last())
            {
                solutionError = $"Модуль не сошёлся с числом напротив нуля: {module} != {rightList.Last()}.";
                return(null);
            }

            if (result.ЧислоНапротивЕдиницы == null)
            {
                solutionError = $"В левом списке не было единицы.";
                return(null);
            }

            if (result.БылЛиМинусНапротивЕдиницы == false)
            {
                return(result.ЧислоНапротивЕдиницы);
            }
            else
            {
                return(module - result.ЧислоНапротивЕдиницы);
            }
        }
Ejemplo n.º 14
0
 public static BigInteger ВСтепень(this int число, WrappedInteger степень) =>
 ((WrappedInteger)число).ВСтепень(степень);
Ejemplo n.º 15
0
 public Subscriber(WrappedInteger openedKey, WrappedInteger closedKey, string name)
 {
     OpenedKey = openedKey;
     ClosedKey = closedKey;
     Name      = name;
 }