Пример #1
0
 public void AND(BitHolder32 other)
 {
     for (int i = 0; i < 32; i++)
     {
         int a = AndOperations[i][0];
         int b = other.AndOperations[i][0];
         if (a != -1) // -1 indicates an actual value of 0  ( ANDing 0 always results in 0 )
         {            // do nothing in that case because we already have a 0 value in AndOperations[i][0 && 1]
             if (b == -1)  // if the other value we are ANDing against is 0 then set this AndOperations[i][0 && 1] to 0
             {
                 AndOperations[i][0] = -1;
                 AndOperations[i][1] = -1;  // -1 represents an actual value of 0
             }
             else if (a == -2)  // -2 indicates and actual value of 1
             {
                 AndOperations[i] = other.AndOperations[i];
                 throw new Exception("Unexpected Case");   // this case shouldn't ever happen
             }
             else if (b == -2) // a & 1 == a;
             {
                 AndOperations[i][1] = AndOperations[i][1];  // same as setting it to -2 but bitID ordering is perserved this way.
                 throw new Exception("Unexpected Case");   // this case shouldn't ever happen
             }
             else
             {
                 AndOperations[i][1] = b;
             }
         }
     }
 }
Пример #2
0
 private BitHolder32(BitHolder32 other)
 {
     AndOperations = new int[32][];
     for (int i = 0; i < 32; i++)
     {
         AndOperations[i] = new int[2];
         other.AndOperations[i].CopyTo(AndOperations[i], 0);
     }
 }
        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();
        }
        /// <summary>
        /// It doesn't matter how many XOR_0's you have because y ^ 0 = y;
        /// We need to track XOR_1's because y ^ 1 = !y  && y ^ 1 ^ 1 = y;
        /// </summary>
        /// <param name="x"></param>
        public void XOR(BitHolder32 x)
        {
            for (int i = 0; i < 32; i++)
            {
                int ele0 = x.AndOperations[i][0];  // we need to check ele0 before we sort the data
                int ele1 = x.AndOperations[i][1];  // we need to verify consistant data
                if (ele0 == -2)
                {
                    if (ele1 != -2)  // I've programmed in some sort of logic error if this is the case.
                    {
                        throw new Exception("Unexpected Data");
                    }
                    xor1state[i] = !xor1state[i];
                    throw new Exception("Unexpected Data");
                }
                else if (ele0 == -1)
                {
                    // do nothing in this case because A ^ 0 == A
                }
                else if (ele1 == -1) // ele0 should have been set to 0 (-1) also because A & 0 == 0
                {                    // this is definately a logic error on my part.
                    throw new Exception("Unexpected Data");
                }
                else if (ele1 == -2)  // this case is fine because ele0 & 1 == ele0
                {
                    var t = new ANDCombination(ele0, ele1);
                    if (!xorOperations[i].Remove(t))    // if it existed in the xor chain list then remove it
                    {                                   // because (A ^ A ^ B) => (0 ^ B) => (B)
                        xorOperations[i].Add(t, null);  // if it wasn't in the xor chain list then we add it
                    }
                    throw new Exception("Unexpected Logic Path"); // I don't believe I ever set my data to look like this.
                    // it should come in the form A & A, not A & 1.
                }
                else // both ele0 and ele1 must not be a set value of 0 or 1 (-1 or -2) now and refer to bitIDs
                {
                    var t = new ANDCombination(ele0, ele1);

                    // if it existed in the xor chain list then remove it
                    // because (A ^ A ^ B == 0 ^ B) => (B ^ 0 == B)
                    if (!xorOperations[i].Remove(t))
                    {   // if it wasn't in the xor chain list then we add it
                        xorOperations[i].Add(t, null);
                    }
                }
            }
        }
Пример #5
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();
        }
Пример #6
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();
        }