Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
how to do masking/hiding email address in c#

i have an issue, i have to apply masking/hiding part of email address in c#. example

jhon@abc.com==> jh**n@abc.com
bigjhon@abc.com==> bi****n@abc.com
brotherhood@abc.com==>br*******od@abc.com

i have this code but its giving exception for some emails. "Index was outside the bounds of the array."

for (int i = 0; i < eml.Length; i++)
{
 int j = i == (eml.Length - 1) ? 0 : 1;
 cc = eml[i].ToString();
 if (i <= 1)
 {
  dispeml += cc;
 }
 else 
 if (eml[i + (j + k)].ToString() == "@")
 {
  dispeml += cc;
  k = 0;
  fl = 1;
 }
 else 
 if (eml[i + j].ToString() == "@")
 {
  dispeml += cc;
  fl = 1;
 }
 else 
 if (fl == 1)
 {
  dispeml += cc;
 }
 else
 {
  dispeml += "*";
 }
}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Here is a approach to solve this with Regex

string input = "jhon@abc.com";
string pattern = @"(?<=[\w]{1})[\w-\._\+%]*(?=[\w]{1}@)";
string result = Regex.Replace(input, pattern, m => new string('*', m.Length));
//j**n@abc.com

Explanation:

(?<=[\w]{1}) the name has to start with 1 word-character

[\w-\._\+%]* the replacement-part can contain 0-n word characters including -_.+%

(?=[\w]{1}@) the name has to end with one word character followed by a @

Depending on the amount of characters you want to remain unchanged you can change {1} to {2} or something else at the beginning or at the end.

over 4 years ago · Santiago Trujillo Denunciar

0

If you always want to mask anything between first character and last character before @ with fixed number of masked characters , you can use the below

var email="abcdef@ghij.com";
var maskedEmail = string.Format("{0}****{1}", email[0], 
email.Substring(email.IndexOf('@')-1));

You can alter the above line for your requirement.

The above line will give you the result "a****f@ghij.com"

Note that masking the email always with a fixed number of characters will make it difficult to guess the email and is slightly more secure.

ex: ab@c.com

after mask: a****b@c.com

over 4 years ago · Santiago Trujillo Denunciar

0

I can't see where your k variable is initialised in your code snippet. If I had to take a wild stab as to why you are getting an index out of bounds exception that would be my stab.

Although I would say that you could achieve something very similar to what you are doing using Regex. I did it like this:

public string ObfuscateEmail(string email)
{
    var displayCase = email;

    var partToBeObfuscated = Regex.Match(displayCase, @"[^@]*").Value;
    if (partToBeObfuscated.Length - 3 > 0) {
        var obfuscation = "";
        for (var i = 0; i < partToBeObfuscated.Length - 3; i++) obfuscation += "*";
        displayCase = String.Format("{0}{1}{2}{3}",  displayCase[0], displayCase[1], obfuscation, displayCase.Substring(partToBeObfuscated.Length - 1));
    } else if (partToBeObfuscated.Length - 3 == 0) {
        displayCase = String.Format("{0}*{1}", displayCase[0], displayCase.Substring(2));
    }

    return displayCase;
}

Here is a fiddle of all your test cases provided passing pretty close to what you were describing https://dotnetfiddle.net/fU2RUo

[EDIT] My code doesn't try to obfuscate emails whose addresses before the @ are less than 3 characters long, if this is a requirement you would need to amend the code but I didn't think it was a very realistic case to have to build a case for.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda