所以我最近开始加密所有查询字符串参数,现在我遇到的问题是检查空值
if ((Security.DecryptQueryString(HttpUtility.UrlDecode(Request.QueryString["Artigo"]))) != null)
{
int artigoID = Convert.ToInt32(Security.DecryptQueryString(HttpUtility.UrlDecode(Request.QueryString["Artigo"])));
hdfIdArtigo.Value = artigoID.ToString();
BindDraftMode(artigoID);
}
这给了我一个例外,就是没有将对象设置为对象的实例...
当值加密或没有值时,如何检查空值?问题是当我没有向其他页面发送任何参数时,因此我不知道加密后如何检查值
编辑:
这是解密功能
public static string DecryptQueryString(string cipherText)
{
string EncryptionKey = "hyddhrii%2moi43Hd5%%";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}