我完全理解你想要什么而不是使用 htttp 传递 url: //mydomain.com? Id = 123,你想传递一个加密的值。而当有人点击这个带有加密 id 的 url 时,你想解密 url 的值。
有两个过程:
在加密中:
1 - 将“ID”(通常是整数)转换为字符串。示例 var NewId = Convert.ToString ((ID);
2 - 使用短语随机加密。示例:“我爱巧克力”(如果你有的话,这个短语可以从你的参数数据库中找到......)
在解密中:
1 - 使用相同的阻止短语来解读。
2 - 使用 Convert.ToInt32 (Shuffled Variable Above) 再次将解密的内容转换为整数
您将必须实现 2 个功能:
加密函数:
private string Encrypt(string clearText)
{
string EncryptionKey = "I love chocolate";
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
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.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
解密函数:
private string Decrypt(string cipherText)
{
string EncryptionKey = "I love chocolate";
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 = System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
将 ID 加密为 ID = "123" 时,ID = "B8 + iXv5 / 8BUQEbHt8 // fGA =="。
当你解密 ID 值 "B8 + iXv5 / 8BUQEbHt8 // fGA ==" 你会再次得到 "123"。
C# 示例:
var OriginalId = 123;
var EncrypetedId = Encrypt(Convert.ToString(OriginalId));
//for recovering original value
var OriginalID = Convert.ToInt32(Decrypt(EncrypetedId));
希望对你有帮助。