I have a series of encrypted strings that I use as ids in a C#/ASP.Net web application. I need to use these in the id attribute, however, the string contain illegal characters (valid characters are only ´[A-Za-z0-9-_:.]`). I need a two-way conversion that maps my encrypted strings to this small set. Something like Base64, but smaller.
What are my alternatives? Are there standard algorithms for this, or is it odd enough I have to invent it myself?
Solution:
In case someone needs this, here’s what I ended up doing. Replace non-valid characters, and remove the padding = char. Then undo this to revert.
private static string MakeReferenceJavascriptCompatible(string reference)
{
return reference.Replace("+", "_")
.Replace("/", "-")
.Replace("=", "");
}
private static string UndoMakeReferenceJavascriptCompatible(string reference)
{
int padding = 4 - (reference.Length % 4);
return reference.Replace("-", "/")
.Replace("_", "+")
.PadRight(reference.Length + padding, '=');
}
If you’ve got A-Z (26), a-z (26), 0-9 (10) and ‘_’, ‘:’ and ‘.’ (3) then you’ve got 65 characters available, just like Base64. (It needs 65 rather than 64 due to using = as padding at the end.) It wasn’t clear to me whether you also were including
-, which would give you 66… positively rich in characters available 🙂It sounds like you just need to convert the “normal” form to your slightly different alphabet. You could do this either by finding a flexible base64 implementation which lets you specify the values to use, or by just calling
string.Replace:Reverse the replacements to get back from your “modified” base64 to “normal” base64 and it’ll be valid for
Convert.FromBase64String.