internal static byte GenerateRandomNumber(this FreeformSecurityTable secTable)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "GenerateRandomNumber"))
            {
                byte result = 0;

                try
                {
                    int count = 10;
                    while (count-- > 0)
                    {
                        byte[] lohi = secTable.GenerateRandomNumberLowHigh();
                        secTable.PartialKeyIndex = (byte)(secTable.PartialKeyIndex > 0 ? secTable.PartialKeyIndex - 1 : 8);
                        result = (byte)(lohi[0] ^ lohi[1]);// ^ secTable.OtherPartialKey[secTable.PartialKeyIndex]);
                        if (result > 0)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(0);
                }

                return(result);
            }
        }
        internal static bool StoreOtherPartialKeyHost(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key, byte[] partialKey)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "StoreOtherPartialKey"))
            {
                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(false);
                    }

                    FreeformSecurityTable secTable = secTables[key];
                    int length = FreeformSecurityTable.KEY_SIZE_2;
                    for (int i = 0; i < length; ++i)
                    {
                        secTable.OtherPartialKey[i] = (byte)(partialKey[i] ^ ((byte)(0x50 + i)));
                    }

                    // method
                    secTable.Method = (((partialKey[length - 1] ^ 0xA5) >> 2) & 0x0F);

                    // indicates that other key is generated and common key is unusable
                    secTable.Flags = (secTable.Flags | FreeformSecurityFlags.OtherPartialKey) & ~FreeformSecurityFlags.CommonKey;
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(false);
                }

                return(true);
            }
        }
        internal static bool StoreOtherPartialKeyGmu(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key, byte[] partialKey)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "StoreOtherPartialKey"))
            {
                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(false);
                    }

                    FreeformSecurityTable secTable = secTables[key];
                    int length = FreeformSecurityTable.KEY_SIZE;
                    for (int i = 0, j = length - 1; i < length; i++, j = ((j + 3) & 0x7))
                    {
                        secTable.OtherPartialKey[j] = partialKey[i];
                    }

                    // indicates that other key is generated and common key is unusable
                    secTable.Flags = (secTable.Flags | FreeformSecurityFlags.OtherPartialKey) & ~FreeformSecurityFlags.CommonKey;
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(false);
                }

                return(true);
            }
        }
        internal static byte[] CreatePartialKeyHost(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreatePartialKey"))
            {
                byte[] result = null;

                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(result);
                    }

                    FreeformSecurityTable secTable = secTables[key];
                    byte[] keys = alphan;
                    int    j    = 0;

                    if (secTable.Method > (FK_1ST - 1))
                    {
                        secTable.Method = ((0xC3 * 100) % (FK_LST - FK_1ST + 1)) + FK_1ST;
                    }

                    if (secTable.Method == (int)FreeformSecurityMethodTypes.Alpha1)
                    {
                        keys = alpha1;
                    }
                    else if (secTable.Method == (int)FreeformSecurityMethodTypes.Alpha2)
                    {
                        keys = alpha2;
                    }
                    else
                    {
                        j = secTable.Method;
                    }

                    int i = 0;
                    for (int k = 3; i < 8; ++i, k = (k + 3) & 7)
                    {
                        secTable.LocalPartialKey[k] = exp_mod(keys[i + j], secTable.LocalRandomNumber[i]);
                    }
                    secTable.LocalPartialKey[8] = (byte)((0xC3 | (secTable.Method << 2)) ^ 0xA5);

                    // indicates that local partial key is generated and common key is unusable
                    secTable.Flags = (secTable.Flags | FreeformSecurityFlags.LocalPartialKey) & ~FreeformSecurityFlags.CommonKey;
                    result         = secTable.LocalPartialKey;
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(null);
                }

                return(result);
            }
        }
        internal static bool DecryptHost(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key, ref byte[] data)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "Decrypt"))
            {
                if (data == null || data.Length == 0)
                {
                    return(false);
                }

                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(false);
                    }

                    FreeformSecurityTable secTable = secTables[key];
                    if ((secTable.Flags & FreeformSecurityFlags.CommonKey) != FreeformSecurityFlags.CommonKey)
                    {
                        return(false);
                    }

                    int j = 0;
                    if (secTable.Method == (int)FreeformSecurityMethodTypes.Alpha2)
                    {
                        j = 5;
                    }

                    for (int i = 0; i < data.Length; ++i)
                    {
                        data[i] ^= (byte)(secTable.CommonKey[(i + j) & 0x7] ^ fudge1[i & 0xF]);
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(false);
                }

                return(true);
            }
        }
        internal static bool CreateCommonKey(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreateCommonKey"))
            {
                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(false);
                    }

                    FreeformSecurityTable secTable = secTables[key];
                    if (secTable.Flags != (FreeformSecurityFlags.LocalPartialKey |
                                           FreeformSecurityFlags.OtherPartialKey))
                    {
                        return(false);
                    }

                    byte[] localNum = secTable.LocalRandomNumber;
                    for (int i = 0; i < FreeformSecurityTable.KEY_SIZE; i++)
                    {
                        secTable.CommonKey[i] = exp_mod(secTable.OtherPartialKey[i], localNum[i]);
                    }

                    // all the keys are now available
                    secTable.Flags |= (FreeformSecurityFlags.LocalPartialKey |
                                       FreeformSecurityFlags.OtherPartialKey |
                                       FreeformSecurityFlags.CommonKey);
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(false);
                }

                return(true);
            }
        }
        internal static bool ClearKeyData(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "Decrypt"))
            {
                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(false);
                    }
                    FreeformSecurityTable secTable = secTables[key];

                    // clear the flags
                    secTable.Flags = 0;

                    // clear common key
                    for (int i = 0; i < secTable.CommonKey.Length; i++)
                    {
                        secTable.CommonKey[i] = 0;
                    }

                    // clear local key
                    for (int i = 0; i < secTable.LocalRandomNumber.Length; i++)
                    {
                        secTable.LocalRandomNumber[i] = 0;
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(false);
                }

                return(true);
            }
        }
        internal static byte[] CreatePartialKeyGmu(this FreeformSecurityTableCollection secTables, SECURITY_KEY_INDEX key)
        {
            using (ILogMethod method = Log.LogMethod(DYN_MODULE_NAME, "CreatePartialKey"))
            {
                byte[] result = null;

                try
                {
                    if (!secTables.ContainsKey(key))
                    {
                        return(result);
                    }

                    FreeformSecurityTable secTable = secTables[key];
                    byte[] keys = alphan;
                    int    j    = 0;

                    // initial method
                    if (secTable.Method == (int)FreeformSecurityMethodTypes.None)
                    {
                        secTable.Method = (int)FreeformSecurityMethodTypes.Alpha1;
                    }

                    if (secTable.Method == (int)FreeformSecurityMethodTypes.Alpha1)
                    {
                        keys = alpha1;
                    }
                    else if (secTable.Method == (int)FreeformSecurityMethodTypes.Alpha2)
                    {
                        keys = alpha2;
                    }
                    else
                    {
                        j = secTable.Method;
                    }

                    // 0 - 7
                    for (int i = 0; i < FreeformSecurityTable.KEY_SIZE; i++)
                    {
                        secTable.LocalPartialKey[i] = Convert.ToByte((exp_mod(keys[i + j], secTable.LocalRandomNumber[i]))
                                                                     ^ Convert.ToByte(0x50 + i));
                    }

                    // 8
                    j = FreeformSecurityTable.KEY_SIZE;
                    secTable.LocalPartialKey[j] = Convert.ToByte(((secTable.LocalRandomNumber[j] & 0xC3) |
                                                                  (secTable.Method << 2)) ^ 0xA5);

                    // indicates that local partial key is generated and common key is unusable
                    secTable.Flags = (secTable.Flags | FreeformSecurityFlags.LocalPartialKey) & ~FreeformSecurityFlags.CommonKey;
                    result         = secTable.LocalPartialKey;
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                    return(null);
                }

                return(result);
            }
        }