在項目中,有時候一些信息不便完全顯示,只需要顯示部分信息。現在提供一些隱藏敏感信息的操作方法,主要為對信息的截取操作: 1.指定左右字元數量,中間的*的個數和實際長度有關: 2.指定左右字元數量,中間的*的個數固定: 3.“*”數量一定,設置為4個,按信息總長度的比例來取,預設左右各取1/3: 4. ...
在項目中,有時候一些信息不便完全顯示,只需要顯示部分信息。現在提供一些隱藏敏感信息的操作方法,主要為對信息的截取操作:
1.指定左右字元數量,中間的*的個數和實際長度有關:
/// <summary> /// 隱藏敏感信息 /// </summary> /// <param name="info">信息實體</param> /// <param name="left">左邊保留的字元數</param> /// <param name="right">右邊保留的字元數</param> /// <param name="basedOnLeft">當長度異常時,是否顯示左邊 /// <code>true</code>顯示左邊,<code>false</code>顯示右邊 /// </param> /// <returns></returns> public static string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft = true) { if (string.IsNullOrEmpty(info)) { throw new ArgumentNullException(info); } var sbText = new StringBuilder(); var hiddenCharCount = info.Length - left - right; if (hiddenCharCount > 0) { string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right); sbText.Append(prefix); for (var i = 0; i < hiddenCharCount; i++) { sbText.Append("*"); } sbText.Append(suffix); } else { if (basedOnLeft) { if (info.Length > left && left > 0) { sbText.Append(info.Substring(0, left) + "****"); } else { sbText.Append(info.Substring(0, 1) + "****"); } } else { if (info.Length > right && right > 0) { sbText.Append("****" + info.Substring(info.Length - right)); } else { sbText.Append("****" + info.Substring(info.Length - 1)); } } } return sbText.ToString(); }
2.指定左右字元數量,中間的*的個數固定:
/// <summary> /// 隱藏敏感信息 /// </summary> /// <param name="info">信息實體</param> /// <param name="left">左邊保留的字元數</param> /// <param name="right">右邊保留的字元數</param> /// <param name="basedOnLeft">當長度異常時,是否顯示左邊 /// <code>true</code>顯示左邊,<code>false</code>顯示右邊 /// <returns></returns> public static string HideSensitiveInfo1(string info, int left, int right, bool basedOnLeft = true) { if (string.IsNullOrEmpty(info)) { throw new ArgumentNullException(info); } var sbText = new StringBuilder(); var hiddenCharCount = info.Length - left - right; if (hiddenCharCount > 0) { string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right); sbText.Append(prefix); sbText.Append("****"); sbText.Append(suffix); } else { if (basedOnLeft) { if (info.Length > left && left > 0) { sbText.Append(info.Substring(0, left) + "****"); } else { sbText.Append(info.Substring(0, 1) + "****"); } } else { if (info.Length > right && right > 0) { sbText.Append("****" + info.Substring(info.Length - right)); } else { sbText.Append("****" + info.Substring(info.Length - 1)); } } } return sbText.ToString(); }
3.“*”數量一定,設置為4個,按信息總長度的比例來取,預設左右各取1/3:
/// <summary> /// 隱藏敏感信息 /// </summary> /// <param name="info">信息</param> /// <param name="sublen">信息總長與左子串(或右子串)的比例</param> /// <param name="basedOnLeft"/>當長度異常時,是否顯示左邊,預設true,預設顯示左邊 /// <code>true</code>顯示左邊,<code>false</code>顯示右邊 /// <returns></returns> public static string HideSensitiveInfo(string info, int sublen = 3, bool basedOnLeft = true) { if (string.IsNullOrEmpty(info)) { throw new ArgumentNullException(info); } if (sublen <= 1) { sublen = 3; } var subLength = info.Length / sublen; if (subLength > 0 && info.Length > (subLength * 2)) { string prefix = info.Substring(0, subLength), suffix = info.Substring(info.Length - subLength); return prefix + "****" + suffix; } if (basedOnLeft) { var prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1); return prefix + "****"; } var suffixs = subLength > 0 ? info.Substring(info.Length - subLength) : info.Substring(info.Length - 1); return "****" + suffixs; }
4.隱藏右鍵詳情
/// <summary> /// 隱藏右鍵詳情 /// </summary> /// <param name="email">郵件地址</param> /// <param name="left">郵件頭保留字元個數,預設值設置為3</param> /// <returns></returns> public static string HideEmailDetails(string email, int left = 3) { if (string.IsNullOrEmpty(email)) { throw new ArgumentNullException(email); } if (!System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) return HideSensitiveInfo(email); var suffixLen = email.Length - email.LastIndexOf('@'); return HideSensitiveInfo(email, left, suffixLen, false); }
在一些信息的隱藏操作,可以採用js實現,也可以採用jquery插件完成操作,但是在前臺進行這樣的操作,存在一些風險,在後臺完成對字元的信息截取,可以很好的對信息進行保護。