我如何可以加密在asp.net中查询字符串? [英] How can I encrypt a querystring in asp.net?
问题描述
我需要加密和解密ASP.NET查询字符串。
I need to encrypt and decrypt a querystring in ASP.NET.
查询字符串可能看起来是这样的:
The querystring might look something like this:
<一个href=\"http://www.mysite.com/report.aspx?id=12345&year=2008\">http://www.mysite.com/report.aspx?id=12345&year=2008
我怎么去,使它看起来像下面这样加密整个查询字符串?
How do I go about encrypting the entire querystring so that it looks something like the following?
<一个href=\"http://www.mysite.com/report.aspx?crypt=asldjfaf32as98df8a\">http://www.mysite.com/report.aspx?crypt=asldjfaf32as98df8a
然后,当然,如何我解密?什么是使用这样的事情最好的加密? TripleDes的?
And then, of course, how to I decrypt it? What's the best encryption to use for something like this? TripleDES?
推荐答案
下面是一个办法做到这一点在VB来源:的 http://www.devcity.net/Articles/47/1/encrypt_querystring.aspx
Here is a way to do it in VB From: http://www.devcity.net/Articles/47/1/encrypt_querystring.aspx
包装器加密code:您通过查询字符串参数到这一点,并更改密钥!
Wrapper for the encryption code: Pass your querystring parameters into this, and change the key!!!
Private _key as string = "!#$a54?3"
Public Function encryptQueryString(ByVal strQueryString As String) As String
Dim oES As New ExtractAndSerialize.Encryption64()
Return oES.Encrypt(strQueryString, _key)
End Function
Public Function decryptQueryString(ByVal strQueryString As String) As String
Dim oES As New ExtractAndSerialize.Encryption64()
Return oES.Decrypt(strQueryString, _key)
End Function
加密code:
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography
Public Class Encryption64
Private key() As Byte = {}
Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Public Function Decrypt(ByVal stringToDecrypt As String, _
ByVal sEncryptionKey As String) As String
Dim inputByteArray(stringToDecrypt.Length) As Byte
Try
key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
Dim des As New DESCryptoServiceProvider()
inputByteArray = Convert.FromBase64String(stringToDecrypt)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), _
CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch e As Exception
Return e.Message
End Try
End Function
Public Function Encrypt(ByVal stringToEncrypt As String, _
ByVal SEncryptionKey As String) As String
Try
key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes( _
stringToEncrypt)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), _
CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch e As Exception
Return e.Message
End Try
End Function
End Class
这篇关于我如何可以加密在asp.net中查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!