0
有人可以告訴我加密和解密查詢字符串的最佳方式是什麼? 什麼算法最好? 對稱算法, 不對稱算法, 數字簽名, 或者其他的算法,我不知道 請幫在asp.net中加密和解密查詢字符串
有人可以告訴我加密和解密查詢字符串的最佳方式是什麼? 什麼算法最好? 對稱算法, 不對稱算法, 數字簽名, 或者其他的算法,我不知道 請幫在asp.net中加密和解密查詢字符串
對稱加密,請查看MSDN文檔和示例:
System.Security.Cryptography.Aes
System.Security.Cryptography.DES
System.Security.Cryptography.RC2
System.Security.Cryptography.Rijndael
System.Security.Cryptography.TripleDES
對於非對稱加密,查看MSDN文檔和示例:
System.Security.Cryptography.DSA
System.Security.Cryptography.ECDiffieHellman
System.Security.Cryptography.ECDsa
System.Security.Cryptography.RSA
示例:
你可以改變這些
static readonly string PasswordHash = "[email protected]@Sw0rd";
static readonly string SaltKey = "[email protected]&KEY";
static readonly string VIKey = "@1B2c3D4e5F6g7H8";
加密:
public static string Encrypt(string plainText)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return Convert.ToBase64String(cipherTextBytes);
}
解密:
public static string Decrypt(string encryptedText)
{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
以上功能的使用:
string strvalue=Encrypt(someid);
Response.Redirect("yourpage.aspx?id=strvalue");
有關重定向頁面:
if(Request.QueryString["id"]!=null)
{
string decryptval=Decrypt(Request.QueryString["id"].ToString());
}
希望這有助於!
來源是什麼?目的是什麼?它是爲了內部製作的網址,還是爲了驗證來自第三方的推介? – sisve
此外,什麼是「最好」的資格?它是最快的嗎?它是最複雜的(最難破解的)它是否會產生最美麗的琴絃?除此之外,我認爲這個問題比http://programmers.stackexchange.com/更適合。 – DrCopyPaste
用於傳輸重要信息 – MiladCr7