コード例 #1
0
ファイル: Utility.cs プロジェクト: wertyBSd/HSMThalesEmu
        public static bool IsParityOK(string hexString, ParityCheck parity)
        {
            if (parity == ParityCheck.NoParity)
            {
                return(true);
            }
            hexString = RemoveKeyType(hexString);
            int i = 0;

            while (i < hexString.Length)
            {
                string b = toBinary(hexString.Substring(i, 2));
                i += 2;
                int l = 0;
                for (int j = 0; j < b.Length; j++)
                {
                    if (b.Substring(j, 1) == "1")
                    {
                        l += 1;
                    }
                }
                if (((l % 2 == 0) && (parity == ParityCheck.OddParity)) || ((l % 2 == 1) && (parity == ParityCheck.EvenParity)))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #2
0
ファイル: Utility.cs プロジェクト: wertyBSd/HSMThalesEmu
        public static string MakeParity(string hexString, ParityCheck parity)
        {
            if (parity == ParityCheck.NoParity)
            {
                return(hexString);
            }

            string head = "";

            if (hexString != RemoveKeyType(hexString))
            {
                head      = hexString.Substring(0, 1);
                hexString = RemoveKeyType(hexString);
            }

            int    i = 0;
            string r = "";

            while (i < hexString.Length)
            {
                string b = toBinary(hexString.Substring(i, 2));
                i += 2;
                int l = b.Replace("0", "").Length;

                if (((l % 2 == 0) && (parity == ParityCheck.OddParity)) || ((l % 2 == 1) && (parity == ParityCheck.EvenParity)))
                {
                    if (b.Substring(7, 1) == "1")
                    {
                        r = r + b.Substring(0, 7) + "0";
                    }
                    else
                    {
                        r = r + b.Substring(0, 7) + "1";
                    }
                }
                else
                {
                    r = r + b;
                }
            }

            return(head + fromBinary(r));
        }
コード例 #3
0
ファイル: Utility.cs プロジェクト: wertyBSd/HSMThalesEmu
        public static string RandomKey(bool EnforceParity, ParityCheck Parity)
        {
            StringBuilder sb = new StringBuilder();
            string        s;

            for (int i = 1; i < 17; i++)
            {
                sb.AppendFormat("{0:X1}", rndMachine.Next(0, 16));
            }
            s  = sb.ToString();
            sb = null;
            if (EnforceParity)
            {
                if (Parity != ParityCheck.NoParity)
                {
                    s = MakeParity(s, Parity);
                }
            }
            return(s);
        }