Ejemplo n.º 1
0
        /// <summary>
        /// 加密之后格式。 手机号码格式:$手机号码前3位明文$base64(encrypt(phone后8位))$111$
        /// nick格式:~base64(encrypt(nick))~111~
        /// </summary>
        /// <param name="data">明文数据</param>
        /// <param name="type">加密字段类型(例如:nick\phone)</param>
        /// <param name="session">用户身份,用户级加密必填</param>
        /// <param name="version">秘钥历史版本</param>
        /// <returns></returns>
        private string Encrypt(string data, string type, string session, Nullable <Int64> version)
        {
            if (string.IsNullOrEmpty(data))
            {
                return(data);
            }
            SecretContext secretContext = secretCore.GetSecret(session, version);

            if (secretContext == null)
            {
                throw new SecretException("secretKey is null");
            }
            if (secretContext.Secret == null)
            {
                return(data);
            }

            string separator = null;

            SecurityBiz.GetSeparatorMap().TryGetValue(type, out separator);
            if (separator == null)
            {
                throw new SecretException("type error");
            }

            SecurityCounter.AddEncryptCount(type);// 计数器
            bool isEncryptIndex = secretCore.IsIndexEncrypt(type, version);

            // 支持密文检索
            if (isEncryptIndex)
            {
                if (PHONE.Equals(type))
                {
                    return(SecurityBiz.EncryptPhoneIndex(data, separator, secretContext));
                }
                else
                {
                    int compressLen = secretCore.GetCompressLen();
                    int slideSize   = secretCore.GetSlideSize();
                    return(SecurityBiz.EncryptNormalIndex(data, compressLen, slideSize, separator, secretContext));
                }
            }
            else
            {
                if (PHONE.Equals(type))
                {
                    return(SecurityBiz.EncryptPhone(data, separator, secretContext));
                }
                else
                {
                    return(SecurityBiz.EncryptNormal(data, separator, secretContext));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 密文检索。 手机号码格式:$base64(H-MAC(phone后4位))$ simple格式:base64(H-MAC(滑窗))
        /// </summary>
        /// <param name="data">明文数据</param>
        /// <param name="type">加密字段类型(例如:simple\phone)</param>
        /// <param name="session">用户身份,用户级加密必填</param>
        /// <param name="version">秘钥历史版本</param>
        /// <returns></returns>
        private string Search(string data, string type, string session, Nullable <Int64> version)
        {
            if (string.IsNullOrEmpty(data))
            {
                return(data);
            }

            SecretContext secretContext = secretCore.GetSecret(session, version);

            if (secretContext == null)
            {
                throw new SecretException("secretKey is null");
            }
            if (secretContext.Secret == null)
            {
                return(data);
            }

            string separator = null;

            SecurityBiz.GetSeparatorMap().TryGetValue(type, out separator);
            if (separator == null)
            {
                throw new SecretException("type error");
            }

            // 公共秘钥版本号用负数区分
            if (session == null)
            {
                SecretContext publicSecretContext = new SecretContext();
                publicSecretContext.Secret        = secretContext.Secret;
                publicSecretContext.SecretVersion = -secretContext.SecretVersion;
                secretContext = publicSecretContext;
            }

            securityCounter.AddSearchCount(type, session);// 计数器
            if (PHONE.Equals(type))
            {
                return(SecurityBiz.SearchPhoneIndex(data, separator, secretContext));
            }
            else
            {
                int compressLen = secretCore.GetCompressLen();
                int slideSize   = secretCore.GetSlideSize();
                return(SecurityBiz.SearchNormalIndex(data, compressLen, slideSize, secretContext));
            }
        }