Esempio n. 1
0
        bool checkP(Matrix Gp, Matrix P1_inv, Ham ham, Matrix Zero, int s, int n)
        {
            bool result = true;

            for (int i = 0; i < s - 1; ++i)
            {
                result &= MatrixFuncs.SubMatrix(Gp * P1_inv, i * n, (i + 1) * n) * MatrixFuncs.Transp(ham.H) == Zero;
            }
            return(result);
        }
Esempio n. 2
0
        public Matrix R;  // secret key

        public double KeyGen(CodeType codetype, int[] param)
        {
            Code code = new Code();
            int  k = 0, n = 0, r, m, l = param[2];

            switch (codetype)
            {
            case CodeType.Hamming:
                r    = param[0];
                code = new Ham(r);
                break;

            case CodeType.RM:
                r    = param[0]; m = param[1];
                code = new RM(r, m);
                break;
            }
            k = code.G.size[0]; n = code.G.size[1];
            Stopwatch t = new Stopwatch(); t.Start();

            //Matrix N
            N = MatrixFuncs.GenNoise(k);

            //Matrix P
            int nn = n + l; P = new Matrix(nn, nn);

            int[] check = new int[nn]; int ind = 0;
            for (int i = 0; i < nn; ++i)
            {
                do
                {
                    ind = Common.GetRand() % nn;
                } while (check[ind] == 1);
                P.data[ind][i] = 1;
                check[ind]     = 1;
            }

            //Matrix R
            R = new Matrix(k, l);
            for (int i = 0; i < k; ++i)
            {
                for (int j = 0; j < l; ++j)
                {
                    R.data[i][j] = Common.GetRand() & 1;
                }
            }

            Gp = N * (code.G | R) * P;
            t.Stop();
            return(t.ElapsedMilliseconds / 1000.00);
        }
Esempio n. 3
0
        public Matrix P;        // secret key

        public double KeyGen(CodeType codetype, int[] param)
        {
            Code code = new Code();
            int  k, n, r = 0, s = 0, m, nn;

            switch (codetype)
            {
            case CodeType.Hamming:
                r    = param[0];
                s    = param[1];
                code = new Ham(r);
                break;

            case CodeType.RM:
                r    = param[0]; m = param[1]; s = param[2];
                code = new RM(r, m);
                break;
            }

            k = code.G.size[0]; n = code.G.size[1]; nn = n * s;
            Stopwatch t = new Stopwatch(); t.Start();

            //Matrix list N
            N = new List <Matrix>();
            Matrix N_i, NG = new Matrix();

            for (int ii = 0; ii < s; ++ii)
            {
                N_i = MatrixFuncs.GenNoise(k);
                N.Add(N_i);
                NG |= N_i * code.G;
            }

            //Matrix P
            P = new Matrix(nn, nn);
            int[] check = new int[nn]; int ind = 0;
            for (int i = 0; i < nn; ++i)
            {
                do
                {
                    ind = Common.GetRand() % nn;
                } while (check[ind] == 1);
                P.data[ind][i] = 1;
                check[ind]     = 1;
            }

            Gp = NG * P;
            t.Stop();
            return(t.ElapsedMilliseconds / 1000.00);
        }
Esempio n. 4
0
        public double AttackHam(out Matrix N1, out Matrix P1)
        {
            int k = Gp.size[0], n = Gp.size[1];

            N1 = new Matrix(k, k); P1 = new Matrix(n, n);
            Code code = new Code(Gp);
            // compute r
            int r = 0;

            while ((1 << r) - 1 < n)
            {
                ++r;
            }
            Stopwatch t = new Stopwatch(); t.Start();

            // Matrix P
            Ham ham = new Ham(r);
            IEnumerable <int> tmp1, tmp2;
            Matrix            P1_inv = new Matrix(n, n);

            for (int j = 0; j < n; ++j)
            {
                tmp1 = code.H.data.Select(x => x[j]);
                for (int jj = 0; jj < n; ++jj)
                {
                    tmp2 = ham.H.data.Select(x => x[jj]);
                    if (tmp1.SequenceEqual(tmp2))
                    {
                        P1.data[jj][j]     = 1;
                        P1_inv.data[j][jj] = 1;
                        break;
                    }
                }
            }
            // Matrix N
            code.G = code.G * P1_inv;
            N1     = MatrixFuncs.Transp(MatrixFuncs.SolveEq(MatrixFuncs.Transp(ham.G), MatrixFuncs.Transp(code.G)));
            t.Stop();
            return(t.ElapsedMilliseconds / 1000.00);
        }
Esempio n. 5
0
        public Matrix P;  // secret key

        public double KeyGen(CodeType codetype, int[] param)
        {
            Code code = new Code();
            int  k = 0, n = 0, r, m;

            switch (codetype)
            {
            case CodeType.Hamming:
                r    = param[0];
                code = new Ham(r);
                break;

            case CodeType.RM:
                r    = param[0]; m = param[1];
                code = new RM(r, m);
                break;
            }

            k = code.G.size[0]; n = code.G.size[1];
            Stopwatch t = new Stopwatch(); t.Start();

            //Matrix N
            N = MatrixFuncs.GenNoise(k);

            //Matrix P
            P = new Matrix(n, n);
            int[] check = new int[n]; int ind = 0;
            for (int i = 0; i < n; ++i)
            {
                do
                {
                    ind = Common.GetRand() % n;
                } while (check[ind] == 1);
                P.data[ind][i] = 1;
                check[ind]     = 1;
            }
            Gp = N * code.G * P;
            t.Stop();
            return(t.ElapsedMilliseconds / 1000.00);
        }
Esempio n. 6
0
        public List <int>[] GetHamIndices(Code code, Ham ham, int r, int s, int n, int k)
        {
            int               hweight = 1 << (r - 1);
            Matrix            Bchange = MatrixFuncs.GenNoise(n * s - k), Hsub = GetBasisOfWeight(code.H, s * hweight, n - k);
            IEnumerable <int> hcol1, hcol2;
            List <int>        check = Enumerable.Range(0, n * s).ToList();

            List <int>[] res_idx = new List <int> [n];

            for (int j = 0; j < n; ++j)
            {
                res_idx[j] = new List <int>();
                hcol1      = ham.H.data.Select(x => x[j]);
                for (int jj = 0; jj < check.Count; ++jj)
                {
                    hcol2 = Hsub.data.Select(x => x[check[jj]]);
                    if (hcol2.SequenceEqual(hcol1))
                    {
                        res_idx[j].Add(check[jj]);
                        check.RemoveAt(jj);
                        --jj;
                    }
                    if (res_idx[j].Count > s)
                    {
                        break;
                    }
                }
                if (res_idx[j].Count != s)
                {
                    Hsub    = GetBasisOfWeight(code.H, s * hweight, n - k);
                    res_idx = new List <int> [n];
                    check   = Enumerable.Range(0, n * s).ToList();
                    j       = -1;
                }
            }
            return(res_idx);
        }
Esempio n. 7
0
        private void btn_keygen_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbox_cslist.SelectedIndex == -1)
                {
                    MessageBox.Show("Выберите криптосистему в выпадающем списке!", "Ошибка"); return;
                }
                if (txtbox_cparams.Text == "")
                {
                    MessageBox.Show("Введите параметры криптосистемы!", "Ошибка"); return;
                }
                int[]  param = txtbox_cparams.Text.Split(',').Select(x => Int32.Parse(x)).ToArray();
                double time  = 0;
                selectedIndex = cbox_cslist.SelectedIndex;

                switch (cbox_cslist.SelectedIndex)
                {
                case 0:
                {
                    if (param.Length != 1)
                    {
                        MessageBox.Show("Неверное число параметров криптосистемы!", "Ошибка"); return;
                    }
                    if (param[0] < 2)
                    {
                        MessageBox.Show("Некорректные параметры криптосистемы!", "Ошибка"); return;
                    }
                    time             = McE.KeyGen(CodeType.Hamming, new[] { param[0] });
                    ham              = new Ham(param[0]);
                    txtbox_keys.Text = "N:\r\n" + McE.N.ToString() + "\r\nP:\r\n" + McE.P.ToString() + "\r\nGp:\r\n" + McE.Gp.ToString();
                    break;
                }

                case 1:
                {
                    if (param.Length != 2)
                    {
                        MessageBox.Show("Неверное число параметров криптосистемы!", "Ошибка"); return;
                    }
                    if (param[0] < 2 || param[1] < 1)
                    {
                        MessageBox.Show("Некорректные параметры криптосистемы!", "Ошибка"); return;
                    }
                    if (param[1] > 3)
                    {
                        MessageBox.Show("Количество блоков должно быть не больше трёх!", "Ошибка"); return;
                    }
                    time             = Sid.KeyGen(CodeType.Hamming, param);
                    ham              = new Ham(param[0]);
                    txtbox_keys.Text = "";
                    for (int i = 0; i < Sid.N.Count; ++i)
                    {
                        txtbox_keys.Text += "N" + (i + 1) + ":\r\n" + Sid.N[i].ToString() + "\r\n";
                    }
                    txtbox_keys.Text += "P:\r\n" + Sid.P.ToString() + "\r\nGp:\r\n" + Sid.Gp.ToString();
                    break;
                }

                case 2:
                {
                    if (param.Length != 2)
                    {
                        MessageBox.Show("Неверное число параметров криптосистемы!", "Ошибка"); return;
                    }
                    if (param[0] < 0 || param[1] < 1)
                    {
                        MessageBox.Show("Некорректные параметры криптосистемы!", "Ошибка"); return;
                    }
                    time             = McE.KeyGen(CodeType.RM, param);
                    rm               = new RM(param[0], param[1]);
                    txtbox_keys.Text = "N:\r\n" + McE.N.ToString() + "\r\nP:\r\n" + McE.P.ToString() + "\r\nGp:\r\n" + McE.Gp.ToString();
                    break;
                }

                case 3:
                {
                    if (param.Length != 3)
                    {
                        MessageBox.Show("Неверное число параметров криптосистемы!", "Ошибка"); return;
                    }
                    if (param[0] < 0 || param[1] < 1 || param[2] < 1 || param[1] < param[0])
                    {
                        MessageBox.Show("Некорректные параметры криптосистемы!", "Ошибка"); return;
                    }
                    time             = Sid.KeyGen(CodeType.RM, param);
                    rm               = new RM(param[0], param[1]);
                    txtbox_keys.Text = "";
                    for (int i = 0; i < Sid.N.Count; ++i)
                    {
                        txtbox_keys.Text += "N" + (i + 1) + ":\r\n" + Sid.N[i].ToString() + "\r\n";
                    }
                    txtbox_keys.Text += "P:\r\n" + Sid.P.ToString() + "\r\nGp:\r\n" + Sid.Gp.ToString();
                    break;
                }

                case 4:
                {
                    if (param.Length != 3)
                    {
                        MessageBox.Show("Неверное число параметров криптосистемы!", "Ошибка"); return;
                    }
                    if (param[0] < 0 || param[1] < 1 || param[2] < 1 || param[1] < param[0])
                    {
                        MessageBox.Show("Некорректные параметры криптосистемы!", "Ошибка"); return;
                    }
                    time             = Wish.KeyGen(CodeType.RM, param);
                    rm               = new RM(param[0], param[1]);
                    txtbox_keys.Text = "N:\r\n" + Wish.N.ToString() + "\r\nP:\r\n" + Wish.P.ToString() + "\r\nR:\r\n" + Wish.R.ToString() + "\r\nGp:\r\n" + Wish.Gp.ToString();
                    break;
                }
                }
                lbl_time.Text = "Time (sec) = " + time;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка");
            }
        }
Esempio n. 8
0
        public double AttackHam(out List <Matrix> Nlist, out Matrix P1)
        {
            int k = Gp.size[0], nn = Gp.size[1];

            Nlist = new List <Matrix>(); P1 = new Matrix(nn, nn);
            int r = (int)Math.Log((nn + 2) / 2, 2);
            int tmp = nn % ((1 << r) - 1), s;

            while (tmp != 0)
            {
                --r;
                tmp = nn % ((1 << r) - 1);
            }
            s = nn / ((1 << r) - 1); int n = nn / s;
            Matrix Ni = new Matrix(k, k); P1 = new Matrix(nn, nn); Matrix P1_inv = new Matrix(nn, nn);
            Code   code = new Code(Gp);
            Ham    ham  = new Ham(r);

            Stopwatch t = new Stopwatch(); t.Start();

            // Matrix P1
            List <int>[] ham_idx = GetHamIndices(code, ham, r, s, n, k);
            Matrix       Zero = new Matrix(k, n - k);
            BigInteger   iter_count = 0, max_iter_num = 1, ind_count = 0;

            List <int>[] idx_copy = new List <int> [n]; int[][] indices = new int[s][];
            for (int i = 0; i < s; ++i)
            {
                indices[i]   = new int[n];
                max_iter_num = BigInteger.Multiply(max_iter_num, BigInteger.Pow(s - i, n));
            }
            do
            {
                ++iter_count; ++ind_count;
                if (iter_count > max_iter_num)
                {
                    ham_idx = GetHamIndices(code, ham, r, s, n, k);
                    for (int i = 0; i < s; ++i)
                    {
                        indices[i] = new int[n];
                    }
                    iter_count = 1; ind_count = 1;
                }
                idx_copy = ham_idx.Select(x => new List <int>(x)).ToArray();
                P1       = new Matrix(nn, nn); P1_inv = new Matrix(nn, nn);
                for (int i = 0; i < s; i++)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        P1.data[i * n + j][idx_copy[j][indices[i][j]]]     = 1;
                        P1_inv.data[idx_copy[j][indices[i][j]]][i * n + j] = 1;
                        idx_copy[j].RemoveAt(indices[i][j]);
                    }
                }
                Common.NextSet(indices[0], s, n);
                if (ind_count == BigInteger.Pow(s, n))
                {
                    indices[0] = new int[n];
                    Common.NextSet(indices[1], s - 1, n);
                    ind_count = 0;
                }
            } while (checkP(Gp, P1_inv, ham, Zero, s, n) == false);

            // Matrices N
            Matrix NG = Gp * P1_inv;

            for (int i = 0; i < s; ++i)
            {
                Ni = MatrixFuncs.SolveEq(MatrixFuncs.Transp(ham.G), MatrixFuncs.Transp(MatrixFuncs.SubMatrix(NG, i * n, (i + 1) * n)));
                var ttt = MatrixFuncs.Transp(Ni) * ham.G == MatrixFuncs.SubMatrix(NG, i * n, (i + 1) * n);
                Nlist.Add(MatrixFuncs.Transp(Ni));
            }
            NG = new Matrix();
            for (int i = 0; i < Nlist.Count; ++i)
            {
                NG |= Nlist[i] * ham.G;
            }
            t.Stop();
            return(t.ElapsedMilliseconds / 1000.00);
        }