Click here to Skip to main content
15,889,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Response.Redirect("WebForm2.Aspx?Parameter=" + Server.UrlEncode(txtName.Text));

But its not encoding.
Posted
Updated 5-Sep-11 0:52am
v2
Comments
Prerak Patel 5-Sep-11 6:53am    
Saying 'not encoding' is not enough. Put a sample text and it's encoding for better understanding.

You're doing it wrong.

I know, my answer might seem vague, or even obtuse, but the qualify of the question dictates the quality of the answer.

Hint: WHAT is the content of the string you're trying to encode?
 
Share this answer
 
URLEncode converts Spaces ( ) to plus signs (+) and Non-alphanumeric characters to their hexadecimal representation. It will not convert string.

You can use the below encoding mechanism to convert to different format.
public string fnEncode(string str)
{
	byte[] byteEncode = new byte[str.Length + 1];
	string strEncoded = null;

	byteEncode = System.Text.Encoding.UTF8.GetBytes(str);
	strEncoded = Convert.ToBase64String(byteEncode);

	return strEncoded;
}

and call this.
Response.Redirect("WebForm2.Aspx?Parameter=" + fnEncode(txtName.Text));


For Decode:
C#
public string fnBase64Decode(string strPwd)
{
	System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
	System.Text.Decoder utf8Decode = null;
	byte[] byteDecode = null;
	int intCharCnt = 0;
	string strDecoded = null;

	utf8Decode = encoder.GetDecoder();
	byteDecode = Convert.FromBase64String(strPwd);
	intCharCnt = utf8Decode.GetCharCount(byteDecode, 0, byteDecode.Length);

	char[] charDecoded = new char[intCharCnt + 1];

	utf8Decode.GetChars(byteDecode, 0, byteDecode.Length, charDecoded, 0);
	strDecoded = new string(charDecoded);

	return strDecoded;
}
 
Share this answer
 
v2
Comments
raj_sagoo 5-Sep-11 7:21am    
how to decode?
Toniyo Jackson 5-Sep-11 7:24am    
Check the updated answer
eshitagupta 13-Apr-13 17:42pm    
whyyy???
eshitagupta 13-Apr-13 17:40pm    
in that case when string has decode with extra \0 like "##########\0" which is giving error while matching with database
use this
C#
public static string Encrypt(string toEncrypt, string key, bool useHashing)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return Convert.ToBase64String(resultArray, 0, resultArray.Length).Replace("+"," ");
    }


    public static string Decrypt(string toDecrypt, string key, bool useHashing)
    {
        toDecrypt = toDecrypt.Replace(" ", "+");
        byte[] keyArray;
        byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return UTF8Encoding.UTF8.GetString(resultArray);
    }


While calling use the syntax
C#
string Id = Decrypt(ID.ToString().Replace("$", "/").Replace(" ", "+"), "Test", true);
string Test = Encrypt(pass.ToString(), "Test", true);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900