讓我來解釋一下情況。在我的代碼中,我正在加密userid轉換爲註冊完成後生成的字符串。 ecrypted字符串作爲url參數傳遞,如下所示。UrlEncode未對+符號進行編碼,而解密格式異常即將到來
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>";
和:
我使用以UrlEncode通過加密的字符串,但問題是,如果加密的文本有一個「+」符號,而解密我得到一個格式異常,因爲這個'+'符號被視爲一個''空間。
例如,如果加密文本在獲取時與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可以解決這個問題,但對我來說,即使使用它並沒有解決問題後。
請讓我知道如何去做這件事? %2B
-
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.";
}
}
問候 Srividhya
你可以發佈'UrlEncode'的代碼嗎? – Oded 2012-04-05 09:33:34
您的'UrlEncode'方法是否調用'HttpUtility.UrlEncode'? 'HttpUtility.Url * Decode *'方法用''代替'+',但'HttpUtility.UrlEncode'不應該。 – drf 2012-04-05 09:50:37
的確,您是否使用.NET Framework庫的標準UrlEncode方法?因爲如果沒有,你應該。 – 2012-04-05 09:57:42