Exemplo n.º 1
0
        /// <summary>
        /// 使用指定的密钥和向量初始化
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="vi">向量</param>
        /// <param name="flag">唯一标识,如果传入标识则会将加密器缓存,获取时将使用缓存</param>
        /// <param name="setting">额外设置</param>
        public YusStringEncrypt(byte[] key, byte[] vi, string flag = null, YusStringEncryptSetting setting = null)
        {
            // 如果没传设置就使用默认设置
            if (setting == null)
            {
                setting = new YusStringEncryptSetting();
            }

            //加密解密是常用功能,预加载所需资源
            Manager = new RijndaelManaged
            {
                Mode      = setting.Mode,
                Padding   = setting.Padding,
                KeySize   = setting.KeySize,
                BlockSize = setting.BlockSize,
                Key       = key, //设定密钥
                IV        = vi   //设定向量
            };

            // 创建加密器
            Encryptor = Manager.CreateEncryptor();
            // 创建解密器
            Decryptor = Manager.CreateDecryptor();

            // 未设置Flag就不操作缓存
            if (flag.YusNullOrWhiteSpace())
            {
                return;
            }

            // 已存在就替换
            if (_InstanceCache.ContainsKey(flag))
            {
                _InstanceCache[flag] = this;
            }
            // 否则就新增
            else
            {
                _InstanceCache.Add(flag, this);
            }
        }
Exemplo n.º 2
0
        /// <param name="key">密钥</param>
        /// <param name="vi">向量</param>
        /// <param name="flag">唯一标识,如果传入标识则会将加密器缓存,获取时将使用缓存</param>
        /// <param name="setting">额外设置</param>
        public static YusStringEncrypt GetInstance(byte[] key, byte[] vi, string flag = null, YusStringEncryptSetting setting = null)
        {
            var instance = GetInstance(flag);

            if (instance != null)
            {
                return(instance);
            }

            return(new YusStringEncrypt(key, vi, flag, setting));
        }