private void btn_attack_Click(object sender, EventArgs e) { if (txtbox_keys.Text == "") { MessageBox.Show("Сначала сгенерируйте ключи!", "Ошибка"); return; } double time = -1; bool check = false; switch (selectedIndex) { case 0: { time = McE.AttackHam(out N1, out P1); Matrix Gp = N1 * ham.G * P1; check = (Gp == McE.Gp); txtbox_unlock.Text = "N':\r\n" + N1.ToString() + "\r\nP':\r\n" + P1.ToString() + "\r\nN' * G * P':\r\n" + Gp.ToString(); break; } case 1: { time = Sid.AttackHam(out Nlist, out P1); Matrix NG = new Matrix(), Gp; txtbox_unlock.Text = ""; for (int i = 0; i < Nlist.Count; ++i) { txtbox_unlock.Text += "N" + (i + 1) + "':\r\n" + Nlist[i].ToString() + "\r\n"; NG |= Nlist[i] * ham.G; } Gp = NG * P1; check = (Gp == Sid.Gp); txtbox_unlock.Text += "\r\nP':\r\n" + P1.ToString() + "\r\n\r\nN' * G * P':\r\n" + Gp.ToString(); break; } case 2: { time = McE.AttackRM(out N1, out P1); Matrix Gp = N1 * rm.G * P1; check = (Gp == McE.Gp); txtbox_unlock.Text = "N':\r\n" + N1.ToString() + "\r\nP':\r\n" + P1.ToString() + "\r\nN' * G * P':\r\n" + Gp.ToString(); break; } case 4: { time = Wish.AttackRM(out N1, out P1, out I); Matrix Gp = N1 * rm.G * P1, Gp_orig = new Matrix(Wish.Gp); int shift = 0; for (int i = 0; i < I.Count; ++i) { Gp_orig ^= I[i] - shift; ++shift; } check = (Gp == Gp_orig); txtbox_unlock.Text = "N':\r\n" + N1.ToString() + "\r\nP':\r\n" + P1.ToString() + "\r\nIndices: " + String.Join(", ", I); break; } } lbl_time.Text = "Time (sec) = " + time + "\r\n\r\n" + "Check = " + check; }
public double AttackRM(out Matrix N1, out Matrix P1, out List <int> I) { int k = Gp.size[0], nn = Gp.size[1]; Code code = new Code(Gp); int r = -1, m = 1, count = 0; while ((1 << (m + 1)) < nn) { ++m; } while (count < k) { ++r; count += Common.C(m, r); } Code code_attack; //2 * r >= m => consider the dual code if (2 * r < m) { code_attack = new Code(code.G); } else { code_attack = new Code(code.H); r = m - r - 1; } int n = (1 << m), l = nn - n; N1 = new Matrix(k, k); P1 = new Matrix(n, n); Stopwatch t = new Stopwatch(); t.Start(); I = new List <int>(); Code Li; int dim_square = Code.Square(code_attack).size[0], idx; List <int> check = Enumerable.Range(0, nn).ToList(); while (I.Count != l) { idx = Common.GetRand() % check.Count; Li = new Code(new Matrix(code_attack.G) ^ check[idx], false); if (Code.Square(Li).size[0] == dim_square - 1) { I.Add(check[idx]); } check.RemoveAt(idx); } //I = new List<int>(); Code Li; //int dim_square = Code.Square(code).size[0]; //for (int i = 0; i < nn; ++i) //{ // Li = new Code(new Matrix(code.G) ^ i, false); // if (Code.Square(Li).size[0] == dim_square - 1) // { // I.Add(i); // } // if (I.Count == l) break; //} t.Stop(); Matrix Tmp = new Matrix(code.G); int shift = 0; I.Sort(); for (int i = 0; i < l; ++i) { Tmp ^= I[i] - shift; ++shift; } McElice McE = new McElice(); McE.Gp = Tmp; McE.AttackRM(out N1, out P1); return(t.ElapsedMilliseconds / 1000.00); }