C#將隱私信息(銀行賬戶,身份證號碼)中央部門特別字符調換成*。本站提示廣大學習愛好者:(C#將隱私信息(銀行賬戶,身份證號碼)中央部門特別字符調換成*)文章只能為提供參考,不一定能成為您想要的結果。以下是C#將隱私信息(銀行賬戶,身份證號碼)中央部門特別字符調換成*正文
年夜家在銀行生意業務某些營業時,都可以看到不管是身份證、銀行賬號中央部門都是用*號調換的,上面小編把代碼整頓以下:
/// <summary> /// 將傳入的字符串中央部門字符調換成特別字符 /// </summary> /// <param name="value">須要調換的字符串</param> /// <param name="startLen">前保存長度</param> /// <param name="endLen">尾保存長度</param> /// <param name="replaceChar">特別字符</param> /// <returns>被特別字符調換的字符串</returns> private static string WordStrWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*') { try { int lenth = value.Length - startLen - endLen; string replaceStr = value.Substring(startLen, lenth); string specialStr = string.Empty; for (int i = 0; i < replaceStr.Length; i++) { specialStr += specialChar; } value = value.WordStr(replaceStr, specialStr); } catch (Exception) { throw; } return value; }
後果圖展現以下:
WordStrWithSpecialChar("柯小呆", 1, 0,'*') -->Result: 柯*呆
WordStrWithSpecialChar("622212345678485") -->Result: 6222*******8485
WordStrWithSpecialChar("622212345678485", 4 , 4 , '*') -->Result: 6222*******8485
注:假如傳入的startLen/endLen 跨越字符串長度,將拋出下標越界異常
C#完成參數隱私代碼
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Data.SqlClient; using System.Configuration; //導入自界說的類庫 using _3Layer.DataLayer.DataCommon; using _3Layer.DataLayer.DataCommon.DataAccess; using Library.ClassLibrary.Crypt.DES; namespace CHONGCHONG.XML public class RenderingXML : System.Web.UI.Page { /// <summary> /// 從數據庫預生成XML數據源 /// </summary> private void PreRenderXML() { string strSQL = "select語句略去........................; myDataLayer.Open(); RenderingXml="<?xml version='1.0' encoding='gb2312'?>\r\n"; RenderingXml+="<xml>\r\n"; try { System.Data.SqlClient.SqlDataReader myDR = (SqlDataReader)myDataLayer.ExecuteReader( strSQL ); while(myDR.Read()) { RenderingXml+="<TreeNode id='"+myDR["BoardID"]+"'>\r\n"; RenderingXml+="<NodeText>"+myDR["BoardName"]+"</NodeText>\r\n"; RenderingXml+="<title>"+myDR["Title"]+"</title>\r\n"; RenderingXml+="<NodeUrl>"+EncodeHTML ( EncodeParameter( myDR["Link"].ToString() ) )+"</NodeUrl>\r\n"; RenderingXml+="<child>"+myDR["children"]+"</child>\r\n"; RenderingXml+="<target>"+myDR["Target"]+"</target>\r\n"; RenderingXml+="</TreeNode>\r\n"; } } catch(System.Data.SqlClient.SqlException ee) { return ; } finally { myDataLayer.Close() ; } RenderingXml+="</xml>"; byte[] bytResult = Encoding.Default.GetBytes( RenderingXml ) ; Response.ContentType = "text/xml" ; Response.BinaryWrite( bytResult ) ; } /// <summary> /// Description:加密途徑參數 /// </summary> /// <param name="sourParameter"></param> /// <returns></returns> private string EncodeParameter( string sourParameter ) { string startString = String.Empty ; string endString = String.Empty ; StringBuilder destParameter = new StringBuilder() ; if( sourParameter == null || sourParameter.Equals("") ) { destParameter.Append( String.Empty ).ToString() ; } else { //開端剖析途徑裡的?字符 if( sourParameter.IndexOf("?")<0 ) { destParameter.Append( sourParameter ).ToString() ; } else { //以?號朋分途徑 string[] paramPath = sourParameter.Split( new char[]{'?'} ) ; startString = paramPath[0].ToString() ; endString = paramPath[1].ToString() ; //開端剖析途徑裡的&字符 if(sourParameter.IndexOf("&")<0) { //只要一個參數,用=號朋分,直接把NameValue停止Des加密 string[] paramNameValue = endString.Split( new char[]{'='} ) ; string paramName = myDES.Encrypt ( paramNameValue[0].ToString() ,myDESKey ) ; string paramValue = myDES.Encrypt ( paramNameValue[1].ToString() ,myDESKey ) ; destParameter.Append( startString ).Append("?"). Append( paramName ).Append("=").Append( paramValue ) ; } else { //有多個參數,以&號朋分?號前面的途徑 string[] paramJoin = endString.Split( new char[]{'&'} ) ; destParameter.Append( startString ).Append("?"). Append( EncoderNameValue( paramJoin ) ) .ToString() ; } } } return destParameter.ToString() ; } /// <summary> /// Description:加密途徑裡的NameValue參數 /// </summary> /// <param name="sourNameValue"></param> /// <returns></returns> private string EncoderNameValue( string[] sourNameValue ) { string[] paramNameValue ; string paramName ; string paramValue ; StringBuilder sb = new StringBuilder() ; for( int i = 0 ; i <= sourNameValue.Length-1 ; i++ ) { //以=號朋分每一個NameValue參數 paramNameValue = sourNameValue[i].Split( new char[]{'='} ) ; //開端對NameValue加密 paramName = myDES.Encrypt( paramNameValue[0].ToString() ,myDESKey ) ; paramValue = myDES.Encrypt( paramNameValue[1].ToString() ,myDESKey ) ; //存儲加密後的途徑字符串 sb.Append( paramName ).Append("=").Append( paramValue ) ; //能否最初一個NameValue參數,若不是在路基裡添加&參數銜接符 if( i<sourNameValue.Length ) { sb.Append("&") ; } } return sb.ToString() ; } }
以上內容就是C#將隱私信息(銀行賬戶,身份證號碼)中央部門特別字符調換成*的全體內容,願望年夜家愛好。