public byte[] Unprotect(byte[] protectedData)
        {
            if (protectedData == null || protectedData.Length < KeyIdLength + 1)
            {
                return(EmptyBytes);
            }

            Guid id;

            try
            {
                id = new Guid(protectedData.Take(KeyIdLength).ToArray());
            }
            catch
            {
                return(EmptyBytes);
            }
            var key = Base256Encoders.Instance.GetKeys().FirstOrDefault(t => t.Id == id);

            if (key == null)
            {
                _logger.LogWarning($"Key not found to Unprotect.");
                return(EmptyBytes);
            }
            var random = protectedData.Skip(KeyIdLength).FirstOrDefault();
            var data   = protectedData.Skip(KeyIdLength + 1).ToArray();

            if (data.Length <= 0)
            {
                return(EmptyBytes);
            }

            byte[] bytes;
            if (this._options.UseCompress)
            {
                _logger.LogDebug($"Use Key '{key.Id}' Unprotect with compress.");
                using (System.IO.MemoryStream memoryStream = new MemoryStream(data))
                {
                    using (System.IO.MemoryStream memoryStream1 = new MemoryStream())
                    {
                        using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
                        {
                            deflateStream.CopyTo(memoryStream1);
                        }
                        bytes = memoryStream1.ToArray();
                    }
                }
            }
            else
            {
                bytes = data;
                _logger.LogDebug($"Use Key '{key.Id}' Unprotect without compress.");
            }
            var _data = Base256Encoders.DecryptFromBase256(bytes, key.MasterKey);

            FromRandom(random, _data);
            return(_data);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <param name="logger"></param>
        public DataProtector(IOptions <DataProtectorOptions> options, ILogger <DataProtector> logger)
        {
            _logger = logger;

            Base256Encoders.Init(options);
            if (Base256Encoders.Instance.CurrentKey == null)
            {
                _logger.LogDebug("Cannot generate the Key");
                throw new Exception("Cannot generate the Key");
            }
            _options = options.Value;
        }
        public byte[] Protect(byte[] plainData)
        {
            if (plainData == null || plainData.Length == 0)
            {
                return(EmptyBytes);
            }
            var key = Base256Encoders.Instance.CurrentKey;

            if (key == null || key.Id == null || key.Id == Guid.Empty ||
                key.MasterKey == null ||
                key.MasterKey.Length != Base256Encoders.MaxLegnth)
            {
                _logger.LogWarning($"Key not found for Protect: [{string.Join(",", plainData)}].");
                return(EmptyBytes);
            }
            plainData = ToRandom(plainData, out byte random);
            var encryptBytes = Base256Encoders.EncryptToBase256(plainData, key.MasterKey);

            if (this._options.UseCompress)
            {
                using (System.IO.MemoryStream memoryStream = new MemoryStream())
                {
                    using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
                    {
                        deflateStream.Write(encryptBytes, 0, encryptBytes.Length);
                    }
                    var bytes = key.Id.ToByteArray().Concat(new byte[] { random }).Concat(memoryStream.ToArray()).ToArray();
                    _logger.LogDebug($"Use Key '{key.Id}' Protect with compress.");
                    return(bytes);
                }
            }
            else
            {
                var bytes = key.Id.ToByteArray().Concat(new byte[] { random }).Concat(encryptBytes).ToArray();
                _logger.LogDebug($"Use Key '{key.Id}' Protect  without compress.");
                return(bytes);
            }
        }
 internal static void Init(IOptions <DataProtectorOptions> _options)
 {
     _Encoder = new Base256Encoders(_options);
 }