Steve Staple Steve Staple - 1 year ago 88
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) {
@Html.DisplayFor(modelItem => item.Phone)
@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 Source

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
      return Decrypt(Phone);

private string Phone;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download