A5.A5Engine.RegistersToShift C# (CSharp) Method

RegistersToShift() public method

public RegistersToShift ( ) : int[]
return int[]
        public int[] RegistersToShift()
        {
            int[] indexValues = GetIndexValues();
            int[] tally = FindFrequency(indexValues);

            int highest = 0;
            int movVal = 0;

            if (dbMode)
            {
                Console.WriteLine("[indexValues]:");
                foreach (int v in indexValues)
                    Console.Write(v.ToString() + " ");
                Console.WriteLine("\n[/indexValues]:");

                Console.WriteLine("[tally]:");
                foreach (int v in tally)
                    Console.Write(v.ToString() + " ");
                Console.WriteLine("\n[/tally]:");
            }

            foreach (int count in tally)
            {
                if (count > highest)
                    highest = count;
            }

            for (int i = 0; i < tally.Length; i++)
            {
                if (tally[i] == highest)
                    movVal = i;
            }

            ArrayList regTS = new ArrayList();

            for (int i = 0; i < indexValues.Length; i++)
            {
                if (indexValues[i] == movVal)
                    regTS.Add(i);
            }

            return (int[])regTS.ToArray(typeof(int));
        }

Usage Example

Esempio n. 1
0
        static void Main(string[] args)
        {
            //Create registers
            A5Engine A5eng = new A5Engine();

            //To expand the algorithm...
            //Edit below properties
            A5eng.numRegisters = 3; //Number of registers to use
            A5eng.maxRegLens = new int[] { 19, 22, 23 }; //Test max reg lengths: { 5,5,5 };
            A5eng.regIndexes = new int[] { 8, 10, 10 }; //Test clocking bits: { 0,0,0 };

            //Test polynomials: { "x^1+x^2+x^3+1", "x^0+x^2+x^3+1", "x^3+x^4+1" };
            A5eng.polynomialsArray = new string[] { "x^8+x^17+x^16+x^13+1",
                                                "x^21+x^20+1",
                                                "x^22+x^21+x^20+x^7+1" };
            A5eng.sourceArray = new int[] { 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
                                            1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
                                            1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
                                            1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
                                            1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1,
                                            0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 };

            /* DO NOT EDIT BELOW THIS POINT */
            int numRegPushes = 100;
            bool testPassed = true; //For cipher check

            if (args.Length > 0)
            {
                if (args[0] == "-d")
                {
                    A5eng.dbMode = true;
                    if (args.Length > 1)
                    {
                        try
                        {
                            numRegPushes = int.Parse(args[1]);
                        }
                        catch (Exception ex)
                        {
                            testPassed = false;

                            Console.WriteLine("Error: Numeric values only!");
                            Console.WriteLine("Exception: " + ex.Message);
                        }
                    }
                }
            }

            if (A5eng.sourceArray.Length < A5eng.GetMaxRegLensTotal())
            {
                testPassed = false;
                Console.WriteLine("[-] Not enough source data!");
            }

            if (A5eng.polynomialsArray.Length != A5eng.numRegisters)
            {
                testPassed = false;
                Console.WriteLine("[-] Not enough polynomials");
            }

            if (testPassed)
            {
                A5eng.Registers = A5eng.CreateRegisters();

                if (A5eng.dbMode)
                {
                    Console.WriteLine("Output (debugging mode): ");
                    for (int ia = 0; ia < numRegPushes; ia++)
                    {
                        Console.WriteLine("[register]");
                        int c = 0;
                        foreach (int[] p in A5eng.Registers)
                        {
                            Console.Write("register: {0} ", c);
                            foreach (int poly in p)
                            {
                                Console.Write(poly.ToString());
                            }
                            Console.WriteLine();

                            c++;
                        }
                        Console.WriteLine("[/register]");

                        int[] regTS = A5eng.RegistersToShift();
                        A5eng.RegisterShift(regTS);

                        System.Threading.Thread.Sleep(20); //Slow the output
                    }

                    Console.WriteLine("\n{0} loops of A5/1 have been completed.", numRegPushes.ToString());
                }
                else
                {
                    A5eng.Intro();

                    Console.WriteLine("Output: ");
                    while (true)
                    {
                        Console.Write(A5eng.GetOutValue().ToString());

                        int[] regTS = A5eng.RegistersToShift();
                        A5eng.RegisterShift(regTS);

                        System.Threading.Thread.Sleep(20); //Slow the output
                    }
                }
            }
        }
All Usage Examples Of A5.A5Engine::RegistersToShift