網址: https://aaa.com/controller/?參數1&參數2
加密後參數=Server.UrlEncode(參數.aesEncryptBase64(key))
解密後參數=Request.QueryString[0].aesDecryptBase64(key)
參數1=解密後參數.Split('&')[0]
參數2=解密後參數.Split('&')[1]
※加密使用AES
public static string aesEncryptBase64(this string SourceStr, string CryptoKey)
{
string encrypt = "";
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
byte[] key = sha256.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
byte[] iv = md5.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
aes.Key = key;
aes.IV = iv;
byte[] dataByteArray = Encoding.UTF8.GetBytes(SourceStr);
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
encrypt = Convert.ToBase64String(ms.ToArray());
}
return encrypt;
}
public static string aesDecryptBase64(this string SourceStr, string CryptoKey)
{
string decrypt = "";
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
byte[] key = sha256.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
byte[] iv = md5.ComputeHash(Encoding.UTF8.GetBytes(CryptoKey));
aes.Key = key;
aes.IV = iv;
byte[] dataByteArray = Convert.FromBase64String(SourceStr);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(ms.ToArray());
}
}
return decrypt;
}
沒有留言:
張貼留言