2

How do I encrypt and decrypt Model values that are passed using RedirectToAction

public ActionResult CustomerDetails(CustomerModel model)
  {
        return RedirectToAction("ConfirmDetails", model);
  }

I want to encrypt all values in the model and decrypt them in ConfirmDetails method.

Angwenyi
  • 319
  • 1
  • 4
  • 16

1 Answers1

0

I am using this decrypt and encrypt method myself, and it works perfect. You can do anything with the output.

Note: read the post and do what he says.

To run the encryption:

Using it is easy: just instantiate the class and then call EncryptToString(string StringToEncrypt) and DecryptString(string StringToDecrypt) as methods. It couldn't be any easier (or more secure) once you have this class in place.

In your case it would be:

SimpleAES SEAS = new SimpleAES();
string decryptedstring = SEAS.EncryptToString(model.modelitem);

To decrypt the encrypted string:

SimpleAES SEAS = new SimpleAES();
string encryptedstring = SEAS.DecryptString(model.modelitem);

EDIT | Just to pass all model items in:

List<string> EncryptedValues = new List<string>();
SimpleAES SEAS = new SimpleAES();
foreach(var modelitem in model)
{
     EncryptedValues.Add(SEAS.EncryptToString(Convert.ToString(modelitem)));
}

// Do something with the list.
Community
  • 1
  • 1
Max
  • 846
  • 1
  • 9
  • 26
  • This means, I have to encrypt and decrypt every value in the model. I was looking for a away to just encrypt the model object like this `Encrypt(model)` – Angwenyi Nov 23 '16 at 07:31
  • I have 10 model values of strings and dates. – Angwenyi Nov 23 '16 at 07:39
  • @Angwenyi See edited answer. NOTE: be sure to convert the datetime to string! You need to have strings only! It can't convert the datetime property. – Max Nov 23 '16 at 07:40
  • @Angwenyi, is this not the method you are looking for? – Max Nov 30 '16 at 09:45