System.ArgumentException on base32 encoding string input
DanielHolland opened this issue · 2 comments
When attempting to base32 encode the string "3FC6145A2155E732" the Base32Encoding.ToBytes()
method throws the exception below:
Character is not a Base32 character. Parameter name: c
I have since used sample code from MS which, despite the awful formatting, works and is able to encode the value. The same can be said for online services.
It would appear that the two '1' (one) characters are invalid base-32 characters per the Base32 alphabet. (https://en.wikipedia.org/wiki/Base32)
Base32 only has the following characters as valid: A-Z , 2-7 and the padding character '='.
It may help to think of this function as similar to the Convert.FromBase64String() function which requires valid base64 characters.
If the intention is to take an arbitrary string and get the associated base32 string representation, you can convert your text using a standard encoding-to-bytes function, then get the base32 string.
A function such as the following can get you a valid base32 string (which can then be converted back to bytes later using the function you asked about).
private string GetBase32StringFromUTF8(string text)
{
var utf8Bytes = Encoding.UTF8.GetBytes(text);
return OtpNet.Base32Encoding.ToString(utf8Bytes);
}
Running your provided string "3FC6145A2155E732" through that function returns a base32 string of :
"GNDEGNRRGQ2UCMRRGU2UKNZTGI======"
Hope this helps!
Hope this helps!
It absolutely did, thank you for the prompt response - I'll correct my approach.