时间:2020-12-04 04:13:16 | 栏目:.NET代码 | 点击:次
大家在银行交易某些业务时,都可以看到无论是身份证、银行账号中间部分都是用*号替换的,下面小编把代码整理如下:
/// <summary> /// 将传入的字符串中间部分字符替换成特殊字符 /// </summary> /// <param name="value">需要替换的字符串</param> /// <param name="startLen">前保留长度</param> /// <param name="endLen">尾保留长度</param> /// <param name="replaceChar">特殊字符</param> /// <returns>被特殊字符替换的字符串</returns> private static string ReplaceWithSpecialChar(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.Replace(replaceStr, specialStr); } catch (Exception) { throw; } return value; }
效果图展示如下:
注:如果传入的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#将隐私信息(银行账户,身份证号码)中间部分特殊字符替换成*的全部内容,希望大家喜欢。