//protected SECURITY_KEY_INDEX GetSecurityKeyIndex(IFreeformEntity_Msg message)
        //{
        //    int sessionId = (int)message.SessionID;
        //    SECURITY_KEY_INDEX keyIndex = SECURITY_KEY_INDEX.NO_KEY;
        //    if (_securityKeys.ContainsKey(sessionId)) keyIndex = _securityKeys[sessionId];
        //    return keyIndex;
        //}

        private byte[] CreatePartialKey(IFreeformEntity_Msg message, Func <FreeformSecurityTableCollection, SECURITY_KEY_INDEX, byte[]> create)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "CreatePartialKey"))
            {
                byte[] result = default(byte[]);

                try
                {
                    result = create(this.Factory.SecurityTables, message.GetSecurityKeyIndex());
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }
        public byte[] Decrypt(IFreeformEntity_Msg message, byte[] buffer)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "Decrypt"))
            {
                List <byte> result = new List <byte>();

                try
                {
                    if (message == null ||
                        buffer == null ||
                        buffer.Length == 0)
                    {
                        return(new byte[0]);
                    }

                    FreeformSecurityTableCollection securityTables = FFMsgHandlerFactory.Current.GetSecurityTables(message.IpAddress);
                    SECURITY_KEY_INDEX securityKey    = message.GetSecurityKeyIndex();
                    ENCRYP_TARGETS     encryptionType = (ENCRYP_TARGETS)buffer[0];
                    FF_FlowInitiation  flowInitiation = message.FlowInitiation;
                    switch (encryptionType)
                    {
                    case ENCRYP_TARGETS.ET_SdsEncryption:
                    {
                        byte[] bufferCopy = buffer.CopyToBuffer(1, buffer.Length - 1);
                        FreeformEncryptionHelper.Decrypt(securityTables, flowInitiation, securityKey, ref bufferCopy);
                        result.AddRange(bufferCopy);
                    }
                    break;

                    case ENCRYP_TARGETS.ET_SdsAuthentication:
                    case ENCRYP_TARGETS.ET_EFTStyleEncryption:
                    {
                        byte   authenticationByteReceived = buffer[1];
                        byte[] bufferCopy = buffer.CopyToBuffer(2, buffer.Length - 2);
                        FreeformEncryptionHelper.Decrypt(securityTables, flowInitiation, securityKey, ref bufferCopy);

                        byte authenticationByteGenerated = FreeformEncryptionHelper.MakeAuthenticationByte(bufferCopy);
                        if (encryptionType == ENCRYP_TARGETS.ET_EFTStyleEncryption)
                        {
                            byte[] temp = new byte[] { authenticationByteGenerated };
                            FreeformEncryptionHelper.Decrypt(securityTables, flowInitiation, securityKey, ref temp);
                            authenticationByteGenerated = temp[0];
                        }
                        if (authenticationByteReceived == authenticationByteGenerated)
                        {
                            result.AddRange(bufferCopy);
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result.ToArray());
            }
        }
        public List <byte> Encrypt(IFreeformEntity_Msg message, IFreeformEntity_MsgTgt target, List <byte> buffer)
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "Encrypt"))
            {
                List <byte> result = new List <byte>();

                try
                {
                    if (message == null ||
                        buffer == null ||
                        buffer.Count == 0)
                    {
                        return(result);
                    }

                    byte[] source = buffer.ToArray();
                    FreeformSecurityTableCollection securityTables = FFMsgHandlerFactory.Current.GetSecurityTables(message.IpAddress);
                    SECURITY_KEY_INDEX        securityKey          = message.GetSecurityKeyIndex();
                    FF_AppId_Encryption_Types encryptionType       = target.EncryptionType;
                    if (target.PrimaryTarget != null &&
                        target.PrimaryTarget.EncryptionType != FF_AppId_Encryption_Types.None)
                    {
                        encryptionType = target.PrimaryTarget.EncryptionType;
                    }

                    FF_FlowInitiation flowInitiation = message.FlowInitiation;
                    switch (encryptionType)
                    {
                    case FF_AppId_Encryption_Types.Standard:
                    {
                        FreeformEncryptionHelper.Encrypt(securityTables, flowInitiation, securityKey, ref source);
                        result.Add((byte)ENCRYP_TARGETS.ET_SdsEncryption);
                        result.AddRange(source);
                    }
                    break;

                    case FF_AppId_Encryption_Types.AuthByteClearData:
                    case FF_AppId_Encryption_Types.AuthByteEncryptedData:
                    {
                        byte[] authenticationByte = new byte[] { FreeformEncryptionHelper.MakeAuthenticationByte(source) };
                        if (encryptionType == FF_AppId_Encryption_Types.AuthByteEncryptedData)
                        {
                            result.Add((byte)ENCRYP_TARGETS.ET_EFTStyleEncryption);
                            FreeformEncryptionHelper.Encrypt(securityTables, flowInitiation, securityKey, ref authenticationByte);
                        }
                        else
                        {
                            result.Add((byte)ENCRYP_TARGETS.ET_SdsAuthentication);
                        }
                        FreeformEncryptionHelper.Encrypt(securityTables, flowInitiation, securityKey, ref source);
                        result.AddRange(authenticationByte);
                        result.AddRange(source);
                    }
                    break;

                    default:
                        result = buffer;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }

                return(result);
            }
        }