简体   繁体   English

如何加密/解密查询字符串数据

[英]How to Encrypt/Decrypt query string data

I render an ActionLink like this: 我这样渲染一个ActionLink:

@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= 我想对查询字符串进行加密,如下所示: 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? 如何使用以上方法对我的查询字符串进行加密和解密?

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. 您说要加密 (防止窃听者查看机密数据),但听起来更像是要编码 -格式化数据以便可以安全地用作URI组件。

The example you show looks like base64: 您显示的示例看起来像base64:

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

Another approach is Uri.EscapeString: 另一种方法是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. 这可能是优点或缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM