Когда я дешифрую эту строку как жестко заданный параметр, я получаю ожидаемое пятизначное число.
string result = Decrypt (HttpUtility.UrlDecode ("ha3bEv8A% 2ffs0goPGeO6NPQ% 3d% 3d"));
Однако, когда я пытаюсь извлечь его из URL ...
http://localhost:12345/pagename?id=ha3bEv8A%2ffs0goPGeO6NPQ%3d%3d
string result1 = Decrypt (Request.QueryString ["id"]);
Метод Decrypt выдает ошибку «Заполнение недопустимо и не может быть удалено».Я удостоверился, что PaddingMode совпадает, что, по-видимому, является причиной такого рода вещей в большинстве случаев.
Кроме того, я заметил единственное различие между тем, что UrlDecode () и QueryString () производят, - это определенные буквыв кепках в первом.
ha3bEv8A / fs0goPGeO6NPQ ==
ha3bev8a / fs0gopgeo6npq ==
Есть ли параметр, который заставил бы его генерировать ту же строку?Потому что один расшифровывает правильно, а другой выдает ошибки.
Я включил методы шифрования и дешифрования, которые я использую ...
private string Encrypt(string clearText)
{
string EncryptionKey = "hyddhrii%2moi43Hd5%%";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
byte[] salt =
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,0x65, 0x64, 0x76,
0x65, 0x64, 0x65, 0x76 };
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb =
new Rfc2898DeriveBytes(EncryptionKey, salt);
encryptor.Padding = PaddingMode.PKCS7;
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;
}
public static string Decrypt(string cipherText)
{
string EncryptionKey = "hyddhrii%2moi43Hd5%%";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
byte[] salt = new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey,
salt);
encryptor.Padding = PaddingMode.PKCS7;
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;
}