C#隱蔽手機號、郵箱等敏感信息的完成辦法。本站提示廣大學習愛好者:(C#隱蔽手機號、郵箱等敏感信息的完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#隱蔽手機號、郵箱等敏感信息的完成辦法正文
Intro
做項目標時刻,頁面上有一些敏感信息,須要用“*”隱蔽一些比擬主要的信息,因而盤算寫一個通用的辦法。
Let's do it !
Method 1:指定閣下字符數目
Method 1.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)) { return ""; } StringBuilder sbText = new StringBuilder(); int hiddenCharCount = info.Length - left - right; if (hiddenCharCount > 0) { string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right); sbText.Append(prefix); for (int 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(); }
Method 1.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)) { return ""; } StringBuilder sbText = new StringBuilder(); int 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(); }
Method 2 : “*”數目必定,設置為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)) { return ""; } if (sublen<=1) { sublen = 3; } int 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; } else { if (basedOnLeft) { string prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1); return prefix + "****"; } else { string suffix = subLength > 0 ? info.Substring(info.Length-subLength) : info.Substring(info.Length-1); return "****"+suffix; } } }
擴大
手機號 1
/// <summary> /// 隱蔽手機號概況 /// </summary> /// <param name="phone">手機號</param> /// <param name="left">右邊保存字符數</param> /// <param name="right">左邊保存字符數</param> /// <returns></returns> public static string HideTelDetails(string phone, int left = 3, int right = 4) { return HideSensitiveInfo(phone, left, right); }
測試成果以下:
手機號 2
/// <summary> /// 隱蔽手機號概況 /// </summary> /// <param name="phone">手機號</param> /// <param name="left">右邊保存字符數</param> /// <param name="right">左邊保存字符數</param> /// <returns></returns> public static string HideTelDetails(string phone, int left = 3, int right = 4) { return HideSensitiveInfo1(phone, left, right); }
測試成果以下:
郵件地址
/// <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)) { return ""; } if (System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))//假如是郵件地址 { int suffixLen =email.Length - email.LastIndexOf('@'); return HideSensitiveInfo(email, left, suffixLen,false); } else { return HideSensitiveInfo(email); } }
測試成果以下:
以上所述是小編給年夜家引見的C#隱蔽手機號、郵箱等敏感信息的完成辦法,願望對年夜家有所贊助,假如年夜家有任何疑問迎接給我留言,小編會實時答復年夜家的,在此也異常感激年夜家對網站的支撐!