我想将id传递给我的网页的QueryString,因为我不希望访问者看到该号码,我使用RijndaelManaged算法对其进行编码,我的问题是,这种加密有时会添加一个字符,例如“+”导致双转义序列错误。 我想知道是否有办法从加密输出中排除某些字符。
我的加密代码如下:
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);
}
答案 0 :(得分:1)
您可以将bytearray转换为Hex字符串而不是base64字符串。十六进制只包含[a-f0-9]
有关详细信息,请参阅this question。
但是对于您的原始问题:您应该对查询字符串使用URL编码,这将解决+
字符的问题。