Steve Staple Steve Staple - 12 days ago 5
C# Question

How to decrypt data for an MVC View?

I am using ASP.NET MVC 4 Runtime, and writing encrypted data to my database using a function.

public string Encrypt(string encryptMe)
{
return Helpers.Encryptor.Encrypt(encryptMe);
}


On return the data is displayed like so:

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)


I want to decrypt the data before displaying it here using the following function:

public static string Decrypt(string decryptMe)
{
return Helpers.Encryptor.Decrypt(decryptMe); ;
}


How can I decrypt my data?

Answer

Add a Property in the model called for example "DecryptedPhone".

This will only have a Get and will call the decrypting function:

public string DecryptedPhone
{
      get
      {
      return Decrypt(Phone);
      }
}

private string Phone;

The phone will be private so that it cannot be accessed.

Comments