Steve Staple Steve Staple - 6 months ago 51
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?


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.