private void CleanupSaltedBytesCache(SaltedBytesCache saltedBytesCache)
 {
     if (saltedBytesCache != null)
     {
         BytesHelper.RemoveArray(saltedBytesCache.SaltedPasswordBytes);
         BytesHelper.RemoveArray(saltedBytesCache.SaltedInitiaVectorBytes);
     }
 }
        public byte[] Decrypt(byte[] secureBytes, uint offset, uint count, SecureHead secureHead)
        {
            string error = null;

            byte[] bytes = null;

            if (isDisposed == true)
            {
                error = "security manager has been disposed.";
            }
            if (BytesHelper.IsNullOrEmptyArray(secureBytes) == true)
            {
                error = "secureBytes array should NOT be null or empty.";
            }
            if (Config.IsValid == false)
            {
                error = "Config is not valid.";
            }
            if (secureHead == null)
            {
                error = "secureHead should NOT be null.";
            }

            if (error == null)
            {
                try
                {
                    SaltedBytesCache decryptionCache = GetDecryptionSaltedBytesCache(secureHead);
                    bytes = AesDecrypt(
                        secureBytes,
                        offset,
                        count,
                        decryptionCache.SaltedPasswordBytes,
                        decryptionCache.SaltedInitiaVectorBytes);
                }
                catch (Exception e)
                {
#if DEBUG
                    Env.Instance.ShowMessage($"decrypt exception desc={e.Message}  stack trace={e.StackTrace}");
#endif
                }
            }
            else
            {
#if DEBUG
                Env.Instance.ShowMessage(error);
#endif
            }



            return(bytes);
        }
        private byte[] EncryptPlainBytes(byte[] plainBytes, uint offset, uint count)
        {
            if (isDisposed == true)
            {
                throw new ArgumentException("security manager has been disposed.");
            }

            if (BytesHelper.IsNullOrEmptyArray(plainBytes) == true)
            {
                throw new ArgumentException("plainBytes array should NOT be null or empty.");
            }

            if (Config.IsValid == false)
            {
                throw new ArgumentException("Config is not valid.");
            }

            if (encryptionCache == null)
            {
                encryptionCache = new SaltedBytesCache(
                    new SecureHead(
                        Config.Salt,
                        Config.InitiaVector,
                        Config.SaltIterations,
                        Config.SaltedInitialVectorBytesCount,
                        Config.SaltedPasswordBytesCount,
                        0),
                    Config.Password);
            }
            if (encryptionCache == null)
            {
                throw new ArgumentException("encryptionCache array should NOT be null.");
            }

            return(AesEncrypt(
                       plainBytes,
                       offset,
                       count,
                       encryptionCache.SaltedPasswordBytes,
                       encryptionCache.SaltedInitiaVectorBytes));
        }