private void btnStructure_Click(object sender, EventArgs e)
        {
            int DATASIZE = (int)numBits.Value;
            //string rawData = "000073af00000000";
            string rawData = "000000b5";
            //string rawData = "73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af73af";
            uint[] aa = new uint[(DATASIZE / 16 > 0) ? DATASIZE / 16 : 1];
            uint[] bb = new uint[(DATASIZE / 16 > 0) ? DATASIZE / 16 : 1];
            BitHolder32[] a = new BitHolder32[(DATASIZE / 16 > 0) ? DATASIZE / 16 : 1];
            XORCollector32Bit[] b = new XORCollector32Bit[(DATASIZE / 16 > 0) ? DATASIZE / 16 : 1];
            for (int i = 0; i < ((DATASIZE / 16 > 0) ? DATASIZE / 16 : 1); i++)
            {
                a[i] = new BitHolder32(i * 32);
                b[i] = new XORCollector32Bit();
                aa[i] = uint.Parse(rawData.Substring(i * 8, 8), System.Globalization.NumberStyles.HexNumber);
            }

            SortedDictionary<ANDCombination, object>[] xorEq = new SortedDictionary<ANDCombination, object>[DATASIZE * 2];
            for (int i = 0; i < xorEq.Length; i++)
            {
                xorEq[i] = new SortedDictionary<ANDCombination, object>();
            }

            for (int i = 0; i < DATASIZE; i++)
            {
                for (int j = 0; j < DATASIZE; j++)
                {
                    bb[(i + j) / 32] ^= (((aa[i / 32] >> (i % 32)) & (aa[j / 32 + DATASIZE / 32] >> (j + DATASIZE % 32)) & 1) << ((i + j) % 32));
                    ANDCombination andc = new ANDCombination(i + 1, j + DATASIZE + 1);
                    xorEq[(i + j)][andc] = null;
                    //if (!TestPartialEncryption(aa, bb, xorEq))
                    //    throw new Exception("You messed up!");
                }
            }

            StringBuilder sb = new StringBuilder();
            AppendStructure(sb, xorEq);

            sb.AppendLine();
            sb.AppendLine();
            bb.AppendHex(sb);
            sb.AppendLine();

            tbOut.Text = sb.ToString();
        }
Exemple #2
0
        private void button5_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string rawData = textBox1.Text.Replace(" ", "");
            uint[] aa = new uint[DATASIZE / 16];
            uint[] bb = new uint[DATASIZE / 16];
            BitHolder32[] a = new BitHolder32[DATASIZE / 16];
            XORCollector32Bit[] b = new XORCollector32Bit[DATASIZE / 16];
            for (int i = 0; i < DATASIZE / 16; i++)
            {
                a[i] = new BitHolder32(i * 32);
                b[i] = new XORCollector32Bit();
                aa[i] = uint.Parse(rawData.Substring(i * 8, 8), System.Globalization.NumberStyles.HexNumber);
            }

            for (int i = 0; i < DATASIZE; i++)
            {
                for (int j = 0; j < DATASIZE; j++)
                {
                    bb[(i + j) / 32] ^= (((aa[i / 32] >> (i % 32)) & (aa[j / 32 + DATASIZE / 32] >> (j % 32)) & 1) << ((i + j) % 32));
                    BitHolder32 r1 = a[i / 32].DeepCopy();
                    r1.RightShift(i % 32);
                    BitHolder32 r2 = a[j / 32 + DATASIZE / 32].DeepCopy();
                    r2.RightShift(j % 32);
                    r1.ANDOne();
                    r1.AND(r2);
                    r1.LeftShift((i + j) % 32);
                    b[(i + j) / 32].XOR(r1);
                    if (bb[(i + j) / 32] != b[(i + j) / 32].TestPartialEncryption(aa))
                        throw new Exception("You messed up!");
                }
            }

            var rdcer = new LogicEquationReducer(b);
            var solution = rdcer.TryThree(aa, sb);

            sb.AppendLine();
            sb.AppendLine();

            for (int i = 0; i < solution.Length; i++)
            {
                sb.Append(solution[i].ToString("X8"));
                sb.Append(" ");
            }
            tbOut.Text = sb.ToString();
        }
Exemple #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            string rawData = textBox1.Text.Replace(" ", "");
            uint[] aa = new uint[DATASIZE / 16];
            uint[] bb = new uint[DATASIZE / 16];
            BitHolder32[] a = new BitHolder32[DATASIZE / 16];
            XORCollector32Bit[] b = new XORCollector32Bit[DATASIZE / 16];
            for (int i = 0; i < DATASIZE / 16; i++)
            {
                a[i] = new BitHolder32(i * 32);
                b[i] = new XORCollector32Bit();
                aa[i] = uint.Parse(rawData.Substring(i * 8, 8), System.Globalization.NumberStyles.HexNumber);
            }

            for (int i = 0; i < DATASIZE; i++)
            {
                for (int j = 0; j < DATASIZE; j++)
                {
                    bb[(i + j) / 32] ^= (((aa[i / 32] >> (i % 32)) & (aa[j / 32 + DATASIZE / 32] >> (j % 32)) & 1) << ((i + j) % 32));
                    BitHolder32 r1 = a[i / 32].DeepCopy();
                    r1.RightShift(i % 32);
                    BitHolder32 r2 = a[j / 32 + DATASIZE / 32].DeepCopy();
                    r2.RightShift(j % 32);
                    r1.ANDOne();
                    r1.AND(r2);
                    r1.LeftShift((i + j) % 32);
                    b[(i + j) / 32].XOR(r1);
                    if (bb[(i + j) / 32] != b[(i + j) / 32].TestPartialEncryption(aa))
                        throw new Exception("You messed up!");
                }
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < DATASIZE / 16; i++)
            {
                sb.Append("32 bits * (");
                sb.Append(i);
                sb.Append(") ");
                sb.Append('-', 10);
                sb.AppendLine();
                sb.Append(b[i].OutputResults());
                sb.AppendLine();
            }
            //tbOut.Text = sb.ToString();

            //bb = new uint[DATASIZE / 16];
            //for (int i = 0; i < DATASIZE; i++)
            //{
            //    for (int j = 0; j < DATASIZE; j++)
            //    {
            //        bb[(i + j) / 32] |= 1u << ((i + j) % 32);
            //    }
            //}

            //sb.AppendLine();
            //for (int i = 0; i < bb.Length; i++)
            //{
            //    sb.Append(bb[i].ToString("X8"));
            //    sb.Append(' ');
            //}
            //sb.Length -= 1;
            sb.AppendLine();

            tbOut.Text = sb.ToString();

            //tbOut.Text = string.Empty;

            BitLogicRecursiveTester blt = new BitLogicRecursiveTester(b, aa);
            var r = blt.EncriptUsingXORBitTracking();
            foreach (var rr in r)
            {
                tbOut.Text += rr.ToString("X8") + " ";
            }
            var rslt = blt.TestCombinations();

            int[] valueChanges = new int[64];
            for (int i = 0; i < valueChanges.Length; i++)
            {
                valueChanges[i] = -1;
            }

            for (int i = 0; i < rslt.Count; i++)
            {
                uint[] finalData = new uint[DATASIZE / 16];
                for (int j = 0; j < rslt.ElementAt(i).Length; j++)
                {
                    if (rslt.ElementAt(i)[j].Value)
                    {
                        //finalData[rslt.ElementAt(i)[j].Key / 32] |= 1u << (rslt.ElementAt(i)[j].Key % 32);
                        valueChanges[rslt.ElementAt(i)[j].Key]++;
                    }
                }
                //sb.Append("Successful Solution ");
                //sb.Append(i);
                //sb.Append(": ");
                //foreach (uint rr in finalData)
                //{
                //    sb.Append(rr.ToString("X8"));
                //    sb.Append(" ");
                //}
                //sb.AppendLine();
            }
            sb.AppendLine();
            sb.Append("No Value Changes For: ");
            for (int i = 0; i < valueChanges.Length; i++)
            {
                if (valueChanges[i] == 0)
                {
                    sb.Append(i);
                    sb.Append(", ");
                }
            }

            tbOut.Text = sb.ToString();
        }
        public BitLogicRecursiveTester(XORCollector32Bit[] logicData, uint[] encryptedData)
        {
            if (logicData.Length != encryptedData.Length)
                throw new ArgumentException("Unexpected Logic Error BitLogicTest constructor; input arrays not the same length.");

            bitXORCollector = logicData;
            bitTestOrder = new List<SortedList<int, int>>();
            bitFillOrder = new List<SortedList<int, int>>();

            encryptedBitStates = new bool[encryptedData.Length * 32];
            for (int i = 0; i < encryptedData.Length; i++)
            {
                for (int j = 0; j < 32; j++)
                {
                    encryptedBitStates[i * 32 + j] = IsBitSet(encryptedData[i], j);
                }
            }

            // uniqueIDs<AND/XOR bitID, Logic Equation bitID>[Logic Equation bitID]
            SortedDictionary<int, int>[] uniqueIDs = new SortedDictionary<int, int>[encryptedData.Length * 32];
            for (int i = 0; i < encryptedData.Length; i++)
            {
                for (int j = 0; j < 32; j++)
                {
                    int bitPos = i * 32 + j;

                    uniqueIDs[bitPos] = new SortedDictionary<int, int>();
                    foreach (KeyValuePair<ANDCombination, object> pair in logicData[i].xorOperations[j])
                    {
                        uniqueIDs[bitPos][pair.Key.FirstVarID] = bitPos;
                        uniqueIDs[bitPos][pair.Key.SecondVarID] = bitPos;
                    }
                }
            }

            int testRank = 0;
            SortedDictionary<int, int> definedBits = null;
            SortedDictionary<int, int> nextDefinedBits = new SortedDictionary<int, int>();
            while (nextDefinedBits != null)
            {
                bitTestOrder.Add(new SortedList<int, int>());
                bitFillOrder.Add(new SortedList<int, int>());
                int lowCount = int.MaxValue;
                definedBits = nextDefinedBits;
                nextDefinedBits = null;
                for (int j = 0; j < uniqueIDs.Length; j++)
                {
                    if (uniqueIDs[j].Count > 0)
                    {
                        if (definedBits != uniqueIDs[j])
                        {
                            foreach (KeyValuePair<int, int> b in definedBits)
                            {
                                uniqueIDs[j].Remove(b.Key);
                            }
                        }

                        if (uniqueIDs[j].Count == 0 || definedBits == uniqueIDs[j])
                        {
                            bitTestOrder[testRank].Add(j, j);
                        }
                        else if (uniqueIDs[j].Count < lowCount)
                        {
                            lowCount = uniqueIDs[j].Count;
                            nextDefinedBits = uniqueIDs[j];
                        }
                    }
                }
                foreach (KeyValuePair<int, int> b in definedBits)
                {
                    bitFillOrder[testRank].Add(b.Key, b.Key);
                }
                definedBits.Clear();
                testRank++;
            }

            bitTestOrder.RemoveAt(0);
            bitFillOrder.RemoveAt(0);
        }