Thomas
Thomas

Reputation: 34188

How to Encrypt/Decrypt query string data

I render an ActionLink like this:

@Html.ActionLink(techName, "Details","Home", new { TopicID = item.TechID },null)

I would like to encrypt the query string, something like this: Home/Details?TopicID=Ek7vP1YwVhc=

I searched on this topic and found a piece of code to encrypt and decrypt the data:

(new MvcSerializer()).Serialize(<Your data here>, SerializationMode.EncryptedAndSigned)

And then to reverse the process...

(new MvcSerializer()).Deserialize(<Serialized data here>, SerializationMode.EncryptedAndSigned)

How to use the above approach to encrypt and decrypt my query string?

Upvotes: 0

Views: 1124

Answers (1)

Timo
Timo

Reputation: 8680

You say you wish to encrypt (prevent eavesdroppers from being able to look at secret data), but it sounds more like you want to encode - to format data such that it can safely be used as a URI component.

The example you show looks like base64:

var base64EncodedText = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(myText));

Another approach is Uri.EscapeString:

var uriEncodedText = Uri.EscapeString(myText);

The latter only changes special characters and thus may look more human-readable. This can be an advantage or a disadvantage.

Upvotes: 1

Related Questions