A5.A5Engine.RegisterShift C# (CSharp) Method

RegisterShift() public method

public RegisterShift ( int regTS ) : void
regTS int
return void
        public void RegisterShift(int[] regTS)
        {
            int[] shiftedElements = new int[regTS.Length];
            int[] regTSFBV = GetFeedbackValues(regTS);

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

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

            for (int i = 0; i < regTS.Length; i++)
            {
                int[] regShifting = registers[regTS[i]]; //Alias the register to shift

                shiftedElements[i] = registers[regTS[i]][0]; //Copy position zero value in registers to shift

                //Creates new register with appropriate max reg length
                int[] nRegister = new int[regShifting.Length]; //Could also use mRegLens[regTS[i]].Length

                //Fill values to length -1
                for (int x = 0; x < (regShifting.Length - 1); x++)
                    nRegister[x] = regShifting[x + 1]; //+1 Grabbing everything after position zero

                //Now put feedback values on the end (former RegisterPush method)
                nRegister[nRegister.Length - 1] = regTSFBV[i];

                registers[regTS[i]] = nRegister; //assign to register (update)
            }
        }
    }

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::RegisterShift