让我解释一下情况。在我的代码中,我将用户标识加密为注册完成后生成的字符串。加密后的字符串作为 url 参数传递,如下所示。
string ConfirmCode = string.Empty;
Common.Secure.Security mySec = new Common.Secure.Security();
ConfirmCode = mySec.Encrypt(myMember.MemberID.ToString());
string EmailValidationLink = "<a href=\'" + pageScheme + "://" + CurrentDomain.HostName + "/Contents/Common/" + "EN" + "/" + CurrentDomain.EmailValidationPage + "?ConfirmCode=" + **UrlEncode(ConfirmCode)** + "\'>Click here to validate your account.</a>";
和:
protected String UrlEncode(String text)
{
return HttpContext.Current.Server.UrlEncode(text);
}
我正在使用 UrlEncode 传递加密字符串,但问题是加密文本是否有一个 '+' 符号,而解密时我得到一个 Format Exception ,因为这个 '+' 符号被视为一个 ' ' 空间。
例如,如果加密文本在获取时类似于“S7+5tZzTm0k=”,则 + 符号被视为“S7 5tZzTm0k=”,这会导致格式异常。
我的加密和解密代码如下
public string Decrypt(string Text)
{
if (!ENABLED) return Text;
des.Key = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
des.Mode = CipherMode.ECB;
ICryptoTransform desdencrypt = des.CreateDecryptor();
Byte[] buff = Convert.FromBase64String(Text);
return ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
}
public string Encrypt(string Text)
{
if (!ENABLED) return Text;
des.Key = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
des.Mode = CipherMode.ECB;
ICryptoTransform desdencrypt = des.CreateEncryptor();
ASCIIEncoding MyASCIIEncoding = new ASCIIEncoding();
Byte[] buff = ASCIIEncoding.ASCII.GetBytes(Text);
return Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
}
我一直在谷歌上搜索这个,每个人都建议使用 UrlEncode 可以解决问题,但在我的情况下,即使使用它也没有解决问题。
请让我知道该怎么做?
if (Request.QueryString["ConfirmCode"] != null)
{
bool isAccountSuspended;
isAccountSuspended = AccountManagement.AcountManager.EmailValidationNote(HttpContext.Current.Server.UrlDecode(Request.QueryString["ConfirmCode"]));
if (!isAccountSuspended)
{
lblMessage.Text = "Your account is already suspended. Please contact customer service.";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
else
{
lblMessage.Text = "Thank you for verifying your email address. We hope you’ll enjoy playing with us.";
}
}
问候斯里维迪亚