2

I am using microsoft anti xss library to form security. I am using HtmlFormUrlEncode method.

How can I decode my recorded data?

sample data :

mail%40sample.com
%c3%96nder  
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
onder
  • 795
  • 3
  • 14
  • 32
  • I have a very very bad solution : return encodedString.Replace("%c7%b4","Ǵ").Replace("%c7%b3","dz").Replace("%c7%b2","Dz").Replace("%c7%b1","DZ")... – onder Jun 30 '11 at 12:06

1 Answers1

7

AntiXSS doesn't provide a decode - the .NET framework tends to do it for you. Indeed, UrlDecode should actually get it correct.

However, if you want to do it manually should be able to create a char[] array that's the same length as the original string, then loop through the string, looking for a % and then grabbing the two characters after, ensuring they're valid hex and then doing the following to get the hex value

if ((h >= '0') && (h <= '9'))
{
    return (h - '0');
}
if (((h >= 'a') && (h <= 'f')) ||
    ((h >= 'A') && (h <= 'F')))
{
    return ((h - 'a') + 10);
}

Once you have both values you'd then combine

byte b = (byte) ((firstByte << 4) | secondByte);

And append it to the array by calling UTF8Encoding.GetChars()

(note: Code is off the top of my head and not tested properly)

blowdart
  • 55,577
  • 12
  • 114
  • 149