コード例 #1
0
        private static bool CRC_OK(string input_frame, bool DEBUG)
        {
            string lblCRC = input_frame.Substring(input_frame.Length - 4, 4);

            //Si le CRC a été forcé à 0 (trame issu de flux WAN)
            if (lblCRC == "0000")
            {
                if (DEBUG)
                {
                    System.Console.WriteLine(Environment.NewLine + "CRC = 0000 -> Continue (the frame presumably comes from a WAN message with a CRC forced to 0000");
                }
                return(true);
            }
            //sinon on calcule le CRC et vérifie qu'il est OK avec celui de la trame
            else
            {
                //calcul du CRC
                string CRCinput       = input_frame.Substring(0, input_frame.Length - 4);
                byte[] CRCinputbytes  = Hex2Bytes(CRCinput);
                short  CRCcomputed    = CLibCRC.CalculateCRC16EN13757(CRCinputbytes);
                string lblCRCcomputed = CRCcomputed.ToString("X2");

                //vérification du CRC calculé et du CRC de la trame
                if (lblCRCcomputed.ToString().ToUpper().PadLeft(4, '0') == lblCRC.ToString().ToUpper())
                {
                    if (DEBUG)
                    {
                        System.Console.WriteLine(Environment.NewLine + "CRC OK" + " CRC computed = " + lblCRCcomputed + " CRC in frame = " + lblCRC);
                    }
                    return(true);
                }
                else
                {
                    System.Console.WriteLine("Error CRC: CRC computed = " + lblCRCcomputed + " CRC in frame = " + lblCRC);
                    return(false);
                }
            }
        }
コード例 #2
0
        private static void LANcipherV1(string[] args, bool DEBUG)
        {
            string lblCField  = args[8];
            string lblMField  = args[5];
            string lblAField  = args[6];
            string lblCIField = args[12]; //"B4";  imposée par la LAN


            string lblL6Vers   = args[2];
            string lblL6Wts    = "0"; //champs reserved du L6Ctrl fixé à 0
            string lblL6KeySel = Convert.ToString(Convert.ToInt32(args[9], 10), 2).PadLeft(4, '0');
            string lblL6Ctrl   = Convert.ToInt32(lblL6Vers + lblL6Wts + lblL6KeySel, 2).ToString("X2");

            string lblL6OprID = args[10];
            string lblL6Cpt   = args[7];
            string lblL6App   = args[11];

            string l7 = args[3];

            string lblL6HashKenc = "00000000"; //recalcule après
            string lblL6Tstamp   = args[13];
            //string lblL6Tstamp = "0000"; //recalculé par le K
            string lblL6HashKmac = "0000"; //recalculé par le K
            string CRC           = "0000"; //recalculé par le K

            byte[] kencbytes = Hex2Bytes(args[4].Substring(0, 32));
            byte[] kmacbytes = Hex2Bytes(args[14].Substring(0, 32));

            //System.Console.WriteLine("l7: " + l7 + Environment.NewLine + "lblMField: " + lblMField + Environment.NewLine + "lblAField: " + lblAField + Environment.NewLine + "lblL6Cpt: " + lblL6Cpt + Environment.NewLine + "lblCField: " + lblCField + Environment.NewLine + "l7: " + l7 + Environment.NewLine);

            //IV
            string txtIV = (lblMField.ToString() + lblAField.ToString() + lblL6Cpt.ToString() + lblCField.ToString()).PadRight(32, '0');

            if (DEBUG)
            {
                System.Console.WriteLine("computed IV: " + txtIV);
            }

            //chiffrement L7
            byte[] l7cipheredbytes = CAES128.AES_CTR(Hex2Bytes(l7), kencbytes, Hex2Bytes(txtIV));
            string l7ciphered      = Bytes2Hex(l7cipheredbytes, l7cipheredbytes.Length);

            if (DEBUG)
            {
                System.Console.WriteLine("LAN L7 ciphered: " + l7ciphered);
            }


            //L-field
            Int32  lfield_calc = 1 + 2 + 6 + 1 + 1 + 1 + 2 + 1 + l7.Length / 2 + 4 + lblL6Tstamp.Length / 2 + 2 + 2; // longeur de la trame du C-field au CRC inclu
            string lblLField   = lfield_calc.ToString("X2");

            //L6HashKenc
            string block0Kenc = lblMField + lblAField + lblL6Cpt + "000000000000";

            lblL6HashKenc = Bytes2Hex(CAES128.AES_CMAC(Hex2Bytes(block0Kenc + l7ciphered), kencbytes), 4);
            if (DEBUG)
            {
                System.Console.WriteLine("lblL6HashKenc " + lblL6HashKenc);
            }

            //L6HashKmac
            string HashKmacInput = lblL6Ctrl + lblL6OprID + lblL6Cpt + lblL6App + l7ciphered + lblL6HashKenc + lblL6Tstamp;
            string block0Kmac    = lblMField + lblAField + "0000000000000000";
            string Cmacinput     = block0Kmac + HashKmacInput;

            if (DEBUG)
            {
                System.Console.WriteLine("full Cmacinput  = " + Cmacinput);
            }
            byte[] Cmacinputbytes = Hex2Bytes(Cmacinput);
            lblL6HashKmac = Bytes2Hex(CAES128.AES_CMAC(Cmacinputbytes, kmacbytes), 2);
            if (DEBUG)
            {
                System.Console.WriteLine("lblL6HashKmac  = " + lblL6HashKmac);
            }

            //CRC
            string CRCinput = lblLField + lblCField + lblMField + lblAField + lblCIField + lblL6Ctrl + lblL6OprID + lblL6Cpt + lblL6App + l7ciphered + lblL6HashKenc + lblL6Tstamp + lblL6HashKmac;

            byte[] CRCinputbytes = Hex2Bytes(CRCinput);
            short  CRCcomputed   = CLibCRC.CalculateCRC16EN13757(CRCinputbytes);

            CRC = CRCcomputed.ToString("X2");
            if (DEBUG)
            {
                System.Console.WriteLine("CRC computed  = " + CRC);
            }

            string L2 = lblLField + lblCField + lblMField + lblAField + lblCIField + lblL6Ctrl + lblL6OprID + lblL6Cpt + lblL6App + l7ciphered + lblL6HashKenc + lblL6Tstamp + lblL6HashKmac + CRC;

            System.Console.WriteLine("L2 " + L2);
        }