Exemplo n.º 1
0
        }                           //число которое расшифровали.

        public RSA(long p, long q, long x)
        {
            try
            {
                if (!Supporting.Simple(p) || !Supporting.Simple(q))
                {
                    throw new Exception("p и q обязаны быть простыми для метода RSA");
                }
                this.p = p;
                this.q = q;
                n      = p * q;
                fi     = (p - 1) * (q - 1);
                e      = Supporting.GetMutuallySimple(fi);
                d      = Supporting.ResolveModuleEquation(1, n - 1, e, fi, 1);
                if (!(0 < x && x < n))
                {
                    throw new Exception("x должен быть больше нуля и меньше n");
                }
                this.x = x;
                C      = Supporting.bin_pow(x, e, n);
                if (!(0 < C && C < n))
                {
                    throw new Exception("C должен быть больше нуля и меньше n");
                }
                X = Supporting.bin_pow(C, d, n);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
Exemplo n.º 2
0
        public Rabin(long p, long q, long x)
        {
            long a = 0;
            long b = 0;
            long d;

            try
            {
                if (!(Supporting.Simple(p) && Supporting.Simple(q) && p % 4 == 3 && q % 4 == 3))
                {
                    throw new Exception("p и q обязаны быть простыми для метода Rabbin ,а также для удобства просчёта p и q по модулю 4 обязаны равняться трём");
                }
                this.p = p;
                this.q = q;
                this.x = x;


                n = p * q;
                c = Supporting.bin_pow(x, 2, n);
                //зашифрование

                r = Supporting.bin_pow(c, (p + 1) / 4, p);
                s = Supporting.bin_pow(c, (q + 1) / 4, q);

                Supporting.ResolveDeofantovoEquation(ref a, ref b, p, q, 1);
                Supporting.extendedEuclid(p, q, out a, out b, out d, out outputExtendEuqlid);
                this.a = a;
                this.b = b;
                m1     = +((a * p * s + b * q * r)) % n;
                if (m1 < 0)
                {
                    m1 += n;
                }
                m2 = -((a * p * s + b * q * r)) % n;
                if (m2 < 0)
                {
                    m2 += n;
                }
                m3 = +((a * p * s - b * q * r)) % n;
                if (m3 < 0)
                {
                    m3 += n;
                }
                m4 = -((a * p * s - b * q * r)) % n;
                if (m4 < 0)
                {
                    m4 += n;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
Exemplo n.º 3
0
        public ElGamale(long p, long g, long X, long K, long Message)
        {
            try
            {
                if (!Supporting.Simple(p))
                {
                    throw new Exception("p should be simple");
                }

                r = new Random(DateTime.Now.Second);
                if (X == 0)
                {
                    x = r.Next(1, (int)p);
                }
                else
                {
                    x = X;
                }
                this.p = p;
                this.g = g;
                Y      = Supporting.bin_pow(g, x, p);
                m      = Message;
                if (K == 0)
                {
                    while (!Supporting.MutuallySimple(k, p - 1))
                    {
                        k = r.Next(1, (int)p - 1);
                    }
                }

                else
                {
                    k = K;
                    if (!(1 < k && k < p - 1) && !Supporting.MutuallySimple(k, p - 1))
                    {
                        throw new Exception("k - сессионный ключ обязан придерживаться условия 1<k<p-1");
                    }
                }
                A = Supporting.bin_pow(g, k, p);

                B = (Supporting.bin_pow(Y, k, p) * (m % p)) % p;

                DecodeM = ((B % p) * Supporting.divide_pow(A, x, p)) % p;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 4
0
        }                          // Y = g^y mod n;

        /// <summary>
        ///
        /// </summary>
        /// <param name="n"></param>
        /// <param name="g"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <exception cref="Exception"></exception>
        public DiffieHellman(long n, long g, long x, long y)
        {
            try
            {
                this.n = n;
                if (!Supporting.Simple(n))
                {
                    throw new Exception("N should be simple");
                }
                if (g == 0)
                {
                    g = Supporting.PrimitivsMultiGroupZ(out string temp, n);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 5
0
            }                           // y = g^x mod p

            public PublicKey(long p, long g, SecretKey sk)
            {
                try
                {
                    if (!Supporting.Simple(p))
                    {
                        throw new Exception("p should be simple");
                    }
                    this.p = p;
                    if (!(1 < g && g < p))
                    {
                        throw new Exception("Condition 1<g<p is not met");
                    }
                    this.g = g;
                    this.y = Supporting.bin_pow(g, sk.x, p);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }