Example #1
0
        private static void EncodeFile()
        {
            Console.Write("Enter first prime number: ");
            var p = new MyBigInteger(Console.ReadLine());

            Console.Write("Enter second prime number: ");
            var q = new MyBigInteger(Console.ReadLine());

            var module = p * q;

            Console.WriteLine($"Your module: {module}");

            var phi            = (p - MyBigInteger.One) * (q - MyBigInteger.One);
            var publicExponent = AlgorithmRSA.CalculatePublicExponent(phi);
            var secretExponent = AlgorithmRSA.CalculateSecretExponent(publicExponent, phi);

            Console.WriteLine($"Your secret exponent: {secretExponent}");

            var message =
                File.ReadAllBytes(@"C:\Users\diyll\Laboratory\ConsoleApp1\ConsoleApp1\FileToEncode.txt")
                .Select(x => (int)x);
            var encoded = AlgorithmRSA.Encode(message, publicExponent, module);

            Console.WriteLine("Your crypted data:");
            foreach (var integer in encoded)
            {
                Console.WriteLine(integer.ToString());
            }
        }
Example #2
0
        public void IntegerShouldEqualCorrectly(long value)
        {
            var first  = new MyBigInteger(value.ToString());
            var second = new MyBigInteger(value.ToString());

            Assert.That(first == second, Is.True);
        }
Example #3
0
        private static void Encode()
        {
            Console.Write("Enter first prime number: ");
            var p = new MyBigInteger(Console.ReadLine());

            Console.Write("Enter second prime number: ");
            var q = new MyBigInteger(Console.ReadLine());

            var module = p * q;

            Console.WriteLine($"Your module: {module}");

            var phi            = (p - MyBigInteger.One) * (q - MyBigInteger.One);
            var publicExponent = AlgorithmRSA.CalculatePublicExponent(phi);
            var secretExponent = AlgorithmRSA.CalculateSecretExponent(publicExponent, phi);

            Console.WriteLine($"Your secret exponent: {secretExponent}");

            Console.WriteLine("Enter your message:");
            var message = Encoding.ASCII
                          .GetBytes(Console.ReadLine() ?? string.Empty)
                          .Select(x => (int)x)
                          .ToArray();

            var encode = AlgorithmRSA.Encode(message, publicExponent, module);

            Console.WriteLine("Your crypted data:");
            foreach (var integer in encode)
            {
                Console.WriteLine(integer);
            }
        }
Example #4
0
        public static MyBigInteger ModPow(MyBigInteger value, MyBigInteger power, MyBigInteger module)
        {
            var binaryValue = ConvertToBinary(power);

            var arr = new MyBigInteger[binaryValue.Count];

            arr[0] = value;
            for (var i = 1; i < binaryValue.Count; i++)
            {
                arr[i] = arr[i - 1] * arr[i - 1] % module;
            }

            var multiplication = One;
            var zero           = Zero;

            for (var j = 0; j < binaryValue.Count; j++)
            {
                if (binaryValue[j] > zero)
                {
                    multiplication *= binaryValue[j] * arr[j];
                }
            }

            return(multiplication % module);
        }
Example #5
0
        public void IntegerShouldSubtractBySumCorrectly(string first, string second,
                                                        string expected)
        {
            var sum = new MyBigInteger(first) + new MyBigInteger(second);

            Assert.AreEqual(expected, sum.ToString());
        }
Example #6
0
        private static MyBigInteger Mod(MyBigInteger a, MyBigInteger b)
        {
            var result = Zero;

            for (var i = a.Size - 1; i >= 0; i--)
            {
                result += Exp(a.GetByte(i), i);

                var x     = 0;
                var left  = 0;
                var right = 10;

                while (left <= right)
                {
                    var middle  = (left + right) >> 1;
                    var current = b * Exp((byte)middle, i);
                    if (current <= result)
                    {
                        x    = middle;
                        left = middle + 1;
                    }
                    else
                    {
                        right = middle - 1;
                    }
                }

                result -= b * Exp((byte)x, i);
            }

            result.RemoveNulls();

            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }
Example #7
0
        public static MyBigInteger GetModInverse(MyBigInteger a, MyBigInteger n)
        {
            var gdc = GreatestCommonDivisor(a, n, out var x, out var _);

            if (gdc != One)
            {
                return(Zero);
            }
            return((x % n + n) % n);
        }
Example #8
0
        public void IntegerToStringCorrectly(string value)
        {
            var integer = new MyBigInteger(value);

            if (value.StartsWith('+'))
            {
                value = value.Substring(1);
            }
            Assert.AreEqual(value, integer.ToString());
        }
Example #9
0
        public static List <MyBigInteger> Encode(IEnumerable <int> bytes, MyBigInteger e, MyBigInteger module)
        {
            var result = new List <MyBigInteger>();

            foreach (var b in bytes)
            {
                var integer = MyBigInteger.ModPow(new MyBigInteger(b.ToString()), e, module);
                result.Add(integer);
            }
            return(result);
        }
Example #10
0
        public static int[] Decode(List <MyBigInteger> integers, MyBigInteger d, MyBigInteger module)
        {
            var result = new List <int>();

            foreach (var integer in integers)
            {
                var code = MyBigInteger.ModPow(integer, d, module);
                result.Add(int.Parse(code.ToString()));
            }
            return(result.ToArray());
        }
Example #11
0
        private static int CompareSize(MyBigInteger a, MyBigInteger b)
        {
            if (a.Size < b.Size)
            {
                return(-1);
            }
            else if (a.Size > b.Size)
            {
                return(1);
            }

            return(CompareDigits(a, b));
        }
Example #12
0
        private static List <MyBigInteger> ConvertToBinary(MyBigInteger value)
        {
            var copy   = new MyBigInteger(value.Sign, value.digits.ToList());
            var two    = new MyBigInteger(2);
            var result = new List <MyBigInteger>();

            while (!copy.IsZero)
            {
                result.Add(copy % two);
                copy /= two;
            }

            return(result);
        }
Example #13
0
        public static MyBigInteger CalculatePublicExponent(MyBigInteger module)
        {
            var exponent = new MyBigInteger(3);
            var one      = MyBigInteger.One;

            for (var i = new MyBigInteger(0); i < module; i++)
            {
                if (MyBigInteger.GreatestCommonDivisor(exponent, module, out var _, out var _) == one)
                {
                    return(exponent);
                }
                exponent += one;
            }
            return(exponent);
        }
Example #14
0
        public void CorrectDecode(string value, string number1, string number2)
        {
            var p              = new MyBigInteger(number1);
            var q              = new MyBigInteger(number2);
            var module         = p * q;
            var phi            = (p - MyBigInteger.One) * (q - MyBigInteger.One);
            var exponent       = AlgorithmRSA.CalculatePublicExponent(phi);
            var secretExponent = AlgorithmRSA.CalculateSecretExponent(exponent, phi);
            var chars          = value.ToCharArray().Select(x => (int)(byte)x).ToArray();
            var encoded        = AlgorithmRSA.Encode(chars,
                                                     exponent, module);
            var result = AlgorithmRSA.Decode(encoded, secretExponent, module);

            CollectionAssert.AreEqual(result, chars);
        }
Example #15
0
        public static MyBigInteger GreatestCommonDivisor(MyBigInteger number, MyBigInteger modulo, out MyBigInteger x,
                                                         out MyBigInteger y)
        {
            if (number.IsZero)
            {
                x = Zero;
                y = One;
                return(modulo);
            }

            var d = GreatestCommonDivisor(modulo % number, number, out var x1, out var y1);

            x = y1 - (modulo / number) * x1;
            y = x1;
            return(d);
        }
Example #16
0
        private static int CompareSign(MyBigInteger a, MyBigInteger b, bool ignoreSign = false)
        {
            if (!ignoreSign)
            {
                if (a.Sign < b.Sign)
                {
                    return(-1);
                }

                if (a.Sign > b.Sign)
                {
                    return(1);
                }
            }

            return(CompareSize(a, b));
        }
Example #17
0
        private static MyBigInteger Multiply(MyBigInteger a, MyBigInteger b)
        {
            var result = Zero;

            for (var i = 0; i < a.Size; i++)
            {
                for (int j = 0, reduce = 0; (j < b.Size) || (reduce > 0); j++)
                {
                    var sum = result.GetByte(i + j) + a.GetByte(i) * b.GetByte(j) + reduce;
                    result.SetByte(i + j, (byte)(sum % 10));
                    reduce = sum / 10;
                }
            }

            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }
Example #18
0
        private static MyBigInteger Subtract(MyBigInteger a, MyBigInteger b)
        {
            var digits  = new List <byte>();
            var max     = Zero;
            var min     = Zero;
            var compare = Comparison(a, b, ignoreSign: true);

            switch (compare)
            {
            case -1:
                min = a;
                max = b;
                break;

            case 0:
                return(Zero);

            case 1:
                min = b;
                max = a;
                break;
            }

            var maxLength = Math.Max(a.Size, b.Size);

            var reduce = 0;

            for (var i = 0; i < maxLength; i++)
            {
                var sub = max.GetByte(i) - min.GetByte(i) - reduce;
                if (sub < 0)
                {
                    sub   += 10;
                    reduce = 1;
                }
                else
                {
                    reduce = 0;
                }

                digits.Add((byte)sub);
            }

            return(new MyBigInteger(max.Sign, digits));
        }
Example #19
0
        private static int CompareDigits(MyBigInteger a, MyBigInteger b)
        {
            var maxLength = Math.Max(a.Size, b.Size);

            for (var i = maxLength; i >= 0; i--)
            {
                if (a.GetByte(i) < b.GetByte(i))
                {
                    return(-1);
                }
                if (a.GetByte(i) > b.GetByte(i))
                {
                    return(1);
                }
            }

            return(0);
        }
Example #20
0
        private static MyBigInteger Add(MyBigInteger a, MyBigInteger b)
        {
            var digits    = new List <byte>();
            var maxLength = Math.Max(a.Size, b.Size);
            var reduce    = 0;

            for (var i = 0; i < maxLength; i++)
            {
                var sum = (byte)(a.GetByte(i) + b.GetByte(i) + reduce);
                reduce = sum / 10;
                digits.Add((byte)(sum % 10));
            }

            if (reduce > 0)
            {
                digits.Add((byte)reduce);
            }

            return(new MyBigInteger(a.Sign, digits));
        }
Example #21
0
        public static MyBigInteger Pow(MyBigInteger value, MyBigInteger power)
        {
            var two = new MyBigInteger(2);

            if (power.IsZero)
            {
                return(One);
            }
            if (power == One)
            {
                return(value);
            }
            if (power % two == One)
            {
                return(value * Pow(value, power - One));
            }
            var b = Pow(value, power / two);

            return(b * b);
        }
Example #22
0
        private static void Decode()
        {
            Console.WriteLine("Enter your module: ");
            var module = new MyBigInteger(Console.ReadLine());

            Console.WriteLine("Enter your secret exponent: ");
            var secretExponent = new MyBigInteger(Console.ReadLine());

            Console.WriteLine("Enter your crypted data per line: ");
            var crypted = new List <MyBigInteger>();
            var line    = Console.ReadLine();

            while (!string.IsNullOrEmpty(line))
            {
                crypted.Add(new MyBigInteger(line));
                line = Console.ReadLine();
            }

            var chars  = AlgorithmRSA.Decode(crypted, secretExponent, module);
            var result = Encoding.ASCII.GetString(chars.Select(x => (byte)x).ToArray());

            Console.WriteLine($"Your message was {result}");
        }
Example #23
0
        private static MyBigInteger Div(MyBigInteger a, MyBigInteger b)
        {
            var result  = Zero;
            var current = Zero;

            for (var i = a.Size - 1; i >= 0; i--)
            {
                current += Exp(a.GetByte(i), i);

                var x     = 0;
                var left  = 0;
                var right = 10;
                while (left <= right)
                {
                    var middle = (left + right) / 2;
                    var cur    = b * Exp((byte)middle, i);
                    if (cur <= current)
                    {
                        x    = middle;
                        left = middle + 1;
                    }
                    else
                    {
                        right = middle - 1;
                    }
                }

                result.SetByte(i, (byte)(x % 10));
                var reduce = b * Exp((byte)x, i);
                current -= reduce;
            }

            result.RemoveNulls();
            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }
Example #24
0
        public void IntegerOverLongShouldSumCorrectly(long a, long b, string expected)
        {
            var actual = new MyBigInteger(a.ToString()) + new MyBigInteger(b.ToString());

            Assert.AreEqual(expected, actual.ToString());
        }
Example #25
0
        public void IntegerShouldGetRightToString(long value)
        {
            var integer = new MyBigInteger(value.ToString());

            Assert.AreEqual(value.ToString(), integer.ToString());
        }
Example #26
0
        public void IntegerShouldModInverseCorrectly(string first, string second, string expected)
        {
            var result = MyBigInteger.GetModInverse(new MyBigInteger(first), new MyBigInteger(second)).ToString();

            Assert.AreEqual(expected, result);
        }
Example #27
0
        public void IntegerShouldPowCorrectly(string value, string power, string expected)
        {
            var result = MyBigInteger.Pow(new MyBigInteger(value), new MyBigInteger(power)).ToString();

            Assert.AreEqual(expected, result);
        }
Example #28
0
        public void IntegerShouldDivCorrectly(string first, string second, string expected)
        {
            var div = new MyBigInteger(first) / new MyBigInteger(second);

            Assert.AreEqual(expected, div.ToString());
        }
Example #29
0
        public void IntegerShouldSubtractCorrectly(string first, string second, string expected)
        {
            var sub = new MyBigInteger(first) - new MyBigInteger(second);

            Assert.AreEqual(sub.ToString(), expected);
        }
Example #30
0
 public static MyBigInteger CalculateSecretExponent(MyBigInteger exponent, MyBigInteger phi)
 {
     return(MyBigInteger.GetModInverse(exponent, phi));
 }