C replaces the special characters in the middle part of the private information of bank account and ID number with *

  • 2021-07-26 08:44:42
  • OfStack

When you trade some businesses in the bank, you can see that both the ID card and the middle part of the bank account are replaced by *. The following site sorts out the codes as follows:


/// <summary>
///  Replace the middle part characters of the passed string with special characters 
/// </summary>
/// <param name="value"> String to be replaced </param>
/// <param name="startLen"> Pre-retention length </param>
/// <param name="endLen"> Tail retention length </param>
/// <param name="replaceChar"> Special character </param>
/// <returns> String replaced by special characters </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;
}

The renderings are shown as follows:


ReplaceWithSpecialChar(" Ke Xiaodai ", 1, 0,'*') -->Result: Ke * Stay
ReplaceWithSpecialChar("622212345678485") -->Result: 6222*******8485
ReplaceWithSpecialChar("622212345678485", 4 , 4 , '*') -->Result: 6222*******8485

Note: If the passed startLen/endLen exceeds the string length, a subscript out-of-bounds exception will be thrown

C # Implementation Parameter Privacy Code


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;
// Import a custom class library 
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>
///  Pre-build from database XML Data source 
/// </summary>
private void PreRenderXML()
{
string strSQL = "select Statement omitted ........................;
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: Encryption path parameters 
/// </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
{
// Start analyzing the ? Character 
if( sourParameter.IndexOf("?")<0 )
{
destParameter.Append( sourParameter ).ToString() ;
}
else
{
// With ? Number partition path 
string[] paramPath = sourParameter.Split( new char[]{'?'} ) ;
startString = paramPath[0].ToString() ;
endString = paramPath[1].ToString() ;
// Start analyzing the & Character 
if(sourParameter.IndexOf("&")<0)
{
// Only 1 Parameters , Use = Sign division , Directly put NameValue Go on Des Encryption 
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
{
// There are multiple parameters , With & Sign division ? The path after the number 
string[] paramJoin = endString.Split( new char[]{'&'} ) ;
destParameter.Append( startString ).Append("?").
Append( EncoderNameValue( paramJoin ) ) .ToString() ;
}
}
}
return destParameter.ToString() ;
} 
/// <summary>
/// Description: In the encrypted path NameValue Parameter 
/// </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++ )
{
// Divide each with a = sign NameValue Parameter 
paramNameValue = sourNameValue[i].Split( new char[]{'='} ) ;
// Begin to be right NameValue Encryption 
paramName = myDES.Encrypt( paramNameValue[0].ToString() ,myDESKey ) ;
paramValue = myDES.Encrypt( paramNameValue[1].ToString() ,myDESKey ) ;
// Stores encrypted path strings 
sb.Append( paramName ).Append("=").Append( paramValue ) ;
// Is it last 1 A NameValue Parameter, if not added in subgrade & Parameter connector 
if( i<sourNameValue.Length )
{
sb.Append("&") ;
}
}
return sb.ToString() ;
}
} 

The above content is the whole content of C # replacing the special characters in the middle of private information (bank account, ID number) with *. I hope everyone likes it.


Related articles: