Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Encoding problem
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Encoding problem
Environment versions
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Miscellaneous
Thread ID:
01652640
Message ID:
01652640
Views:
51
Hi everybody,

This was developed by our former contractors for string PKs.

We have two functions:
/// <summary>
        /// Decodes the string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FromBase64UrlEncoded(this string str)
        {
            var base64String = HttpUtility.UrlDecode(str);
            var encodedIdBytes = Convert.FromBase64String(base64String);
            var id = Encoding.UTF8.GetString(encodedIdBytes);
            return id;
        }
and also
/// <summary>
        /// Encodes the string
        /// </summary>
        /// <param name="str"></param>
        /// <param name="trimBeforeHashing"></param>
        /// <returns></returns>
        public static string ToBase64UrlEncoded(this string str, bool trimBeforeHashing = false)
        {
            if (trimBeforeHashing && !String.IsNullOrEmpty(str))
            {
                str = str.Trim();
            }
            var idBytes = Encoding.UTF8.GetBytes(str);
            var idBase64String = Convert.ToBase64String(idBytes);
            var idUrlString = HttpUtility.UrlEncode(idBase64String);
            return idUrlString;
        }
The result of the ToBase64UrlEncoded is what we name, for example, categoryHash and what we're sending in our API calls.

It seems to work OK for most of the cases. However, for this category

'COÑAC' the result seems to be incorrect.

The result of this function is
idUrlString = "Q0%2fDkUFDICAgICA%3d"
. Do you see what make it wrong? Also I noticed that it produced 11 bytes while for all other strings it correctly produced 10 bytes (our columns being 10 characters in length). May be we can not use UTF8 here?

Do you see what is wrong here and how should I adjust the above 2 functions to make them bullet-proof?

Thanks a lot in advance.

UPDATE. So, the issue here is the %2F character that converts to /

The question still remains as what to do with the code above to avoid such sequence.
If it's not broken, fix it until it is.


My Blog
Reply
Map
View

Click here to load this message in the networking platform