Example #1
0
        public override string Decrypt(string text)
        {
            List<int> numbers = new List<int>();
            string[] stringNumbers = text.Split(' ');
            foreach (var stringNumber in stringNumbers)
            {
                numbers.Add(int.Parse(stringNumber));
            }

            StringBuilder result = new StringBuilder();

            foreach (var number in numbers)
            {

                Mathematics.ModularExponentiation modularExponention = new Mathematics.ModularExponentiation(number, privateKey[0], privateKey[1]);
                result.Append(alphabet[modularExponention.RaisedToThePowerModulo()]);
            }
            return result.ToString();
        }
Example #2
0
        public override string Encrypt(string text)
        {
            List<int> letterIndexes = new List<int>();
            foreach (var letter in text)
            {
                letterIndexes.Add(alphabet.Letters.IndexOf(letter));
            }
            generateKeysTest();
            StringBuilder resultStringBuilder = new StringBuilder();

            foreach (int letterIndex in letterIndexes)
            {

                Mathematics.ModularExponentiation modularExponention = new Mathematics.ModularExponentiation(letterIndex, PublicKey[0], PublicKey[1]);
                resultStringBuilder.Append(modularExponention.RaisedToThePowerModulo() + " ");
            }

            string result = resultStringBuilder.ToString();
            result = result.Substring(0, result.Length - 1);

            return result;
        }
Example #3
0
        private string algorithmExample()
        {
            basis = Mathematics.PrimeNumbers.GetPrime();
            moduler = Mathematics.PrimeNumbers.GetPrime();

            int a = Mathematics.PrimeNumbers.GetPrime();
            Mathematics.ModularExponentiation meA = new Mathematics.ModularExponentiation(basis, a, moduler);
            int A = meA.RaisedToThePowerModulo();


            int b = Mathematics.PrimeNumbers.GetPrime();
            Mathematics.ModularExponentiation meB = new Mathematics.ModularExponentiation(basis, b, moduler);
            int B = meB.RaisedToThePowerModulo();

            int keyA = Mathematics.ModularExponentiation.RaisedToThePowerModulo(B, a, moduler);
            int keyB = Mathematics.ModularExponentiation.RaisedToThePowerModulo(A, b, moduler);

            return  "1) Generated two prime numbers: p = " + basis.ToString() + ", g = " + moduler.ToString() + "\n" +
                    "2) Alice choosed number a = " + a.ToString() + " and sent to Bob number A = " + A.ToString() + "\n" +
                    "3) Bob choosed number b = " + a.ToString() + " and sent to Alice number B = " + B.ToString() + "\n" +
                    "4) Alice generate her key = " + keyA.ToString() + "\n" +
                    "5) Bob generate his key = " + keyB.ToString();
        }